Skip to content

Commit

Permalink
Bugfix - removeFeatureState fails with target.id === 0 (#8150) (#8164)
Browse files Browse the repository at this point in the history
* Fix an edge case where 0 target.id would fail to clear the feature state

* Check for number & string explicitly

* Add tests for 0 feature ID

* Fix logical check
  • Loading branch information
mourner authored Apr 17, 2019
1 parent 8af8aca commit 2d9001e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -878,12 +878,12 @@ class Style extends Evented {
return;
}

if (target.id && isNaN(featureId) || featureId < 0) {
if (target.id !== undefined && isNaN(featureId) || featureId < 0) {
this.fire(new ErrorEvent(new Error(`The feature id parameter must be non-negative.`)));
return;
}

if (key && !target.id) {
if (key && (typeof target.id !== 'string' && typeof target.id !== 'number')) {
this.fire(new ErrorEvent(new Error(`A feature id is requred to remove its specific state property.`)));
return;
}
Expand Down
21 changes: 21 additions & 0 deletions test/unit/ui/map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,27 @@ test('Map', (t) => {
t.end();
});
});
t.test('remove properties for zero-based feature IDs.', (t) => {
const map = createMap(t, {
style: {
"version": 8,
"sources": {
"geojson": createStyleSource()
},
"layers": []
}
});
map.on('load', () => {
map.setFeatureState({ source: 'geojson', id: 0}, {'hover': true, 'foo': true});
map.removeFeatureState({ source: 'geojson', id: 0});

const fState = map.getFeatureState({ source: 'geojson', id: 0});
t.equal(fState.hover, undefined);
t.equal(fState.foo, undefined);

t.end();
});
});
t.test('other properties persist when removing specific property', (t) => {
const map = createMap(t, {
style: {
Expand Down

0 comments on commit 2d9001e

Please sign in to comment.