GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 043e72...5d2753 )
by Benjamin
06:48 queued 02:54
created

test/reducers/components/datasource.test.js   A

Complexity

Total Complexity 34
Complexity/F 1

Size

Lines of Code 719
Function Count 34

Duplication

Duplicated Lines 428
Ratio 59.53 %

Importance

Changes 4
Bugs 1 Features 2
Metric Value
cc 0
wmc 34
c 4
b 1
f 2
nc 1
mnd 0
bc 25
fnc 34
dl 428
loc 719
rs 9.2
bpm 0.7352
cpm 1
noi 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
/* eslint-enable describe it */
2
import expect from 'expect';
3
import { fromJS } from 'immutable';
4
5
import {
6
    SET_DATA,
7
    DISMISS_EDITOR,
8
    REMOVE_ROW,
9
    ADD_NEW_ROW,
10
    SAVE_ROW,
11
    SORT_DATA,
12
    CLEAR_FILTER_LOCAL,
13
    FILTER_DATA,
14
    UPDATE_ROW
15
} from './../../../src/constants/ActionTypes';
16
17
import
18
    dataSource
19
from './../../../src/reducers/components/datasource';
20
21
import {
22
    resetLastUpdate
23
} from './../../../src/util/lastUpdate';
24
25
describe('The grid dataSource reducer setData func', () => {
26
    beforeEach(() => resetLastUpdate());
27
28
    it('Should set data with a total', () => {
29
30
        const state = fromJS({});
31
32
        const action = {
33
            stateKey: 'test-grid',
34
            type: SET_DATA,
35
            total: 2,
36
            data: [{ x: 1 }, { x: 2 }]
37
        };
38
39
        expect(
40
            dataSource(state, action)
41
        ).toEqualImmutable(fromJS({
42
            'test-grid': {
43
                data: [
44
                    { x: 1 }, { x: 2 }
45
                ],
46
                proxy: [
47
                    { x: 1 }, { x: 2 }
48
                ],
49
                treeData: undefined,
50
                gridType: 'grid',
51
                total: 2,
52
                currentRecords: [
53
                    { x: 1 }, { x: 2 }
54
                ],
55
                lastUpdate: 1
56
            }
57
        }));
58
59
    });
60
61
    it('Should set data without a total', () => {
62
63
        const state = fromJS({});
64
65
        const action = {
66
            stateKey: 'test-grid',
67
            type: SET_DATA,
68
            data: [{x: 1}, {x: 2}]
69
        };
70
71
        expect(
72
            dataSource(state, action)
73
        ).toEqualImmutable(fromJS({
74
            'test-grid': {
75
                data: [{ x: 1 }, { x: 2 }],
76
                proxy: [{ x: 1 }, { x: 2 }],
77
                total: 2,
78
                treeData: undefined,
79
                gridType: 'grid',
80
                currentRecords: [{ x: 1 }, { x: 2 }],
81
                lastUpdate: 1
82
            }
83
        }));
84
85
    });
86
87
    it('Should set currentRecords when they are passed', () => {
88
89
        const state = fromJS({});
90
91
        const action = {
92
            stateKey: 'test-grid',
93
            type: SET_DATA,
94
            data: [{x: 1}, {x: 2}],
95
            currentRecords: [
96
                { banana: 2 }
97
            ],
98
            lastUpdate: 1
99
        };
100
101
        expect(
102
            dataSource(state, action)
103
        ).toEqualImmutable(fromJS({
104
            'test-grid': {
105
                data: [{ x: 1 }, { x: 2 }],
106
                proxy: [{ x: 1 }, { x: 2 }],
107
                total: 2,
108
                treeData: undefined,
109
                gridType: 'grid',
110
                currentRecords: [{ banana: 2 }],
111
                lastUpdate: 1
112
            }
113
        }));
114
115
    });
116
117
});
118
119
describe('The grid dataSource reducer dissmissEditor func', () => {
120
    beforeEach(() => resetLastUpdate());
121
122
    it('Should wipe previous values upon dissmiss', () => {
123
124
        const inState = fromJS({
125
            'test-grid': {
126
                proxy: [
127
                    { cell: 1 },
128
                    { cell: 2 }
129
                ],
130
                total: 2
131
            }
132
        });
133
134
        const outState = fromJS({
135
            'test-grid': {
136
                proxy: [
137
                    { cell: 1 },
138
                    { cell: 2 }
139
                ],
140
                total: 2,
141
                data: [
142
                    { cell: 1 },
143
                    { cell: 2 }
144
                ],
145
                currentRecords: [
146
                    { cell: 1 },
147
                    { cell: 2 }
148
                ],
149
                isEditing: false,
150
                lastUpdate: 1
151
            }
152
        });
153
154
        const action = {
155
            stateKey: 'test-grid',
156
            type: DISMISS_EDITOR
157
        };
158
159
        expect(
160
            dataSource(inState, action)
161
        ).toEqualImmutable(outState);
162
    });
163
164
    it('Should decrement total if proxy less values than data', () => {
165
166
        const inState = fromJS({
167
            'test-grid': {
168
                data: [
169
                    { cell: 1 },
170
                    { cell: 2 },
171
                    { cell: 3 }
172
173
                ],
174
                proxy: [
175
                    { cell: 1 },
176
                    { cell: 2 }
177
                ],
178
                total: 3
179
            }
180
        });
181
182
        const outState = fromJS({
183
            'test-grid': {
184
                proxy: [
185
                    { cell: 1 },
186
                    { cell: 2 }
187
                ],
188
                total: 2,
189
                data: [
190
                    { cell: 1 },
191
                    { cell: 2 }
192
                ],
193
                currentRecords: [
194
                    { cell: 1 },
195
                    { cell: 2 }
196
                ],
197
                isEditing: false,
198
                lastUpdate: 1
199
            }
200
        });
201
202
        const action = {
203
            stateKey: 'test-grid',
204
            type: DISMISS_EDITOR
205
        };
206
207
        expect(
208
            dataSource(inState, action)
209
        ).toEqualImmutable(outState);
210
    });
211
});
212
213
describe('The grid dataSource reducer removeRow func', () => {
214
    beforeEach(() => resetLastUpdate());
215
216
    const inState = fromJS({
217
        'test-grid': {
218
            proxy: [
219
                { cell: 1 },
220
                { cell: 2 }
221
            ],
222
            data: [
223
                { cell: 1 },
224
                { cell: 2 }
225
            ],
226
            currentRecords: [
227
                { cell: 1 },
228
                { cell: 2 }
229
            ],
230
            total: 2,
231
            lastUpdate: 1
232
        }
233
    });
234
235
    it('Should remove row at 0 index of none is passed', () => {
236
237
        const outState = fromJS({
238
            'test-grid': {
239
                proxy: [
240
                    { cell: 2 }
241
                ],
242
                data: [
243
                    { cell: 2 }
244
                ],
245
                currentRecords: [
246
                    { cell: 2 }
247
                ],
248
                total: 2,
249
                lastUpdate: 1
250
            }
251
        });
252
253
        const action = {
254
            stateKey: 'test-grid',
255
            type: REMOVE_ROW
256
        };
257
258
        expect(
259
            dataSource(inState, action)
260
        ).toEqualImmutable(outState);
261
    });
262
263
    it('Should remove row at 1 index if arg is passed', () => {
264
265
        const outState = fromJS({
266
            'test-grid': {
267
                proxy: [
268
                    { cell: 1 }
269
                ],
270
                data: [
271
                    { cell: 1 }
272
                ],
273
                currentRecords: [
274
                    { cell: 1 }
275
                ],
276
                total: 2,
277
                lastUpdate: 1
278
            }
279
        });
280
281
        const action = {
282
            stateKey: 'test-grid',
283
            rowIndex: 1,
284
            type: REMOVE_ROW
285
        };
286
287
        expect(
288
            dataSource(inState, action)
289
        ).toEqualImmutable(outState);
290
291
    });
292
293
});
294
295
describe('The grid dataSource reducer addRow func', () => {
296
    beforeEach(() => resetLastUpdate());
297
298
    const inState = fromJS({
299
        'test-grid': {
300
            proxy: [
301
                { cell: 1 },
302
                { cell: 2 }
303
            ],
304
            data: [
305
                { cell: 1 },
306
                { cell: 2 }
307
            ],
308
            currentRecords: [
309
                { cell: 1 },
310
                { cell: 2 }
311
            ],
312
            total: 2
313
        }
314
    });
315
316
    it('Should add a new blank row if rows have been established', () => {
317
        const outState = fromJS({
318
            'test-grid': {
319
                proxy: [
320
                    { cell: 1 },
321
                    { cell: 2 }
322
                ],
323
                data: [
324
                    { cell: '' },
325
                    { cell: 1 },
326
                    { cell: 2 }
327
                ],
328
                currentRecords: [
329
                    { cell: 1 },
330
                    { cell: 2 }
331
                ],
332
                total: 3,
333
                isEditing: true,
334
                lastUpdate: 1
335
            }
336
        });
337
338
        const action = {
339
            stateKey: 'test-grid',
340
            type: ADD_NEW_ROW
341
        };
342
343
        expect(
344
            dataSource(inState, action)
345
        ).toEqualImmutable(outState);
346
    });
347
348
    it('Should add a new blank row if no rows have been established', () => {
349
        const innerState = fromJS({
350
            'test-grid': {
351
                proxy: [],
352
                data: [],
353
                currentRecords: [],
354
                total: 0,
355
                lastUpdate: 1
356
            }
357
        });
358
359
        const outState = fromJS({
360
            'test-grid': {
361
                proxy: [],
362
                data: [{}],
363
                currentRecords: [],
364
                total: 1,
365
                isEditing: true,
366
                lastUpdate: 1
367
            }
368
        });
369
370
        const action = {
371
            stateKey: 'test-grid',
372
            type: ADD_NEW_ROW
373
        };
374
375
        expect(
376
            dataSource(innerState, action)
377
        ).toEqualImmutable(outState);
378
    });
379
380
});
381
382
describe('The grid dataSource reducer saveRow func', () => {
383
    beforeEach(() => resetLastUpdate());
384
385
    const inState = fromJS({
386
        'test-grid': {
387
            proxy: [
388
                { cell: 1 },
389
                { cell: 2 }
390
            ],
391
            data: [
392
                { cell: 1 },
393
                { cell: 2 }
394
            ],
395
            currentRecords: [
396
                { cell: 1 },
397
                { cell: 2 }
398
            ],
399
            total: 2,
400
            lastUpdate: 1
401
        }
402
    });
403
404
    it('Should update row values on save', () => {
405
406
        const outState = fromJS({
407
            'test-grid': {
408
                proxy: [
409
                    { cell: 'newValues' },
410
                    { cell: 2 }
411
                ],
412
                data: [
413
                    { cell: 'newValues' },
414
                    { cell: 2 }
415
                ],
416
                currentRecords: [
417
                    { cell: 'newValues' },
418
                    { cell: 2 }
419
                ],
420
                total: 2,
421
                lastUpdate: 1
422
            }
423
        });
424
425
        const action = {
426
            stateKey: 'test-grid',
427
            type: SAVE_ROW,
428
            rowIndex: 0,
429
            values: { cell: 'newValues' }
430
        };
431
432
        expect(
433
            dataSource(inState, action)
434
        ).toEqualImmutable(outState);
435
436
    });
437
438
});
439
440
describe('The grid dataSource reducer sortData func', () => {
441
    beforeEach(() => resetLastUpdate());
442
443
    const inState = fromJS({
444
        'test-grid': {
445
            proxy: [
446
                { cell: 1 },
447
                { cell: 2 }
448
            ],
449
            data: [
450
                { cell: 1 },
451
                { cell: 2 }
452
            ],
453
            currentRecords: [
454
                { cell: 1 },
455
                { cell: 2 }
456
            ],
457
            total: 2,
458
            lastUpdate: 1
459
        }
460
    });
461
462
    it('Should update sort data', () => {
463
464
        const outState = fromJS({
465
            'test-grid': {
466
                proxy: [
467
                    { cell: 1 },
468
                    { cell: 2 }
469
                ],
470
                data: [
471
                    { cell: 2 },
472
                    { cell: 1 }
473
                ],
474
                currentRecords: [
475
                    { cell: 1 },
476
                    { cell: 2 }
477
                ],
478
                total: 2,
479
                lastUpdate: 1
480
            }
481
        });
482
483
        const action = {
484
            stateKey: 'test-grid',
485
            type: SORT_DATA,
486
            data: [
487
                { cell: 2 },
488
                { cell: 1 }
489
            ]
490
        };
491
492
        expect(
493
            dataSource(inState, action)
494
        ).toEqualImmutable(outState);
495
496
    });
497
498
});
499
500
describe('The grid dataSource reducer clearFilter func', () => {
501
    beforeEach(() => resetLastUpdate());
502
503
    const inState = fromJS({
504
        'test-grid': {
505
            proxy: [
506
                { cell: 1 },
507
                { cell: 2 }
508
            ],
509
            data: [
510
                { cell: 2 },
511
                { cell: 1 }
512
            ],
513
            currentRecords: [
514
                { cell: 2 },
515
                { cell: 1 }
516
            ],
517
            total: 2,
518
            lastUpdate: 1
519
        }
520
    });
521
522
    it('Should revert back to proxy if avail', () => {
523
524
        const outState = fromJS({
525
            'test-grid': {
526
                proxy: [
527
                    { cell: 1 },
528
                    { cell: 2 }
529
                ],
530
                data: [
531
                    { cell: 1 },
532
                    { cell: 2 }
533
                ],
534
                currentRecords: [
535
                    { cell: 1 },
536
                    { cell: 2 }
537
                ],
538
                total: 2,
539
                lastUpdate: 1
540
            }
541
        });
542
543
        const action = {
544
            stateKey: 'test-grid',
545
            type: CLEAR_FILTER_LOCAL
546
        };
547
548
        expect(
549
            dataSource(inState, action)
550
        ).toEqualImmutable(outState);
551
552
    });
553
554
});
555
556
describe('The grid dataSource reducer updateRow action', () => {
557
    beforeEach(() => resetLastUpdate());
558
559
    const inStateForUpdate = fromJS({
560
        'test-grid': {
561
            proxy: [
562
                { cell: 1 },
563
                { cell: 2, otherCell: 2 }
564
            ],
565
            data: [
566
                { cell: 1 },
567
                { cell: 2, otherCell: 2 }
568
            ],
569
            currentRecords: [
570
                { cell: 1 },
571
                { cell: 2, otherCell: 2 }
572
            ],
573
            total: 2,
574
            lastUpdate: 1
575
        }
576
    });
577
578
    it('Should update a single value', () => {
579
580
        const action = {
581
            stateKey: 'test-grid',
582
            type: UPDATE_ROW,
583
            rowIndex: 1,
584
            values: {
585
                cell: 'moreNewVals'
586
            }
587
        };
588
589
        expect(dataSource(inStateForUpdate, action))
590
            .toEqualImmutable(fromJS({
591
                'test-grid': {
592
                    proxy: [
593
                        { cell: 1 },
594
                        { cell: 'moreNewVals', otherCell: 2 }
595
                    ],
596
                    data: [
597
                        { cell: 1 },
598
                        { cell: 'moreNewVals', otherCell: 2 }
599
                    ],
600
                    currentRecords: [
601
                        { cell: 1 },
602
                        { cell: 'moreNewVals', otherCell: 2 }
603
                    ],
604
                    total: 2,
605
                    lastUpdate: 1
606
                }
607
            }));
608
    });
609
610
    it('Should update a multiple value', () => {
611
612
        const action = {
613
            stateKey: 'test-grid',
614
            type: UPDATE_ROW,
615
            rowIndex: 1,
616
            values: {
617
                cell: 'moreNewVals',
618
                otherCell: 'newValforCell2'
619
            }
620
        };
621
622
        expect(dataSource(inStateForUpdate, action))
623
            .toEqualImmutable(fromJS({
624
                'test-grid': {
625
                    proxy: [
626
                        { cell: 1 },
627
                        { cell: 'moreNewVals', otherCell: 'newValforCell2' }
628
                    ],
629
                    data: [
630
                        { cell: 1 },
631
                        { cell: 'moreNewVals', otherCell: 'newValforCell2' }
632
                    ],
633
                    currentRecords: [
634
                        { cell: 1 },
635
                        { cell: 'moreNewVals', otherCell: 'newValforCell2' }
636
                    ],
637
                    total: 2,
638
                    lastUpdate: 1
639
                }
640
            }));
641
642
    });
643
644
    it('Should return existing state if index doesnt exist', () => {
645
646
        const action = {
647
            stateKey: 'test-grid',
648
            type: UPDATE_ROW,
649
            rowIndex: 100,
650
            values: {
651
                cell: 'moreNewVals',
652
                otherCell: 'newValforCell2'
653
            }
654
        };
655
656
        expect(dataSource(inStateForUpdate, action))
657
            .toEqualImmutable(fromJS(inStateForUpdate));
658
659
    });
660
});
661
662
describe('The grid dataSource reducer filterData func', () => {
663
    beforeEach(() => resetLastUpdate());
664
665
    const inState = fromJS({
666
        'test-grid': {
667
            proxy: [
668
                { cell: 1 },
669
                { cell: 2 }
670
            ],
671
            data: [
672
                { cell: 1 },
673
                { cell: 2 }
674
            ],
675
            currentRecords: [
676
                { cell: 1 },
677
                { cell: 2 }
678
            ],
679
            total: 2,
680
            lastUpdate: 1
681
        }
682
    });
683
684
    it('Should revert back to proxy if avail', () => {
685
686
        const outState = fromJS({
687
            'test-grid': {
688
                proxy: [
689
                    { cell: 1 },
690
                    { cell: 2 }
691
                ],
692
                data: [
693
                    { cell: 'newVals' },
694
                    { cell: 'moreNewVals' }
695
                ],
696
                currentRecords: [
697
                    { cell: 1 },
698
                    { cell: 2 }
699
                ],
700
                total: 2,
701
                lastUpdate: 1
702
            }
703
        });
704
705
        const action = {
706
            stateKey: 'test-grid',
707
            type: FILTER_DATA,
708
            data: [
709
                { cell: 'newVals' },
710
                { cell: 'moreNewVals' }
711
            ]
712
        };
713
714
        expect(
715
            dataSource(inState, action)
716
        ).toEqualImmutable(outState);
717
718
    });
719
720
});
721