Skip to content
This repository was archived by the owner on Jan 7, 2020. It is now read-only.

Commit 7abd157

Browse files
committedDec 5, 2019
Add loading state test. Pre-review cleanup
1 parent 9a64ff4 commit 7abd157

File tree

4 files changed

+29
-38
lines changed

4 files changed

+29
-38
lines changed
 

‎src/geo/Geography.jsx

+8-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React, { Component } from 'react'
22
import { Link } from 'react-router-dom'
3+
import { isEqual } from 'lodash'
34
import { getItemCSV, getSubsetCSV, getSubsetDetails } from '../api.js'
45
import Header from '../common/Header.jsx'
5-
// import LEI_COUNTS from '../constants/leiCounts.js'
66
import MSAMD_COUNTS from '../constants/msamdCounts.js'
77
import STATE_COUNTS from '../constants/stateCounts.js'
88
import { makeSearchFromState, makeStateFromSearch } from '../query.js'
@@ -12,7 +12,6 @@ import './Geography.css'
1212
import InstitutionSelect from './InstitutionSelect'
1313
import ItemSelect from './ItemSelect.jsx'
1414
import { fetchLeis, filterLeis } from './leiUtils'
15-
import { isEqual } from 'lodash'
1615
import {
1716
createItemOptions,
1817
createVariableOptions,
@@ -63,7 +62,7 @@ class Geography extends Component {
6362
leiDetails: {
6463
loading: true,
6564
counts: {},
66-
leis: []
65+
leis: {}
6766
}
6867
}
6968

@@ -76,9 +75,7 @@ class Geography extends Component {
7675
componentDidMount(){
7776
this.fetchLeis()
7877
this.filterLeis()
79-
this.setState({
80-
isLargeFile: this.checkIfLargeFile(this.state.category, this.state.items)
81-
})
78+
this.setState({ isLargeFile: this.checkIfLargeFile(this.state.category, this.state.items) })
8279
}
8380

8481
componentDidUpdate(prevProps, prevState){
@@ -88,9 +85,7 @@ class Geography extends Component {
8885
if(geographyChanged) this.fetchLeis()
8986
if(geographyChanged || leisReloaded) this.filterLeis()
9087
if(leisReloaded)
91-
this.setState({
92-
isLargeFile: this.checkIfLargeFile(this.state.category, this.state.items)
93-
})
88+
this.setState({ isLargeFile: this.checkIfLargeFile(this.state.category, this.state.items) })
9489
}
9590

9691
updateSearch() {
@@ -167,8 +162,7 @@ class Geography extends Component {
167162
checkIfLargeCount(selected, countMap) {
168163
const MAX = 1048576
169164
if(!selected) return countMap > MAX
170-
const count = selected.reduce((acc, curr) => acc + countMap[curr], 0)
171-
return count > MAX
165+
return selected.reduce((acc, curr) => acc + countMap[curr], 0) > MAX
172166
}
173167

174168
onCategoryChange(catObj) {
@@ -268,7 +262,8 @@ class Geography extends Component {
268262
}
269263

270264
render() {
271-
const { category, items, leis, isLargeFile, variables, orderedVariables, details, loadingDetails, error } = this.state
265+
const { category, details, error, isLargeFile, items, leiDetails, leis,
266+
loadingDetails, orderedVariables, variables } = this.state
272267

273268
const nationwide = isNationwide(category)
274269
const enabled = nationwide || items.length
@@ -309,10 +304,7 @@ class Geography extends Component {
309304
<InstitutionSelect
310305
items={leis}
311306
onChange={this.onInstitutionChange}
312-
options={this.itemOptions}
313-
geoCategory={category}
314-
geoItems={items}
315-
leiDetails={this.state.leiDetails}
307+
leiDetails={leiDetails}
316308
/>
317309
<VariableSelect
318310
options={this.variableOptions}

‎src/geo/InstitutionSelect.jsx

+6-9
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const styleFn = {
6060
export function pruneLeiOptions(data, selected) {
6161
const selectedLeis = selected.map(s => s.value)
6262
const institutions = Object.values(data)
63-
let opts = institutions
63+
const opts = institutions
6464
.filter(institution => !selectedLeis.includes(institution.lei))
6565
.map(institution => ({ value: institution.lei, label: `${institution.name.toUpperCase()} - ${institution.lei}` }))
6666
.sort(sortByLabel)
@@ -70,17 +70,14 @@ export function pruneLeiOptions(data, selected) {
7070

7171
export function itemPlaceholder(loading, hasItems, category, selectedValues) {
7272
if(loading) return 'Loading...'
73-
if (!hasItems)
74-
return (
75-
'All institutions selected. ' +
76-
makeItemPlaceholder(category, selectedValues) +
77-
' to filter'
78-
)
79-
return makeItemPlaceholder(category, selectedValues)
73+
const placeholder = makeItemPlaceholder(category, selectedValues)
74+
if (!hasItems) return `All institutions selected. ${placeholder} to filter`
75+
return placeholder
8076
}
8177

8278
InstitutionSelect.defaultProps = {
83-
geoItems: []
79+
items: [],
80+
leiDetails: { leis: {}, loading: true}
8481
}
8582

8683
export default InstitutionSelect

‎src/geo/InstitutionSelect.test.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
import {pruneLeiOptions, itemPlaceholder} from './InstitutionSelect'
1+
import React from 'react'
2+
import { shallow } from 'enzyme'
3+
import InstitutionSelect, { itemPlaceholder, pruneLeiOptions } from './InstitutionSelect'
4+
5+
describe('InstitutionSelect', () => {
6+
it('renders loading states', () => {
7+
const wrapper = shallow(<InstitutionSelect />)
8+
expect(wrapper.find('Pills').length).toBe(0)
9+
expect(wrapper.find('LoadingIcon').length).toBe(1)
10+
expect(wrapper.find('StateManager').prop('placeholder')).toContain('Loading')
11+
})
12+
})
213

314
describe('pruneLeiOptions', () => {
415
it('includes an all option', () => {

‎src/geo/leiUtils.js

+3-12
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,21 @@ export function keepValidLeis(valid, selected) {
55
return selected.filter(s => valid[s])
66
}
77

8-
export function countRecords(selected, counts) {
9-
if (!selected) return 0
10-
return selected.reduce((acc, selectOpt) => acc + counts[selectOpt.value], 0)
11-
}
12-
138
export function filterLeis() {
149
const leis = this.state.leiDetails.leis
1510
if (Object.keys(leis).length) {
1611
const validLeis = keepValidLeis(leis, this.state.leis)
17-
if (!isEqual(this.state.leis, validLeis)) {
12+
if (!isEqual(this.state.leis, validLeis))
1813
this.onInstitutionChange(validLeis.map(v => ({ value: v })))
19-
}
2014
}
2115
}
2216

2317
export function fetchLeis() {
2418
const { category, items } = this.state
25-
this.setState(state => ({
26-
leiDetails: { ...state.leiDetails, loading: true }
27-
}))
19+
this.setState(state => ({ leiDetails: { ...state.leiDetails, loading: true }}))
2820
runFetch(makeFilersUrl({ category, items }))
2921
.then(data => {
30-
const counts = {}
31-
const leis = {}
22+
const counts = {}, leis = {}
3223
data.institutions.forEach(institution => {
3324
counts[institution.lei] = institution.count
3425
leis[institution.lei] = {...institution}

0 commit comments

Comments
 (0)
This repository has been archived.