Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

call setData when children disappear #152

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
call setData when children disappear
Previously, if you had a Layer that had Features as children,
and then the props changed such that the Layer no longer had
children, <source>.setData would not be called to remove the
Features from the source.

To fix this, I've changed Layer so that it will always call
setData when rendered. This should be fine because Layer#render
will only be called if the component should update.

I've added tests as well.
  • Loading branch information
jluxenberg committed Mar 14, 2017
commit 07e95f73f6c3e18c31dfc2e5f8431835be53b415
39 changes: 38 additions & 1 deletion src/__tests__/layer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ describe('Layer', () => {
let LayerWithContext: any;
let addLayerMock = jest.fn();
let addSourceMock = jest.fn();
let setDataMock = jest.fn();
let children: any[];
let childrenWithOneFeature: any[];
let feature: Object;

beforeEach(() => {
addLayerMock = jest.fn();
addSourceMock = jest.fn();
setDataMock = jest.fn();
feature = { coordinates: [-123, 45] };
children = [{ props: {}}];
childrenWithOneFeature = [{ props: feature }];

LayerWithContext = withContext({
map: React.PropTypes.object
Expand All @@ -21,7 +27,7 @@ describe('Layer', () => {
addSource: addSourceMock,
addLayer: addLayerMock,
on: jest.fn(),
getSource: jest.fn().mockReturnValue({ setData: jest.fn() })
getSource: jest.fn().mockReturnValue({ setData: setDataMock })
}
}))(Layer);
});
Expand Down Expand Up @@ -58,4 +64,35 @@ describe('Layer', () => {
}]);
});

it('Should set features based on children', () => {
const layer = mount(
<LayerWithContext
children={childrenWithOneFeature}
/> as React.ReactElement<any>
);

expect(setDataMock.mock.calls[0]).toEqual([{
'type': 'FeatureCollection',
'features': [{
'geometry': {...feature, 'type': 'Point'},
'properties': {id: 0},
'type': 'Feature'
}]
}])
});

it('Should set features to empty array when children disappear', () => {
const layer = mount(
<LayerWithContext
children={childrenWithOneFeature}
/> as React.ReactElement<any>
);

layer.setProps({ children: undefined });

expect(setDataMock.mock.calls[1]).toEqual([{
'type': 'FeatureCollection',
'features': []
}])
});
});
21 changes: 9 additions & 12 deletions src/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,20 +208,17 @@ export default class Layer extends React.PureComponent<Props, void> {

public render() {
const { map } = this.context;
const children = ([] as any).concat(this.props.children || []);

if (this.props.children) {
const children = ([] as any).concat(this.props.children);
const features = children
.map(({ props }: any, id: string) => this.makeFeature(props, id))
.filter(Boolean);

const features = children
.map(({ props }: any, id: string) => this.makeFeature(props, id))
.filter(Boolean);

const source = map.getSource(this.props.sourceId || this.id);
(source as MapboxGL.GeoJSONSource).setData({
type: 'FeatureCollection',
features
});
}
const source = map.getSource(this.props.sourceId || this.id);
(source as MapboxGL.GeoJSONSource).setData({
type: 'FeatureCollection',
features
});

return null;
}
Expand Down