1 | jest.mock('mapbox-gl', () => ({
|
2 | Map: jest.fn()
|
3 | }));
|
4 |
|
5 | import * as React from 'react';
|
6 | import ReactMapboxGl, { FitBounds } from '../map';
|
7 | import { mount } from 'enzyme';
|
8 | import { getMapMock } from '../jest/util';
|
9 |
|
10 | let mockfitBounds = jest.fn();
|
11 | let mockon = jest.fn();
|
12 |
|
13 | const getMock = (override = {}) =>
|
14 | getMapMock({
|
15 | fitBounds: mockfitBounds,
|
16 | on: mockon,
|
17 | remove: jest.fn(),
|
18 | getCenter: jest.fn().mockReturnValue({ lat: 2, lng: 1 }),
|
19 | getZoom: jest.fn(),
|
20 | getBearing: jest.fn(),
|
21 | getPitch: jest.fn(),
|
22 | flyTo: jest.fn(),
|
23 | ...override
|
24 | });
|
25 | describe('Map', () => {
|
26 |
|
27 | beforeEach(() => {
|
28 | mockfitBounds = jest.fn();
|
29 | mockon = jest.fn();
|
30 | });
|
31 |
|
32 | it('Should render the map correctly', () => {
|
33 | const MapboxMap = ReactMapboxGl({
|
34 | accessToken: '',
|
35 | mapInstance: getMock() as any
|
36 | });
|
37 | mount(<MapboxMap style="" />);
|
38 | });
|
39 |
|
40 | it('Should call fitBounds with the right parameters', () => {
|
41 | const fitBoundsValues: FitBounds = [[0, 1], [2, 3]];
|
42 | const fitBoundsOptions = { linear: true };
|
43 | const MapboxMap = ReactMapboxGl({
|
44 | accessToken: '',
|
45 | mapInstance: getMock() as any
|
46 | });
|
47 |
|
48 | mount(
|
49 | <MapboxMap
|
50 | style=""
|
51 | fitBounds={fitBoundsValues}
|
52 | fitBoundsOptions={fitBoundsOptions}
|
53 | />
|
54 | );
|
55 |
|
56 | expect(mockfitBounds).toBeCalledWith(fitBoundsValues, fitBoundsOptions, {
|
57 | fitboundUpdate: true
|
58 | });
|
59 | });
|
60 |
|
61 | it('Should update fitBounds if fitBoundsOptions changes', () => {
|
62 | const flyTo = jest.fn();
|
63 | const fitBoundsValues: FitBounds = [[0, 1], [2, 3]];
|
64 | const fitBoundsOptions = { offset: [150, 0] as [number, number] };
|
65 | const newFitBoundsOptions = { offset: [0, 0] };
|
66 |
|
67 | const MapboxMap = ReactMapboxGl({
|
68 | accessToken: '',
|
69 | mapInstance: getMock({
|
70 | flyTo,
|
71 | fitBounds: mockfitBounds
|
72 | }) as any
|
73 | });
|
74 |
|
75 | const wrapper = mount(
|
76 | <MapboxMap
|
77 | style=""
|
78 | fitBounds={fitBoundsValues}
|
79 | fitBoundsOptions={fitBoundsOptions}
|
80 | />
|
81 | );
|
82 |
|
83 | wrapper.setProps({ fitBoundsOptions: newFitBoundsOptions });
|
84 |
|
85 | expect(mockfitBounds.mock.calls[1][1]).toBe(newFitBoundsOptions);
|
86 | });
|
87 |
|
88 | it.skip('Should calc the center from fitbounds if center is not given', () => {
|
89 | const fitBoundsValues: FitBounds = [[0, 3], [2, 9]];
|
90 | const mockMap = getMock() as any;
|
91 | const MapboxMap = ReactMapboxGl({ accessToken: '', mapInstance: mockMap });
|
92 |
|
93 | mount(<MapboxMap style="" fitBounds={fitBoundsValues} />);
|
94 |
|
95 |
|
96 | const lastCall: any = mockMap.mock.calls[mockMap.mock.calls.length - 1];
|
97 | expect(lastCall[0].center).toEqual([1, 6]);
|
98 | });
|
99 |
|
100 | it('Should listen onStyleLoad event', () => {
|
101 | const MapboxMap = ReactMapboxGl({
|
102 | accessToken: '',
|
103 | mapInstance: getMock() as any
|
104 | });
|
105 |
|
106 | mount(<MapboxMap style="" onStyleLoad={jest.fn()} />);
|
107 |
|
108 | expect(mockon).toBeCalledWith('load', jasmine.any(Function));
|
109 | });
|
110 |
|
111 | it('Should update the map center position', () => {
|
112 | const flyTo = jest.fn();
|
113 | const center = [3, 4];
|
114 | const MapboxMap = ReactMapboxGl({
|
115 | accessToken: '',
|
116 | mapInstance: getMock({
|
117 | flyTo
|
118 | }) as any
|
119 | });
|
120 |
|
121 | const wrapper = mount(<MapboxMap style="" center={[1, 2]} />);
|
122 |
|
123 | wrapper.setProps({ center });
|
124 |
|
125 | expect(flyTo.mock.calls[0][0].center).toEqual(center);
|
126 | });
|
127 |
|
128 | it('Should update maxBounds', () => {
|
129 | const flyTo = jest.fn();
|
130 | const maxBoundsProps = [[1, 0], [0, 1]];
|
131 | const mockMaxBounds = jest.fn();
|
132 |
|
133 | const MapboxMap = ReactMapboxGl({
|
134 | accessToken: '',
|
135 | mapInstance: getMock({
|
136 | setMaxBounds: mockMaxBounds,
|
137 | flyTo
|
138 | }) as any
|
139 | });
|
140 |
|
141 | const wrapper = mount(<MapboxMap style="" />);
|
142 | wrapper.setProps({ maxBounds: maxBoundsProps });
|
143 |
|
144 | expect(mockMaxBounds).toBeCalledWith(maxBoundsProps);
|
145 | });
|
146 |
|
147 |
|
148 | it('Should not update zoom when using same reference equality', () => {
|
149 | const flyTo = jest.fn();
|
150 | const MapboxMap = ReactMapboxGl({
|
151 | accessToken: '',
|
152 | mapInstance: getMock({
|
153 | flyTo
|
154 | }) as any
|
155 | });
|
156 |
|
157 | const zoom: [number] = [3];
|
158 |
|
159 | const wrapper = mount(<MapboxMap style="" zoom={zoom} />);
|
160 |
|
161 | wrapper.setProps({ zoom });
|
162 |
|
163 | expect(flyTo).not.toHaveBeenCalled();
|
164 | });
|
165 |
|
166 | it('Should update the zoom on broken reference equality', () => {
|
167 | const flyTo = jest.fn();
|
168 | const MapboxMap = ReactMapboxGl({
|
169 | accessToken: '',
|
170 | mapInstance: getMock({
|
171 | flyTo
|
172 | }) as any
|
173 | });
|
174 |
|
175 | const wrapper = mount(<MapboxMap style="" zoom={[1]} />);
|
176 |
|
177 | wrapper.setProps({ zoom: [1] });
|
178 |
|
179 | expect(flyTo).toHaveBeenCalled();
|
180 | });
|
181 |
|
182 |
|
183 | it('Should not update bearing when using same reference equality', () => {
|
184 | const flyTo = jest.fn();
|
185 | const MapboxMap = ReactMapboxGl({
|
186 | accessToken: '',
|
187 | mapInstance: getMock({
|
188 | flyTo
|
189 | }) as any
|
190 | });
|
191 | const bearing: [number] = [3];
|
192 |
|
193 | const wrapper = mount(<MapboxMap style="" bearing={bearing} />);
|
194 |
|
195 | wrapper.setProps({ bearing });
|
196 |
|
197 | expect(flyTo).not.toHaveBeenCalled();
|
198 | });
|
199 |
|
200 | it('Should update the bearing on broken reference equality', () => {
|
201 | const flyTo = jest.fn();
|
202 | const MapboxMap = ReactMapboxGl({
|
203 | accessToken: '',
|
204 | mapInstance: getMock({
|
205 | flyTo
|
206 | }) as any
|
207 | });
|
208 |
|
209 | const wrapper = mount(<MapboxMap style="" bearing={[1]} />);
|
210 |
|
211 | wrapper.setProps({ bearing: [1] });
|
212 |
|
213 | expect(flyTo).toHaveBeenCalled();
|
214 | });
|
215 |
|
216 |
|
217 | it('Should not update pitch when using same reference equality', () => {
|
218 | const flyTo = jest.fn();
|
219 | const MapboxMap = ReactMapboxGl({
|
220 | accessToken: '',
|
221 | mapInstance: getMock({
|
222 | flyTo
|
223 | }) as any
|
224 | });
|
225 | const pitch: [number] = [3];
|
226 |
|
227 | const wrapper = mount(<MapboxMap style="" pitch={pitch} />);
|
228 |
|
229 | wrapper.setProps({ pitch });
|
230 |
|
231 | expect(flyTo).not.toHaveBeenCalled();
|
232 | });
|
233 |
|
234 | it('Should update the pitch on broken reference equality', () => {
|
235 | const flyTo = jest.fn();
|
236 | const MapboxMap = ReactMapboxGl({
|
237 | accessToken: '',
|
238 | mapInstance: getMock({
|
239 | flyTo
|
240 | }) as any
|
241 | });
|
242 |
|
243 | const wrapper = mount(<MapboxMap style="" pitch={[1]} />);
|
244 |
|
245 | wrapper.setProps({ pitch: [1] });
|
246 |
|
247 | expect(flyTo).toHaveBeenCalled();
|
248 | });
|
249 |
|
250 | it('Should pass animation options and flyTo options', () => {
|
251 | const flyTo = jest.fn();
|
252 | const MapboxMap = ReactMapboxGl({
|
253 | accessToken: '',
|
254 | mapInstance: getMock({
|
255 | flyTo
|
256 | }) as any
|
257 | });
|
258 | const zoom: [number] = [3];
|
259 | const flyToOptions = {
|
260 | speed: 0.1,
|
261 | curve: 0.9
|
262 | };
|
263 | const animationOptions = {
|
264 | offset: [20, 60]
|
265 | };
|
266 |
|
267 | const wrapper = mount(
|
268 | <MapboxMap
|
269 | style=""
|
270 | zoom={zoom}
|
271 | flyToOptions={flyToOptions}
|
272 | animationOptions={animationOptions}
|
273 | />
|
274 | );
|
275 |
|
276 | wrapper.setProps({ zoom: [1] });
|
277 |
|
278 | expect(flyTo.mock.calls[0][0]).toEqual({
|
279 | ...flyToOptions,
|
280 | ...animationOptions,
|
281 | bearing: undefined,
|
282 | center: { lat: 2, lng: 1 },
|
283 | pitch: undefined,
|
284 | zoom: 1
|
285 | });
|
286 | });
|
287 | });
|