-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
vis-state-test.js
5169 lines (4577 loc) · 134 KB
/
vis-state-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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 test from 'tape-catch';
import sinon from 'sinon';
import {console as Console} from 'global/window';
import {drainTasksForTesting, succeedTaskInTest, errorTaskInTest} from 'react-palm/tasks';
import CloneDeep from 'lodash.clonedeep';
import * as VisStateActions from 'actions/vis-state-actions';
import * as MapStateActions from 'actions/map-state-actions';
import reducer from 'reducers/vis-state';
import {INITIAL_VIS_STATE, DEFAULT_ANIMATION_CONFIG} from 'reducers/vis-state-updaters';
import {serializeLayer} from 'reducers/vis-state-merger';
import {getDefaultInteraction} from 'utils/interaction-utils';
import {getDefaultFilter} from 'utils/filter-utils';
import {createNewDataEntry} from 'utils/dataset-utils';
import {maybeToDate} from 'utils/data-utils';
import KeplerTable from 'utils/table-utils/kepler-table';
import {processCsvData, processGeojson} from '@kepler.gl/processors';
import {Layer, KeplerGlLayers} from '@kepler.gl/layers';
import {
ALL_FIELD_TYPES,
EDITOR_MODES,
LAYER_VIS_CONFIGS,
DEFAULT_TEXT_LABEL,
DEFAULT_COLOR_UI
} from '@kepler.gl/constants';
const {ArcLayer, PointLayer, GeojsonLayer, LineLayer, TripLayer} = KeplerGlLayers;
import {createDataContainer} from 'utils/table-utils';
// fixtures
import testData, {mergedTimeFilter, testFields, testAllData} from 'test/fixtures/test-csv-data';
import {
geojsonData,
geoJsonTripFilterProps,
expectedDataToFeature,
updatedGeoJsonLayer,
fields as geojsonFields,
rows as geojsonRows
} from 'test/fixtures/geojson';
import tripGeojson, {timeStampDomain, tripDataInfo} from 'test/fixtures/trip-geojson';
import {mockPolygonFeature, mockPolygonFeature2, mockPolygonData} from 'test/fixtures/polygon';
// test helpers
import {
cmpFilters,
cmpLayers,
cmpDatasets,
cmpDataset,
cmpObjectKeys,
cmpField,
assertDatasetIsTable
} from 'test/helpers/comparison-utils';
import {
applyActions,
StateWTripGeojson,
StateWSplitMaps,
StateWFilters,
StateWFiles,
StateWFilesFiltersLayerColor,
StateWH3Layer,
testCsvDataId,
testGeoJsonDataId,
InitialState
} from 'test/helpers/mock-state';
import {getNextColorMakerValue} from 'test/helpers/layer-utils';
const mockData = {
fields: [
{
name: 'start_point_lat',
type: 'real'
},
{
name: 'start_point_lng',
type: 'real'
},
{
name: 'end_point_lat',
type: 'real'
},
{
name: 'end_point_lng',
type: 'real'
}
],
data: [
[12.25, 37.75, 45.21, 100.12],
[null, 35.2, 45.0, 21.3],
[12.29, 37.64, 46.21, 99.127],
[null, null, 33.1, 29.34]
]
};
const expectedFields = [
{
name: 'start_point_lat',
displayName: 'start_point_lat',
id: 'start_point_lat',
type: 'real',
fieldIdx: 0,
analyzerType: 'FLOAT',
format: '',
valueAccessor: values => values[1]
},
{
name: 'start_point_lng',
displayName: 'start_point_lng',
id: 'start_point_lng',
type: 'real',
fieldIdx: 1,
analyzerType: 'FLOAT',
format: '',
valueAccessor: values => values[1]
},
{
name: 'end_point_lat',
displayName: 'end_point_lat',
id: 'end_point_lat',
type: 'real',
fieldIdx: 2,
analyzerType: 'FLOAT',
format: '',
valueAccessor: values => values[2]
},
{
name: 'end_point_lng',
displayName: 'end_point_lng',
id: 'end_point_lng',
type: 'real',
fieldIdx: 3,
analyzerType: 'FLOAT',
format: '',
valueAccessor: values => values[3]
}
];
const mockFilter = {
fieldIdx: 0,
name: mockData.fields[0].name,
displayName: mockData.fields[0].name,
id: mockData.fields[0].name,
type: 'range',
fieldType: 'real',
value: [12.25, 12.28]
};
const mockRawData = {
fields: [
{
name: 'start_point_lat',
id: 'start_point_lat',
displayName: 'start_point_lat',
type: 'real',
fieldIdx: 0
},
{
name: 'start_point_lng',
id: 'start_point_lng',
displayName: 'start_point_lng',
type: 'real',
fieldIdx: 1
},
{
name: 'end_point_lat',
id: 'end_point_lat',
displayName: 'end_point_lat',
type: 'real',
fieldIdx: 2
},
{
name: 'end_point_lng',
id: 'end_point_lng',
displayName: 'end_point_lng',
type: 'real',
fieldIdx: 3
}
],
rows: [
[12.25, 37.75, 45.21, 100.12],
[null, 35.2, 45.0, 21.3],
[12.29, 37.64, 46.21, 99.127],
[null, null, 33.1, 29.34]
]
};
test('#visStateReducer', t => {
t.deepEqual(
reducer(undefined, {}),
{...INITIAL_VIS_STATE, initialState: {}},
'should return the initial state'
);
t.end();
});
test('#visStateReducer -> ADD_FILTER', t => {
const dataId = 'kitten';
const newFilter = getDefaultFilter(dataId);
const newReducer = reducer({filters: [mockFilter]}, VisStateActions.addFilter(dataId));
const expectedReducer = {filters: [mockFilter, newFilter]};
t.equal(newReducer.filters.length, expectedReducer.filters.length, 'should add a default filter');
t.deepEqual(newReducer.filters[0], expectedReducer.filters[0], 'should add a default filter');
cmpFilters(t, newReducer.filters[1], expectedReducer.filters[1]);
t.end();
});
test('#visStateReducer -> ADD_LAYER.1', t => {
const oldState = {
...INITIAL_VIS_STATE,
datasets: {
puppy: {
data: mockData.data,
fields: mockData.fields
}
},
layers: [{id: 'existing_layer'}],
layerData: [[{data: [1, 2, 3]}, {data: [4, 5, 6]}]],
layerOrder: [0],
splitMaps: [
{
layers: {existing_layer: false}
},
{
layers: {existing_layer: false}
}
]
};
const newReducer = reducer(oldState, VisStateActions.addLayer());
const newId = newReducer.layers[1].id;
const expectedSplitMaps = [
{
layers: {
existing_layer: false,
[newId]: true
}
},
{
layers: {
existing_layer: false,
[newId]: true
}
}
];
t.equal(newReducer.layers.length, 2, 'should have 2 layers');
t.equal(newReducer.layers[1].config.isVisible, true, 'newLayer visibility should be set to true');
t.equal(
newReducer.layers[1].config.isConfigActive,
true,
'newLayer isConfigActive should be set to true'
);
t.equal(newReducer.layers[1].config.dataId, 'puppy', 'newLayer dataId should be set to default');
t.deepEqual(
newReducer.layerData,
[oldState.layerData[0], {}],
'newState should have empty layer datat'
);
t.deepEqual(newReducer.layerOrder, [0, 1], 'should add to layerOrder');
t.deepEqual(newReducer.splitMaps, expectedSplitMaps, 'should add to SplitMaps');
t.end();
});
test('#visStateReducer -> LAYER_TYPE_CHANGE.0', t => {
const layer = new Layer({id: 'blue'});
const oldState = {
...INITIAL_VIS_STATE
};
const nextState = reducer(oldState, VisStateActions.layerTypeChange());
t.equal(oldState, nextState, 'should return state when no argument is given');
const nextState2 = reducer(oldState, VisStateActions.layerTypeChange(layer, 'no_type'));
t.equal(oldState, nextState2, 'should return state when pass a none existing type');
t.end();
});
test('#visStateReducer -> LAYER_TYPE_CHANGE.1', t => {
const layer = new Layer({id: 'more_layer'});
const oldState = {
...INITIAL_VIS_STATE,
datasets: {
puppy: {
data: mockData.data,
fields: mockData.fields
}
},
layers: [{id: 'existing_layer'}, layer],
layerData: [[{data: [1, 2, 3]}, {data: [4, 5, 6]}]],
layerOrder: [1, 0],
hoverInfo: {
layer: {props: {id: 'more_layer'}},
picked: true
},
clicked: {
layer: {props: {id: 'more_layer'}},
picked: true
},
splitMaps: [
{
layers: {
existing_layer: false,
more_layer: true
}
},
{
layers: {
existing_layer: false,
more_layer: false
}
}
]
};
const nextState = reducer(oldState, VisStateActions.layerTypeChange(layer, 'point'));
const newId = nextState.layers[1].id;
const expectedSplitMaps = [
{
layers: {
existing_layer: false,
[newId]: true
}
},
{
layers: {
existing_layer: false,
[newId]: false
}
}
];
t.ok(nextState.layers[1].id !== 'more_layer', 'should update layer id');
t.equal(nextState.layers[1].type, 'point', 'should update type to point');
t.deepEqual(
nextState.splitMaps,
expectedSplitMaps,
'should add newId to SplitMaps, and replace old id'
);
t.ok(!nextState.clicked, 'should reset clicked');
t.ok(!nextState.hoverInfo, 'should reset hoverInfo');
t.end();
});
test('#visStateReducer -> LAYER_TYPE_CHANGE.2', t => {
const pointLayer = new PointLayer({id: 'a', dataId: 'smoothie'});
const mockColorRange = {
name: 'abc',
isReversed: true,
colors: ['a', 'b', 'c']
};
const datasets = createNewDataEntry({
info: {id: 'smoothie'},
data: {
rows: testAllData,
fields: testFields
}
});
const oldState = {
...INITIAL_VIS_STATE,
datasets,
layers: [pointLayer],
layerData: []
};
// set point layer colorField to a string field
let stringField = testFields.find(f => f.type === 'string');
stringField = {
...stringField,
valueAccessor: stringField.valueAccessor(datasets.smoothie.dataContainer)
};
let nextState = reducer(
oldState,
VisStateActions.layerVisualChannelConfigChange(pointLayer, {colorField: stringField}, 'color')
);
nextState = reducer(
nextState,
VisStateActions.layerVisConfigChange(nextState.layers[0], {
colorRange: mockColorRange
})
);
const newLayer = nextState.layers[0];
t.equal(newLayer.config.colorField, stringField, 'should update colorField');
t.equal(newLayer.config.colorScale, 'ordinal', 'should scale to ordinal');
t.deepEqual(
newLayer.config.colorDomain,
['driver_analytics', 'driver_analytics_0', 'driver_gps'],
'should calculate color domain'
);
t.equal(newLayer.config.visConfig.colorRange, mockColorRange, 'should update color range');
// set point layer sizeField to a int field
let intField = testFields.find(f => f.type === 'integer');
intField = {...intField, valueAccessor: intField.valueAccessor(datasets.smoothie.dataContainer)};
let nextState2 = reducer(
nextState,
VisStateActions.layerVisualChannelConfigChange(newLayer, {sizeField: intField}, 'size')
);
nextState2 = reducer(
nextState2,
VisStateActions.layerVisConfigChange(nextState2.layers[0], {
radiusRange: [5, 10]
})
);
const newLayer2 = nextState2.layers[0];
t.equal(newLayer2.config.sizeField, intField, 'should update sizeField');
t.equal(newLayer2.config.sizeScale, 'sqrt', 'should scale to sqrt');
t.deepEqual(newLayer2.config.sizeDomain, [1, 12124], 'should calculate size domain');
t.deepEqual(newLayer2.config.visConfig.radiusRange, [5, 10], 'should update size range');
// change point layer type to hexagon
const nextState3 = reducer(nextState2, VisStateActions.layerTypeChange(newLayer2, 'hexagon'));
const newLayer3 = nextState3.layers[0];
t.equal(newLayer3.type, 'hexagon', 'should change type to hexagon');
t.equal(newLayer3.config.colorField, stringField, 'should keep colorField');
t.deepEqual(
newLayer3.config.colorDomain,
[0, 1],
'should set colorDomain to default, it is calculated inside deck.gl layer'
);
t.equal(newLayer3.config.colorScale, 'ordinal', 'should set colorScale to ordinal');
t.equal(newLayer3.config.sizeScale, 'sqrt', 'should set sizeScale to default');
t.deepEqual(newLayer3.config.sizeDomain, [0, 1], 'should set sizeDomain to default');
t.equal(newLayer3.config.sizeField, intField, 'should keep sizeField');
t.notEqual(newLayer3.id, newLayer2.id, 'should change id');
t.equal(newLayer3.config.visConfig.colorRange, mockColorRange, 'should not deep copy colorRange');
t.equal(
newLayer3.config.visConfig.sizeRange,
LAYER_VIS_CONFIGS.elevationRange.defaultValue,
'should set sizeRange back to default'
);
// change point layer type to icon
const nextState4 = reducer(nextState2, VisStateActions.layerTypeChange(newLayer2, 'icon'));
const newLayer4 = nextState4.layers[0];
t.equal(newLayer4.type, 'icon', 'should change type to icon');
t.notEqual(newLayer4.id, newLayer2.id, 'should change id');
t.equal(newLayer4.config.colorField, stringField, 'should keep colorField');
t.deepEqual(
newLayer4.config.colorDomain,
['driver_analytics', 'driver_analytics_0', 'driver_gps'],
'should calculate color domain'
);
t.equal(newLayer4.config.colorScale, 'ordinal', 'should keep color scale');
t.equal(newLayer4.config.sizeField, intField, 'should keep sizeField');
t.equal(newLayer4.config.sizeScale, 'sqrt', 'should scale to linear');
t.deepEqual(newLayer4.config.sizeDomain, [1, 12124], 'should keep size domain');
t.deepEqual(newLayer4.config.visConfig.radiusRange, [5, 10], 'should keep size range');
t.equal(newLayer4.config.visConfig.colorRange, mockColorRange, 'should not deep copy colorRange');
t.end();
});
test('#visStateReducer -> LAYER_TYPE_CHANGE.3 -> animationConfig', t => {
const layer = new GeojsonLayer({
label: 'taro and blue',
dataId: 'taro',
columns: {geojson: {fieldIdx: 0, value: '_geojson'}},
isVisible: true,
color: [1, 1, 1],
id: 'taro'
});
const dataset = createNewDataEntry({
info: {id: 'taro'},
data: processGeojson(tripGeojson)
});
const oldState = {
...INITIAL_VIS_STATE,
datasets: dataset,
layers: [layer],
layerData: [{}],
layerOrder: [0]
};
// change GeoJson layer to Trip layer
const nextState = reducer(oldState, VisStateActions.layerTypeChange(layer, 'trip'));
const foundLayer = nextState.layers[0];
const foundLayerId = foundLayer.id;
t.ok(foundLayerId !== 'taro', 'should update layer id');
t.equal(foundLayer.type, 'trip', 'should update type to trip');
t.deepEqual(
foundLayer.config.animation,
{enabled: true, domain: timeStampDomain},
'should set correct animation domain'
);
t.deepEqual(
nextState.animationConfig,
{
...DEFAULT_ANIMATION_CONFIG,
domain: timeStampDomain,
currentTime: timeStampDomain[0],
defaultTimeFormat: 'L LTS'
},
'should update visState.animationConfig'
);
// change Trip layer to Geojson layer
const nextState2 = reducer(oldState, VisStateActions.layerTypeChange(foundLayer, 'geojson'));
const foundLayer2 = nextState2.layers[0];
t.ok(foundLayer2.id !== foundLayerId, 'should update layer id');
t.equal(foundLayer2.type, 'geojson', 'should update type to trip');
t.deepEqual(
foundLayer2.config.animation,
{enabled: false},
'should set correct animation domain fro Geojson layer'
);
t.deepEqual(
nextState2.animationConfig,
DEFAULT_ANIMATION_CONFIG,
'should set animationConfig to default'
);
t.end();
});
test('#visStateReducer -> LAYER_CONFIG_CHANGE -> isVisible -> animationConfig', t => {
const initialState = StateWTripGeojson;
const layer = initialState.visState.layers[0];
// change Trip layer isVisible
const nextState = reducer(
StateWTripGeojson.visState,
VisStateActions.layerConfigChange(layer, {isVisible: false})
);
t.deepEqual(
nextState.animationConfig,
{
domain: null,
currentTime: 1565577261000,
speed: 1,
isAnimating: false,
defaultTimeFormat: null,
timeFormat: null,
timezone: null
},
'should set animationConfig to default'
);
const nextState2 = reducer(
nextState,
VisStateActions.layerConfigChange(nextState.layers[0], {isVisible: true})
);
t.deepEqual(
nextState2.animationConfig,
{
...nextState2.animationConfig,
domain: timeStampDomain,
currentTime: timeStampDomain[0],
defaultTimeFormat: 'L LTS'
},
'should set animationConfig domain and currentTime'
);
t.end();
});
test('#visStateReducer -> LAYER_CONFIG_CHANGE -> isVisible -> splitMaps', t => {
const initialState = StateWSplitMaps.visState;
const layer = initialState.layers[0];
const initialSplitMaps = [
{layers: {'point-0': false, 'geojson-1': true}},
{layers: {'point-0': true, 'geojson-1': true}}
];
const expectedSplitMaps = [{layers: {'geojson-1': true}}, {layers: {'geojson-1': true}}];
t.deepEqual(initialState.splitMaps, initialSplitMaps, 'should has the same initial splitMaps');
const nextState = reducer(
initialState,
VisStateActions.layerConfigChange(layer, {isVisible: false})
);
t.equal(nextState.layers[0].config.isVisible, false, 'should set layer 0 visibility to false');
t.equal(initialState.layerData[0], nextState.layerData[0], 'should not update layerData');
t.deepEqual(nextState.splitMaps, expectedSplitMaps, 'should remove layer from splitMaps');
const nextState2 = reducer(
nextState,
VisStateActions.layerConfigChange(layer, {isVisible: true})
);
const initialSplitMaps2 = [
{layers: {'point-0': true, 'geojson-1': true}},
{layers: {'point-0': true, 'geojson-1': true}}
];
t.deepEqual(nextState2.splitMaps, initialSplitMaps2, 'should add layer to splitMaps');
t.end();
});
test('visStateReducer -> layerDataIdChangeUpdater', t => {
const initialState = CloneDeep(StateWFilesFiltersLayerColor).visState;
const pointLayer = initialState.layers[0];
const nextState = reducer(
initialState,
VisStateActions.layerConfigChange(pointLayer, {
dataId: testGeoJsonDataId
})
);
const updatedLayer = nextState.layers[0];
t.equal(updatedLayer.config.dataId, testGeoJsonDataId, 'should update point layer dataId');
t.deepEqual(
updatedLayer.config.columns,
{
altitude: {value: null, fieldIdx: -1, optional: true},
lat: {value: null, fieldIdx: -1},
lng: {value: null, fieldIdx: -1}
},
'should not update point layer column'
);
t.equal(updatedLayer.config.colorField, null, 'should not update point layer colorField');
// add layer
const nextState1 = reducer(nextState, VisStateActions.addLayer());
const newLayer = nextState1.layers[nextState1.layers.length - 1];
const nextState2 = reducer(
nextState1,
VisStateActions.layerConfigChange(newLayer, {
dataId: testGeoJsonDataId,
color: [1, 2, 3]
})
);
t.equal(
nextState2.layers[nextState1.layers.length - 1].config.dataId,
testGeoJsonDataId,
'should update new layer dataId'
);
t.deepEqual(
nextState2.layers[nextState1.layers.length - 1].config.color,
[1, 2, 3],
'should update new layer color'
);
t.end();
});
test('visStateReducer -> layerDataIdChangeUpdater -> geojson', t => {
const initialState = CloneDeep(StateWFilesFiltersLayerColor).visState;
const nextState = reducer(
initialState,
// add another geojson
VisStateActions.updateVisData([
{
info: {id: 'geojson2', label: 'Some Geojson'},
data: {fields: geojsonFields, rows: geojsonRows.slice(0, 3)}
}
])
);
// find geojson layer
const index = nextState.layers.findIndex(l => l.type === 'geojson');
const geojsonLayer = nextState.layers[index];
const id = geojsonLayer.id;
// change dataId
const nextState1 = reducer(
nextState,
VisStateActions.layerConfigChange(geojsonLayer, {
dataId: 'geojson2'
})
);
const neextGeojsonLayer = nextState1.layers.find(l => l.id === id);
t.equal(neextGeojsonLayer.config.dataId, 'geojson2', 'should update layer dataId');
t.equal(neextGeojsonLayer.dataToFeature.length, 3, 'should calculate dataToFeature');
t.equal(nextState1.layerData[index].data.length, 3, 'should calculate layerData');
t.end();
});
test('visStateReducer -> layerDataIdChangeUpdater -> validation', t => {
const initialState = CloneDeep(StateWFilesFiltersLayerColor).visState;
const pointLayer = initialState.layers[0];
const textField = pointLayer.config.textLabel[0].field;
const newDataInfo = {
id: 'new-data-csv',
label: 'new Data'
};
const fieldIdx = testFields.findIndex(f => f.name === textField.name);
// remove 1 field from sample data
const fields = testFields.filter(f => f.name !== textField.name);
const rows = testAllData.map(row => [
...row.slice(0, fieldIdx),
...row.slice(fieldIdx + 1, row.length)
]);
// add another dataset
const nextState = reducer(
initialState,
VisStateActions.updateVisData([{info: newDataInfo, data: {fields, rows}}])
);
const nextState1 = reducer(
nextState,
VisStateActions.layerConfigChange(pointLayer, {
dataId: 'new-data-csv'
})
);
const updatedLayer = nextState1.layers[0];
t.equal(updatedLayer.config.dataId, 'new-data-csv', 'should update point layer dataId');
t.equal(
updatedLayer.config.colorField.name,
'gps_data.types',
'should update point layer colorField'
);
t.deepEqual(
updatedLayer.config.columns,
{
altitude: {value: null, fieldIdx: -1, optional: true},
lat: {value: 'gps_data.lat', fieldIdx: 1},
lng: {value: 'gps_data.lng', fieldIdx: 2}
},
'should update point layer column'
);
t.equal(
updatedLayer.config.textLabel[0].field,
undefined,
'should assign fields not exists to undefined'
);
t.end();
});
test('#visStateReducer -> LAYER_VIS_CONFIG_CHANGE -> opacity', t => {
const initialState = StateWFiles.visState;
const layer = initialState.layers[0];
const nextState = reducer(
initialState,
VisStateActions.layerVisConfigChange(layer, {opacity: 0.3})
);
t.equal(nextState.layers[0].config.visConfig.opacity, 0.3, 'should update layer opacity');
t.end();
});
test('#visStateReducer -> LAYER_TEXT_LABEL_CHANGE', t => {
const initialState = StateWFiles.visState;
// point layer
const layer = initialState.layers[0];
t.deepEqual(layer.config.textLabel, [DEFAULT_TEXT_LABEL], 'should set initial textLabel');
const nextState = reducer(
initialState,
VisStateActions.layerTextLabelChange(layer, 0, 'random', 1)
);
t.equal(
nextState.layers[0].config.textLabel,
layer.config.textLabel,
'should not update textLabel if prop is not in textLabel'
);
const nextState2 = reducer(
nextState,
VisStateActions.layerTextLabelChange(nextState.layers[0], 0, 'anchor', 'start')
);
t.deepEqual(
nextState2.layers[0].config.textLabel[0],
{...DEFAULT_TEXT_LABEL, anchor: 'start'},
'should start text label prop'
);
const valueAccessor = () => 1;
const expectedField = {
name: 'taro',
valueAccessor
};
// set text label field
const nextState3 = reducer(
nextState2,
VisStateActions.layerTextLabelChange(nextState2.layers[0], 0, 'field', expectedField)
);
const expectedTextLabel1 = {
...DEFAULT_TEXT_LABEL,
anchor: 'start',
field: expectedField
};
t.deepEqual(
nextState3.layers[0].config.textLabel[0],
expectedTextLabel1,
'should set text field'
);
// add empty field
const nextState4 = reducer(
nextState3,
VisStateActions.layerTextLabelChange(nextState3.layers[0], 1)
);
t.deepEqual(
nextState4.layers[0].config.textLabel,
[expectedTextLabel1, DEFAULT_TEXT_LABEL],
'should add text label'
);
// add or remove labels
const nextState5 = reducer(
nextState4,
VisStateActions.layerTextLabelChange(nextState4.layers[0], 'all', 'fields', [
{name: 'blue', valueAccessor},
{name: 'taro', valueAccessor}
])
);
const expected5 = [
expectedTextLabel1,
{...DEFAULT_TEXT_LABEL, field: {name: 'blue', valueAccessor}}
];
t.deepEqual(nextState5.layers[0].config.textLabel, expected5, 'should add text label taro');
// // add 1 more label
const nextState6 = reducer(
nextState5,
VisStateActions.layerTextLabelChange(nextState5.layers[0], 2, 'field', {
name: 'cat',
valueAccessor
})
);
const expected6 = [
expectedTextLabel1,
{...DEFAULT_TEXT_LABEL, field: {name: 'blue', valueAccessor}},
{...DEFAULT_TEXT_LABEL, field: {name: 'cat', valueAccessor}}
];
t.deepEqual(nextState6.layers[0].config.textLabel, expected6, 'should add text label cat');
// remove label
const nextState7 = reducer(
nextState6,
VisStateActions.layerTextLabelChange(nextState6.layers[0], 2, 'field', null)
);
const expected7 = [
expectedTextLabel1,
{...DEFAULT_TEXT_LABEL, field: {name: 'blue', valueAccessor}}
];
t.deepEqual(nextState7.layers[0].config.textLabel, expected7, 'should remove text label cat');
// remove label with all
const nextState8 = reducer(
nextState7,
VisStateActions.layerTextLabelChange(nextState7.layers[0], 'all', 'fields', [
{name: 'blue', valueAccessor}
])
);
const expected8 = [{...DEFAULT_TEXT_LABEL, field: {name: 'blue', valueAccessor}}];
t.deepEqual(nextState8.layers[0].config.textLabel, expected8, 'should remove text label blue');
t.end();
});
test('#visStateReducer -> REORDER_LAYER', t => {
const newReducer = reducer(
{layers: [], layerOrder: [0, 1, 2]},
VisStateActions.reorderLayer([0, 2, 1])
);
t.deepEqual(newReducer, {layers: [], layerOrder: [0, 2, 1]}, 'should re order layers');
t.end();
});
test('#visStateReducer -> UPDATE_LAYER_BLENDING', t => {
const newReducer = reducer(
{layerBlending: 'none'},
VisStateActions.updateLayerBlending('additive')
);
t.deepEqual(newReducer, {layerBlending: 'additive'}, 'should update layerBlending');
t.end();
});
test('#visStateReducer -> REMOVE_FILTER', t => {
const initialState = CloneDeep(StateWFilters.visState);
// filter[0]: 'time' testCsvData
// filter[1]: 'RATE' testGeoJsonData
const dataset0 = initialState.datasets[testCsvDataId];
const dataset1 = initialState.datasets[testGeoJsonDataId];
const expectedData0 = {
...dataset0,
gpuFilter: {
filterRange: [
[1474606800000 - 1474588800000, 1474617600000 - 1474588800000],
[0, 0],
[0, 0],
[0, 0]
],
filterValueUpdateTriggers: {
gpuFilter_0: 'time',
gpuFilter_1: null,
gpuFilter_2: null,
gpuFilter_3: null
},
filterValueAccessor: {
inputs: [
{
index: 0
}
],
result: [0, 0, 0, 0]
}
},
filterRecord: {
dynamicDomain: [],
fixedDomain: [initialState.filters[0]],
cpu: [],
gpu: [initialState.filters[0]]
}
};
const expectedData1 = {
...dataset1,
filteredIndex: dataset1.allIndexes,
filteredIndexForDomain: dataset1.allIndexes,
filterRecord: {
dynamicDomain: [],
fixedDomain: [],
cpu: [],
gpu: []
}
};
const expectedFilters = [mergedTimeFilter];
// remove smoothie filter - gpu: true, fixedDomain: false
const newReducer = reducer(initialState, VisStateActions.removeFilter(1));
const expectedLayerData1 = {data: dataset1.dataContainer.mapIndex(d => d)};
const expectedState = {
...initialState,
filters: expectedFilters,
datasets: {
[testCsvDataId]: expectedData0,
[testGeoJsonDataId]: expectedData1
},
layerData: [initialState.layerData[0], expectedLayerData1]
};
cmpObjectKeys(t, expectedState, newReducer, 'After removing filter, visState');
Object.keys(newReducer).forEach(key => {
switch (key) {