UNPKG

2.06 kBTypeScriptView Raw
1import * as React from 'react';
2import Image from '../image';
3import { getMapMock } from '../jest/util';
4import { mount } from 'enzyme';
5
6describe('Image', () => {
7 it('Should add image on mount', () => {
8 const mapMock = getMapMock();
9 const onLoaded = jest.fn();
10 const onError = jest.fn();
11
12 const imageId = 'image';
13 const imageData = {};
14 const imageOptions = {};
15
16 mount(
17 <Image
18 id={imageId}
19 map={mapMock}
20 data={imageData}
21 options={imageOptions}
22 onError={onError}
23 onLoaded={onLoaded}
24 />
25 );
26
27 expect(mapMock.addImage.mock.calls[0]).toEqual([
28 imageId,
29 imageData,
30 imageOptions
31 ]);
32
33 expect(onLoaded).toBeCalled();
34 expect(onError).not.toBeCalled();
35 });
36
37 it('Should remove image on unmount', () => {
38 const mapMock = getMapMock({
39 getStyle: jest.fn(() => ({}))
40 });
41 const onLoaded = jest.fn();
42 const onError = jest.fn();
43
44 const imageId = 'image';
45 const imageData = {};
46 const imageOptions = {};
47
48 const component = mount(
49 <Image
50 id={imageId}
51 map={mapMock}
52 data={imageData}
53 options={imageOptions}
54 onError={onError}
55 onLoaded={onLoaded}
56 />
57 );
58
59 expect(mapMock.addImage).toBeCalled();
60 expect(onLoaded).toBeCalled();
61
62 component.unmount();
63 expect(mapMock.removeImage).toBeCalled();
64 });
65
66 it('Should not call removeImage when map styles are undefined', () => {
67 const mapMock = getMapMock({
68 getStyle: jest.fn(() => undefined)
69 });
70
71 const onLoaded = jest.fn();
72 const onError = jest.fn();
73
74 const imageId = 'image';
75 const imageData = {};
76 const imageOptions = {};
77
78 const component = mount(
79 <Image
80 id={imageId}
81 map={mapMock}
82 data={imageData}
83 options={imageOptions}
84 onError={onError}
85 onLoaded={onLoaded}
86 />
87 );
88
89 expect(onLoaded).toBeCalled();
90
91 component.unmount();
92 expect(mapMock.removeImage).not.toBeCalled();
93 });
94});