Skip to content

Commit

Permalink
Merge pull request #728 from citychallenge/master
Browse files Browse the repository at this point in the history
Fixing error thrown by <Image /> component on unmount
  • Loading branch information
mklopets authored Jul 14, 2019
2 parents c6746d1 + d9578f8 commit 6429794
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
33 changes: 32 additions & 1 deletion src/__tests__/image.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ describe('Image', () => {
});

it('Should remove image on unmount', () => {
const mapMock = getMapMock();
const mapMock = getMapMock({
getStyle: jest.fn(() => ({}))
});
const onLoaded = jest.fn();
const onError = jest.fn();

Expand All @@ -60,4 +62,33 @@ describe('Image', () => {
component.unmount();
expect(mapMock.removeImage).toBeCalled();
});

it('Should not call removeImage when map styles are undefined', () => {
const mapMock = getMapMock({
getStyle: jest.fn(() => undefined)
});

const onLoaded = jest.fn();
const onError = jest.fn();

const imageId = 'image';
const imageData = {};
const imageOptions = {};

const component = mount(
<Image
id={imageId}
map={mapMock}
data={imageData}
options={imageOptions}
onError={onError}
onLoaded={onLoaded}
/>
);

expect(onLoaded).toBeCalled();

component.unmount();
expect(mapMock.removeImage).not.toBeCalled();
});
});
2 changes: 1 addition & 1 deletion src/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Image extends React.Component<Props> {

private static removeImage(props: Props) {
const { id, map } = props;
if (map) {
if (map && map.getStyle()) {
map.removeImage(id);
}
}
Expand Down

0 comments on commit 6429794

Please sign in to comment.