1 | import * as React from 'react';
|
2 | import Layer, { ImageDefinitionWithOptions } from '../layer';
|
3 | import { getMapMock } from '../jest/util';
|
4 | import { mount } from 'enzyme';
|
5 |
|
6 | describe('Layer', () => {
|
7 | it('Should render layer with default options', () => {
|
8 | const children = [{ props: {}, type: 'symbol', key: '1' }];
|
9 | const mockMap = getMapMock();
|
10 |
|
11 | mount(<Layer id="1" map={mockMap as any} children={children} />);
|
12 |
|
13 | expect(mockMap.addLayer.mock.calls[0]).toEqual([
|
14 | {
|
15 | id: '1',
|
16 | source: '1',
|
17 | type: 'symbol',
|
18 | layout: {},
|
19 | paint: {}
|
20 | },
|
21 | undefined
|
22 | ]);
|
23 | });
|
24 |
|
25 | it('Should set all parameters of add layer', () => {
|
26 | const before = 'test1';
|
27 | const children = [{ props: {}, type: 'symbol', key: '1' }];
|
28 | const mockMap = getMapMock();
|
29 |
|
30 | const props = {
|
31 | id: '123',
|
32 | type: 'symbol' as 'symbol',
|
33 | paint: {},
|
34 | layout: {},
|
35 | metadata: {},
|
36 | filter: []
|
37 | };
|
38 |
|
39 | const mappedProps = {
|
40 | minZoom: 2,
|
41 | maxZoom: 10,
|
42 | sourceLayer: 'sourceTest',
|
43 | sourceId: 'test'
|
44 | };
|
45 |
|
46 | mount(
|
47 | <Layer
|
48 | children={children}
|
49 |
|
50 | map={mockMap as any}
|
51 | {...props}
|
52 | {...mappedProps}
|
53 | before={before}
|
54 | />
|
55 | );
|
56 | expect(mockMap.addLayer.mock.calls[0]).toEqual([
|
57 | {
|
58 | ...props,
|
59 | minzoom: 2,
|
60 | maxzoom: 10,
|
61 | source: 'test',
|
62 | 'source-layer': 'sourceTest'
|
63 | },
|
64 | before
|
65 | ]);
|
66 | });
|
67 |
|
68 | it('Should render layer with default source', () => {
|
69 | const children = [{ props: {}, type: 'symbol', key: '1' }];
|
70 | const mockMap = getMapMock({ getSource: jest.fn(() => undefined) });
|
71 |
|
72 |
|
73 | mount(<Layer id="1" map={mockMap as any} children={children} />);
|
74 |
|
75 | expect(mockMap.addSource.mock.calls[0]).toEqual([
|
76 | '1',
|
77 | {
|
78 | type: 'geojson',
|
79 | data: {
|
80 | type: 'FeatureCollection',
|
81 | features: []
|
82 | }
|
83 | }
|
84 | ]);
|
85 | });
|
86 |
|
87 | it('Should set all parameters of add source with geojsonSourceOptions', () => {
|
88 | const children = [{ props: {}, type: 'symbol', key: '1' }];
|
89 | const mockMap = getMapMock({ getSource: jest.fn(() => undefined) });
|
90 |
|
91 | const geoJSONSourceOptions = {
|
92 | maxzoom: 10,
|
93 | buffer: 2,
|
94 | tolerance: 1,
|
95 | cluster: 10,
|
96 | clusterRadius: 50,
|
97 | clusterMaxZoom: 10
|
98 | };
|
99 | const layerSourceId = 'testId';
|
100 | mount(
|
101 | <Layer
|
102 | children={children}
|
103 |
|
104 | map={mockMap as any}
|
105 | id={layerSourceId}
|
106 | geoJSONSourceOptions={geoJSONSourceOptions}
|
107 | />
|
108 | );
|
109 |
|
110 | expect(mockMap.addSource.mock.calls[0]).toEqual([
|
111 | layerSourceId,
|
112 | {
|
113 | type: 'geojson',
|
114 | ...geoJSONSourceOptions,
|
115 | data: {
|
116 | type: 'FeatureCollection',
|
117 | features: []
|
118 | }
|
119 | }
|
120 | ]);
|
121 | });
|
122 |
|
123 | it('Should set features based on children', () => {
|
124 | const mockMap = getMapMock();
|
125 | const feature = { coordinates: [-123, 45] };
|
126 | const children = [{ props: feature, type: 'symbol', key: '1' }];
|
127 |
|
128 | mount(
|
129 |
|
130 | <Layer id="1" map={mockMap as any} children={children} />
|
131 | );
|
132 |
|
133 | expect(mockMap.getSource().setData.mock.calls[0]).toEqual([
|
134 | {
|
135 | type: 'FeatureCollection',
|
136 | features: [
|
137 | {
|
138 | geometry: { ...feature, type: 'Point' },
|
139 | properties: { id: 0 },
|
140 | type: 'Feature'
|
141 | }
|
142 | ]
|
143 | }
|
144 | ]);
|
145 | });
|
146 |
|
147 | it('Should set features to empty array when children disappear', () => {
|
148 | const mockMap = getMapMock();
|
149 | const feature = { coordinates: [-123, 45] };
|
150 | const children = [{ props: feature, type: 'symbol', key: '1' }];
|
151 |
|
152 | const layer = mount(
|
153 |
|
154 | <Layer id="1" map={mockMap as any} children={children} />
|
155 | );
|
156 |
|
157 | layer.setProps({ children: undefined });
|
158 |
|
159 | expect(mockMap.getSource().setData.mock.calls[1]).toEqual([
|
160 | {
|
161 | type: 'FeatureCollection',
|
162 | features: []
|
163 | }
|
164 | ]);
|
165 | });
|
166 |
|
167 | it('Should flatten features', () => {
|
168 | const mockMap = getMapMock();
|
169 |
|
170 |
|
171 | const children: any = [
|
172 | <div key="1">Test</div>,
|
173 | [<div key="2">Test</div>, <div key="3">Test</div>]
|
174 | ];
|
175 |
|
176 |
|
177 | mount(<Layer id="1" map={mockMap as any} children={children} />);
|
178 |
|
179 | expect(mockMap.getSource().setData.mock.calls[0][0].features).toHaveLength(
|
180 | 3
|
181 | );
|
182 | });
|
183 |
|
184 | it('Should add images', () => {
|
185 | const mockMap = getMapMock();
|
186 | const images: ImageDefinitionWithOptions = ['test', new Image(), {}];
|
187 | const children = [{ props: {}, type: 'symbol', key: '1' }];
|
188 |
|
189 | mount(
|
190 |
|
191 | <Layer id="1" children={children} map={mockMap as any} images={images} />
|
192 | );
|
193 |
|
194 | expect(mockMap.addImage.mock.calls[0]).toEqual(images);
|
195 | });
|
196 |
|
197 | it('Should render Polygon for fill', () => {
|
198 | const mockMap = getMapMock();
|
199 | const feature = { coordinates: [[[-123, 45], [123, 45]]] };
|
200 | const children = [{ props: feature, type: 'symbol', key: '1' }];
|
201 |
|
202 | mount(
|
203 |
|
204 | <Layer id="1" type="fill" children={children} map={mockMap as any} />
|
205 | );
|
206 |
|
207 | expect(mockMap.getSource().setData.mock.calls[0]).toEqual([
|
208 | {
|
209 | type: 'FeatureCollection',
|
210 | features: [
|
211 | {
|
212 | geometry: { ...feature, type: 'Polygon' },
|
213 | properties: { id: 0 },
|
214 | type: 'Feature'
|
215 | }
|
216 | ]
|
217 | }
|
218 | ]);
|
219 | });
|
220 |
|
221 | it('Should render MultiPolygon for fill', () => {
|
222 | const mockMap = getMapMock();
|
223 | const feature = { coordinates: [[[[-123, 45], [123, 45]]]] };
|
224 | const children = [{ props: feature, type: 'symbol', key: '1' }];
|
225 |
|
226 | mount(
|
227 |
|
228 | <Layer id="1" type="fill" children={children} map={mockMap as any} />
|
229 | );
|
230 |
|
231 | expect(mockMap.getSource().setData.mock.calls[0]).toEqual([
|
232 | {
|
233 | type: 'FeatureCollection',
|
234 | features: [
|
235 | {
|
236 | geometry: { ...feature, type: 'MultiPolygon' },
|
237 | properties: { id: 0 },
|
238 | type: 'Feature'
|
239 | }
|
240 | ]
|
241 | }
|
242 | ]);
|
243 | });
|
244 |
|
245 | it('Should update minZoom and maxZoom if they change', () => {
|
246 | const mockMap = getMapMock();
|
247 | const children = [{ props: {}, type: 'symbol', key: '1' }];
|
248 | const wrapper = mount(
|
249 |
|
250 | <Layer map={mockMap as any} id="zoomer" children={children} />
|
251 | );
|
252 |
|
253 | wrapper.setProps({ minZoom: 4 });
|
254 | wrapper.setProps({ maxZoom: 10 });
|
255 | wrapper.setProps({ minZoom: undefined, maxZoom: undefined });
|
256 | wrapper.setProps({ maxZoom: 6 });
|
257 |
|
258 | expect(mockMap.setLayerZoomRange.mock.calls).toEqual([
|
259 | ['zoomer', 4, undefined],
|
260 | ['zoomer', 4, 10],
|
261 | ['zoomer', undefined, undefined],
|
262 | ['zoomer', undefined, 6]
|
263 | ]);
|
264 | });
|
265 |
|
266 | it('Should start listening onClick mouse event', () => {
|
267 | const mockMap = getMapMock();
|
268 | const id = 'layer-test';
|
269 | mount(
|
270 |
|
271 | <Layer id={id} map={mockMap as any} onClick={jest.fn()} />
|
272 | );
|
273 |
|
274 | expect(mockMap.on.mock.calls[0][0]).toEqual('click');
|
275 | expect(mockMap.on.mock.calls[0][1]).toEqual(id);
|
276 | });
|
277 | });
|