Skip to content

Commit

Permalink
Handle invalid id in checkbox svg map
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorCazanave committed Jun 11, 2020
1 parent 4000cce commit a404ae3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
4 changes: 2 additions & 2 deletions __tests__/checkbox-svg-map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe('CheckboxSVGMap component', () => {
wrapper = mount(
<CheckboxSVGMap
map={FakeMap}
selectedLocationIds={['id0', 'id1']}
selectedLocationIds={['id0', 'id1', 'invalid-id']}
onChange={handleOnChange}
/>,
{ attachTo: container }
Expand All @@ -121,7 +121,7 @@ describe('CheckboxSVGMap component', () => {
handleOnChange.mockClear();
});

test('selects initial locations when ids are provided', () => {
test('selects initial locations when valid ids are provided', () => {
expect(selectedLocation.props()['aria-checked']).toBeTruthy();
expect(otherSelectedLocation.props()['aria-checked']).toBeTruthy();
expect(unselectedLocation.props()['aria-checked']).toBeFalsy();
Expand Down
5 changes: 3 additions & 2 deletions src/checkbox-svg-map.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class CheckboxSVGMap extends React.Component {
// Cannot use ref on SvgMap (with React 16.0.0) because it is a functional component
// https://5a046bf5a6188f4b8fa4938a--reactjs.netlify.app/docs/refs-and-the-dom.html#refs-and-functional-components
const svgNode = ReactDOM.findDOMNode(this);
const selectedLocations = this.props.selectedLocationIds.map(locationId => svgNode.getElementById(locationId));
const selectedLocations = this.props.selectedLocationIds.map(locationId => svgNode.getElementById(locationId))
.filter(location => !!location); // Remove null locations when invalid id

this.setState({ selectedLocations });
}
Expand All @@ -37,7 +38,7 @@ class CheckboxSVGMap extends React.Component {
* @returns {boolean} True if the location is selected
*/
isLocationSelected(location) {
return this.state.selectedLocations.findIndex(selectedLocation => selectedLocation.id === location.id) > -1;
return this.state.selectedLocations.some(selectedLocation => selectedLocation.id === location.id);
}

/**
Expand Down

0 comments on commit a404ae3

Please sign in to comment.