-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
map-control-test.js
310 lines (266 loc) · 9.7 KB
/
map-control-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Copyright (c) 2022 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
/* eslint-disable max-statements */
import React from 'react';
import sinon from 'sinon';
import test from 'tape';
import {IntlWrapper, mountWithTheme} from 'test/helpers/component-utils';
import {MapControlButton} from 'components/common/styled-components';
import ToolbarItem from 'components/common/toolbar-item';
import {mapFieldsSelector} from 'components/kepler-gl';
import {
appInjector,
MapLayerSelector,
MapContainerFactory,
MapLegendFactory,
MapControlFactory,
MapControlToolbarFactory
} from 'components';
import {
mockKeplerProps,
mockKeplerPropsWithState,
StateWSplitMaps,
StateWFiles
} from '../../../helpers/mock-state';
import {Cube3d, Split, Legend, DrawPolygon, Layers, Delete} from 'components/common/icons';
import {LOCALE_CODES, LOCALES} from '@kepler.gl/localization';
import {toggleMapControl} from 'actions';
import {keplerGlReducerCore} from 'reducers';
const MapControl = appInjector.get(MapControlFactory);
const MapContainer = appInjector.get(MapContainerFactory);
const MapLegend = appInjector.get(MapLegendFactory);
const MapControlToolbar = appInjector.get(MapControlToolbarFactory);
const initialProps = mapFieldsSelector(mockKeplerProps);
test('MapControlFactory - display options', t => {
let wrapper;
t.doesNotThrow(() => {
wrapper = mountWithTheme(
<IntlWrapper>
<MapContainer {...initialProps} />
</IntlWrapper>
);
}, 'Map Container should not fail without props');
// wrapper
// MapControl
t.equal(wrapper.find(MapControl).length, 1, 'Should render MapControl');
// layer selector is not active
t.equal(wrapper.find(MapControlButton).length, 5, 'Should show 5 MapControlButton');
t.equal(wrapper.find(Split).length, 1, 'Should show 1 split map button');
t.equal(wrapper.find(Cube3d).length, 1, 'Should show 1 toggle 3d button');
t.equal(wrapper.find(Legend).length, 1, 'Should show 1 map legend button');
t.equal(wrapper.find(DrawPolygon).length, 1, 'Should show 1 map draw button');
t.equal(wrapper.find('.map-control-button__locale').length, 1, 'Should show 1 locale button');
// with split map and active layer selector
const propsWithSplitMap = mapFieldsSelector(mockKeplerPropsWithState({state: StateWSplitMaps}));
wrapper.setProps({
children: <MapContainer {...propsWithSplitMap} />
});
t.equal(wrapper.find(MapControlButton).length, 6, 'Should show 6 MapControlButton');
t.equal(wrapper.find(Split).length, 0, 'Should show 0 split map split button');
t.equal(wrapper.find(Delete).length, 1, 'Should show 1 split map delete button');
t.equal(wrapper.find(Layers).length, 1, 'Should show 1 Layer button');
// with 0 mapcontrols
wrapper.setProps({
children: <MapContainer {...propsWithSplitMap} mapControls={{}} />
});
t.equal(wrapper.find(MapControlButton).length, 0, 'Should show 0 MapControlButton');
t.end();
});
test('MapControlFactory - click options', t => {
const onToggleSplitMap = sinon.spy();
const onTogglePerspective = sinon.spy();
const onToggleMapControl = sinon.spy();
const onSetEditorMode = sinon.spy();
const onToggleEditorVisibility = sinon.spy();
const onSetLocale = sinon.spy();
const visStateActions = {
setEditorMode: onSetEditorMode,
toggleEditorVisibility: onToggleEditorVisibility
};
const mapStateActions = {
toggleSplitMap: onToggleSplitMap,
togglePerspective: onTogglePerspective
};
const uiStateActions = {
toggleMapControl: onToggleMapControl,
setLocale: onSetLocale
};
const mapContainerProps = mapFieldsSelector(
mockKeplerPropsWithState({
state: StateWSplitMaps,
visStateActions,
mapStateActions,
uiStateActions
})
);
let wrapper;
t.doesNotThrow(() => {
wrapper = mountWithTheme(
<IntlWrapper>
<MapContainer {...mapContainerProps} />
</IntlWrapper>
);
}, 'MapContainer should not fail without props');
// layer selector is not active
t.equal(wrapper.find(MapControlButton).length, 6, 'Should show 5 MapControlButton');
t.equal(wrapper.find(Delete).length, 1, 'Should show 1 delete split map button');
// click split Map
wrapper
.find('.map-control-button.split-map')
.at(0)
.simulate('click');
t.ok(onToggleSplitMap.calledOnce, 'should call onToggleSplitMap');
t.deepEqual(onToggleSplitMap.args[0], [0], 'should call onToggleSplitMap with mapindex');
// click toggle3d
wrapper
.find('.map-control-button.toggle-3d')
.at(0)
.simulate('click');
t.ok(onTogglePerspective.calledOnce, 'should call onTogglePerspective');
// click map legend
wrapper
.find('.map-control-button.show-legend')
.at(0)
.simulate('click');
t.equal(wrapper.find(MapLegend).length, 1, 'should render MapLegend');
// click map draw
wrapper
.find('.map-control-button.map-draw')
.at(0)
.simulate('click');
t.equal(wrapper.find(MapLegend).length, 1, 'should render MapLegend');
t.ok(onToggleMapControl.calledOnce, 'should call onToggleMapControl');
t.deepEqual(
onToggleMapControl.args[0],
['mapDraw', 0],
'should call onToggleMapControl with mapDraw'
);
// click locale
wrapper
.find('.map-control-button.map-locale')
.at(0)
.simulate('click');
t.ok(onToggleMapControl.calledTwice, 'should call onToggleMapControl');
t.deepEqual(
onToggleMapControl.args[1],
['mapLocale', 0],
'should call onToggleMapControl with mapLocale'
);
// click layer selector
wrapper
.find('.map-control-button.toggle-layer')
.at(0)
.simulate('click');
t.ok(onToggleMapControl.calledThrice, 'should call onToggleMapControl');
t.deepEqual(
onToggleMapControl.args[2],
['visibleLayers', 0],
'should call onToggleMapControl with visibleLayers'
);
t.end();
});
test('MapControlFactory - show panels', t => {
// show legend
let updateState = keplerGlReducerCore(StateWFiles, toggleMapControl('mapLegend', 0));
let mapContainerProps = mapFieldsSelector(mockKeplerPropsWithState({state: updateState}));
let wrapper;
t.doesNotThrow(() => {
wrapper = mountWithTheme(
<IntlWrapper>
<MapContainer {...mapContainerProps} />
</IntlWrapper>
);
}, 'MapContainer should not fail without props');
// show legend
t.equal(wrapper.find(MapLegend).length, 1, 'should render 1 MapLegend');
// show layer selector
const toggleLayerForMap = sinon.spy();
const visStateActions = {
toggleLayerForMap
};
updateState = keplerGlReducerCore(StateWSplitMaps, toggleMapControl('visibleLayers', 1));
mapContainerProps = mapFieldsSelector(
mockKeplerPropsWithState({state: updateState, visStateActions})
);
wrapper.setProps({
children: <MapContainer {...mapContainerProps} index={1} />
});
// click layer selector
t.equal(wrapper.find(MapLayerSelector).length, 1, 'should render 1 MapLayerSelector');
t.equal(
wrapper.find('.map-layer-selector__item').length,
Object.keys(updateState.visState.splitMaps[1].layers).length,
'MapLayerSelector should render correct number of layers'
);
wrapper
.find('input')
.at(0)
.simulate('change');
t.ok(toggleLayerForMap.calledOnce, 'should call toggleLayerForMap');
t.deepEqual(
toggleLayerForMap.args[0],
[1, 'point-0'],
'should call toggleLayerForMap with mapIndex and layerid'
);
// show map draw panel
updateState = keplerGlReducerCore(StateWSplitMaps, toggleMapControl('mapDraw', 1));
mapContainerProps = mapFieldsSelector(mockKeplerPropsWithState({state: updateState}));
wrapper.setProps({
children: <MapContainer {...mapContainerProps} index={1} />
});
t.equal(wrapper.find(MapControlToolbar).length, 1, 'should render 1 MapControlToolbar');
t.equal(
wrapper
.find(MapControlToolbar)
.at(0)
.find(ToolbarItem).length,
4,
'should render 4 ToolbarItem'
);
// show locale
updateState = keplerGlReducerCore(StateWSplitMaps, toggleMapControl('mapLocale', 1));
mapContainerProps = mapFieldsSelector(mockKeplerPropsWithState({state: updateState}));
wrapper.setProps({
children: <MapContainer {...mapContainerProps} index={1} />
});
t.equal(wrapper.find(MapControlToolbar).length, 1, 'should render 1 MapControlToolbar');
t.equal(
wrapper
.find(MapControlToolbar)
.at(0)
.find(ToolbarItem).length,
Object.keys(LOCALE_CODES).length,
`should render ${Object.keys(LOCALE_CODES).length} LOCALE_CODES`
);
Object.keys(LOCALE_CODES).forEach((locale, index) => {
t.equal(
wrapper
.find(MapControlToolbar)
.at(0)
.find(ToolbarItem)
.at(index)
.find('.toolbar-item__title')
.text(),
LOCALES[locale],
'should render correct locale'
);
});
t.end();
});