Skip to content

Commit

Permalink
Adding the Image component tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel committed Jan 23, 2019
1 parent 2308c19 commit 51fc671
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/__tests__/image.test.tsx
@@ -0,0 +1,63 @@
import * as React from 'react';
import Image from '../image';
import { getMapMock } from '../jest/util';
import { mount } from 'enzyme';

describe('Image', () => {
it('Should add image on mount', () => {
const mapMock = getMapMock();
const onLoaded = jest.fn();
const onError = jest.fn();

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

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

expect(mapMock.addImage.mock.calls[0]).toEqual([
imageId,
imageData,
imageOptions
]);

expect(onLoaded).toBeCalled();
expect(onError).not.toBeCalled();
});

it('Should remove image on unmount', () => {
const mapMock = getMapMock();
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(mapMock.addImage).toBeCalled();
expect(onLoaded).toBeCalled();

component.unmount();
expect(mapMock.removeImage).toBeCalled();
});
});
2 changes: 2 additions & 0 deletions src/jest/util.tsx
Expand Up @@ -13,6 +13,8 @@ export const getMapMock = (override?: { [key: string]: any }) => ({
setLayerZoomRange: jest.fn(),
getLayer: jest.fn(),
addImage: jest.fn(),
loadImage: jest.fn(),
removeImage: jest.fn(),
hasImage: jest.fn(),
getSource: jest.fn().mockReturnValue({ setData: jest.fn() }),
project: jest.fn(),
Expand Down

0 comments on commit 51fc671

Please sign in to comment.