Skip to content
This repository was archived by the owner on Jun 10, 2019. It is now read-only.

Commit 3c7cd55

Browse files
committed
Fix tests - pass code schools as a prop to child components
1 parent 99c6a9a commit 3c7cd55

4 files changed

Lines changed: 56 additions & 45 deletions

File tree

src/scenes/home/codeSchools/approvedSchools/approvedSchools.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import _ from 'lodash';
22
import React, { Component } from 'react';
3+
import PropTypes from 'prop-types';
34
import Section from 'shared/components/section/section';
45
import SchoolCard from 'shared/components/schoolCard/schoolCard';
56
import styles from './approvedSchools.css';
@@ -10,16 +11,15 @@ class ApprovedSchools extends Component {
1011
this.state = {
1112
vaSchools: null
1213
};
14+
}
1315

14-
fetch('https://api.operationcode.org/api/v1/code_schools.json')
15-
.then(response => response.json()).then(data =>
16-
this.setState({ vaSchools: this.getApprovedSchools(data) })
17-
);
16+
componentWillMount() {
17+
this.setState({ vaSchools: this.loadSchools() });
1818
}
1919

20-
getApprovedSchools = (schools) => {
20+
loadSchools() {
2121
let approvedSchools = [];
22-
schools.forEach((school) => {
22+
this.props.schools.forEach((school) => {
2323
const locations = school.locations.filter(location => location.va_accepted === true);
2424
if (locations.length > 0) {
2525
approvedSchools = approvedSchools.concat(locations.map(location =>
@@ -31,8 +31,7 @@ class ApprovedSchools extends Component {
3131
}
3232

3333
render() {
34-
console.log(JSON.stringify(this.state.vaSchools, null, 2));
35-
const vaSchools = !this.state.vaSchools ? null : this.state.vaSchools
34+
const vaSchools = this.state.vaSchools
3635
.map(school =>
3736
(
3837
<SchoolCard
@@ -75,4 +74,8 @@ class ApprovedSchools extends Component {
7574
}
7675
}
7776

77+
ApprovedSchools.propTypes = {
78+
schools: PropTypes.array.isRequired // eslint-disable-line
79+
};
80+
7881
export default ApprovedSchools;

src/scenes/home/codeSchools/codeSchools.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,17 @@ class CodeSchools extends Component {
1313
super(props);
1414

1515
this.state = {
16-
schools: {}
16+
schools: null
1717
};
1818
}
19-
19+
componentWillMount() {
20+
return fetch('https://api.operationcode.org/api/v1/code_schools.json').then(response =>
21+
response.json().then((data) => {
22+
console.log(data); // eslint-disable-line
23+
this.setState({ schools: data });
24+
})
25+
);
26+
}
2027
render() {
2128
return (
2229
<div>
@@ -64,10 +71,10 @@ class CodeSchools extends Component {
6471
</div>
6572
</Section>
6673

67-
<ApprovedSchools />
68-
<PartnerSchools />
69-
<OnlineSchools />
70-
<StateSortedSchools />
74+
{this.state.schools && <ApprovedSchools schools={this.state.schools} />}
75+
{this.state.schools && <PartnerSchools schools={this.state.schools} />}
76+
{this.state.schools && <OnlineSchools schools={this.state.schools} />}
77+
{this.state.schools && <StateSortedSchools schools={this.state.schools} />}
7178
</div>
7279
);
7380
}

src/scenes/home/codeSchools/onlineSchools/onlineSchools.js

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { Component } from 'react';
2+
import PropTypes from 'prop-types';
23
import Section from 'shared/components/section/section';
34
import SchoolCard from 'shared/components/schoolCard/schoolCard';
45
import ImageCard from 'shared/components/imageCard/imageCard';
@@ -13,32 +14,31 @@ class OnlineSchools extends Component {
1314
this.state = {
1415
eSchools: null
1516
};
17+
}
1618

17-
fetch('https://api.operationcode.org/api/v1/code_schools.json')
18-
.then(response => response.json()).then(data =>
19-
this.setState({ eSchools: data })
20-
);
19+
componentWillMount() {
20+
this.setState({ eSchools: this.loadSchools() });
2121
}
2222

23+
loadSchools() {
24+
return this.props.schools.filter(school => school.has_online);
25+
}
2326
render() {
24-
const eSchools = !this.state.eSchools ? null : this.state.eSchools
25-
.filter(school => school.has_online)
26-
.map(school =>
27-
(
28-
<SchoolCard
29-
key={school.name}
30-
alt={school.name}
31-
schoolName={school.name}
32-
link={school.url}
33-
schoolAddress="Online"
34-
logo={school.logo}
35-
GI={school.va_accepted ? 'Yes' : 'No'}
36-
fullTime={school.full_time ? 'Full-Time' : 'Flexible'}
37-
hardware={school.hardware_included ? 'Yes' : 'No'}
38-
/>
39-
)
40-
);
41-
27+
const eSchools = this.state.eSchools.map(school =>
28+
(
29+
<SchoolCard
30+
key={school.name}
31+
alt={school.name}
32+
schoolName={school.name}
33+
link={school.url}
34+
schoolAddress="Online"
35+
logo={school.logo}
36+
GI={school.va_accepted ? 'Yes' : 'No'}
37+
fullTime={school.full_time ? 'Full-Time' : 'Flexible'}
38+
hardware={school.hardware_included ? 'Yes' : 'No'}
39+
/>
40+
)
41+
);
4242
return (
4343
<Section
4444
id="onlineSchools"
@@ -99,4 +99,8 @@ class OnlineSchools extends Component {
9999
}
100100
}
101101

102+
OnlineSchools.propTypes = {
103+
schools: PropTypes.array.isRequired // eslint-disable-line
104+
};
105+
102106
export default OnlineSchools;

src/scenes/home/codeSchools/stateSortedSchools/stateSortedSchools.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
import React, { Component } from 'react';
2+
import PropTypes from 'prop-types';
23
import Section from 'shared/components/section/section';
34
import SchoolCard from 'shared/components/schoolCard/schoolCard';
45
import FormInput from 'shared/components/form/formInput/formInput';
56
import styles from './stateSortedSchools.css';
67
import stateCodes from '../stateCodes.json';
78

8-
const gettingSchoolData = fetch('https://api.operationcode.org/api/v1/code_schools.json')
9-
.then(response => response.json());
10-
119
class StateSortedSchools extends Component {
1210
constructor(props) {
1311
super(props);
1412
this.state = {
1513
query: null,
16-
schools: null,
1714
schoolsByState: null
1815
};
19-
20-
gettingSchoolData.then(data =>
21-
this.setState({ schools: data })
22-
);
2316
}
2417

2518
onSearchChange = (value) => {
@@ -44,7 +37,7 @@ class StateSortedSchools extends Component {
4437
return school.state.includes(input) || stateName.includes(input);
4538
}
4639

47-
this.state.schools.forEach((school) => {
40+
this.props.schools.forEach((school) => {
4841
school.locations.filter(_school => matchesState(_school, userInput)).forEach((location) => {
4942
schools.push({
5043
name: school.name,
@@ -105,4 +98,8 @@ class StateSortedSchools extends Component {
10598
}
10699
}
107100

101+
StateSortedSchools.propTypes = {
102+
schools: PropTypes.array.isRequired // eslint-disable-line
103+
};
104+
108105
export default StateSortedSchools;

0 commit comments

Comments
 (0)