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/actions/GridActions.test.js   B

Complexity

Total Complexity 38
Complexity/F 1

Size

Lines of Code 775
Function Count 38

Duplication

Duplicated Lines 421
Ratio 54.32 %

Importance

Changes 3
Bugs 1 Features 2
Metric Value
cc 0
wmc 38
c 3
b 1
f 2
nc 1
mnd 0
bc 35
fnc 38
dl 421
loc 775
rs 8.3999
bpm 0.921
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
import expect from 'expect';
2
3
import {
4
    getAsyncData,
5
    setColumns,
6
    setSortDirection,
7
    doLocalSort,
8
    doRemoteSort,
9
    setColumnVisibility,
10
    resizeColumns,
11
    setData,
12
    setTreeData,
13
    setTreeNodeVisibility
14
} from './../../src/actions/GridActions';
15
16
describe('The getAsyncData actions', () => {
17
18
    it('Should return a successful res with a function', (done) => {
19
20
        const read = () => {
21
            return new Promise(resolve => {
22
                resolve({
23
                    data: [],
24
                    total: 0
25
                });
26
            });
27
        };
28
29
        const res = [];
30
31
        getAsyncData({
32
            dataSource: read,
33
            stateKey: 'test-grid'
34
        })((resp) => {
35
            res.push(resp);
36
        });
37
38
        setTimeout(() => {
39
            expect(res).toEqual([
40
                { stateKey: 'test-grid', type: 'DISMISS_EDITOR' },
41
                {
42
                    state: true,
43
                    stateKey: 'test-grid',
44
                    type: 'SET_LOADING_STATE'
45
                },
46
                {
47
                    state: false,
48
                    stateKey: 'test-grid',
49
                    type: 'SET_LOADING_STATE'
50
                },
51
                {
52
                    currentRecords: [],
53
                    data: [],
54
                    stateKey: 'test-grid',
55
                    success: true,
56
                    total: 0,
57
                    type: 'SET_DATA'
58
                }
59
            ]);
60
            done();
61
        }, 0);
62
63
    });
64
65
    it('Should return a failure res with a faulty function', (done) => {
66
67
        const read = () => {
68
            return new Promise(resolve => {
69
                resolve({});
70
            });
71
        };
72
73
        const res = [];
74
75
        getAsyncData({
76
            dataSource: read,
77
            stateKey: 'test-grid'
78
        })((resp) => {
79
            res.push(resp);
80
        });
81
82
        setTimeout(() => {
83
            expect(res).toEqual([
84
                { stateKey: 'test-grid', type: 'DISMISS_EDITOR' },
85
                {
86
                    state: true,
87
                    stateKey: 'test-grid',
88
                    type: 'SET_LOADING_STATE'
89
                },
90
                {
91
                    state: false,
92
                    stateKey: 'test-grid',
93
                    type: 'SET_LOADING_STATE'
94
                },
95
                {
96
                    error: 'Unable to Retrieve Grid Data',
97
                    errorOccurred: true,
98
                    type: 'ERROR_OCCURRED',
99
                    stateKey: 'test-grid'
100
                }
101
            ]);
102
            done();
103
        }, 0);
104
105
    });
106
107
});
108
109
describe('The getAsyncData actions', () => {
110
111
    it([
112
        'Should return the correct remote sort ',
113
        'response when datasource is a func'].join(''), (done) => {
114
        const res = [];
115
116
        const dataSource = () => (
117
            new Promise(resolve => resolve({
118
                data: [],
119
                total: 0
120
            }))
121
        );
122
123
        const dispatch = val => (res.push(val));
124
125
        doRemoteSort({
126
            dataSource,
127
            pageIndex: 1,
128
            pageSize: 30,
129
            sortParams: {
130
                name: 'ASC'
131
            },
132
            stateKey: 'test-grid'
133
        })(dispatch);
134
135
        setTimeout(() => {
136
            expect(res).toEqual([
137
                {
138
                    state: true,
139
                    stateKey: 'test-grid',
140
                    type: 'SET_LOADING_STATE'
141
                },
142
                {
143
                    currentRecords: [],
144
                    data: [],
145
                    stateKey: 'test-grid',
146
                    success: true,
147
                    total: 0,
148
                    type: 'SET_DATA'
149
                },
150
                {
151
                    state: false,
152
                    stateKey: 'test-grid',
153
                    type: 'SET_LOADING_STATE'
154
                }
155
            ]);
156
            done();
157
        }, 10);
158
159
    });
160
161
});
162
163
describe('The setColumns actions', () => {
164
165
    const columns = [
166
        {
167
            name: 'col1',
168
            dataIndex: 'col1'
169
        },
170
        {
171
            name: 'col2',
172
            dataIndex: 'col2'
173
        }
174
    ];
175
176
    const stateKey = 'test-grid';
177
178
    it('Should add a unique ID if none is given', () => {
179
        expect(setColumns({
180
            columns,
181
            stateKey
182
        })).toEqual({
183
            type: 'SET_COLUMNS',
184
            columns: [
185
                {
186
                    dataIndex: 'col1',
187
                    id: 'Y29sMWdyaWQtY29sdW1u',
188
                    name: 'col1'
189
                },
190
                {
191
                    dataIndex: 'col2',
192
                    id: 'Y29sMmdyaWQtY29sdW1u',
193
                    name: 'col2'
194
                }
195
            ],
196
            stateKey,
197
            stateful: undefined
198
        });
199
    });
200
201
    it('Should keep id if they are defined', () => {
202
203
        const colsWithIds = [
204
            {
205
                dataIndex: 'col1',
206
                id: 'hasanid',
207
                name: 'col1'
208
            },
209
            {
210
                dataIndex: 'col2',
211
                id: 'alsohasanid',
212
                name: 'col2'
213
            }
214
        ];
215
216
        expect(setColumns({
217
            columns: colsWithIds,
218
            stateKey,
219
            stateful: true
220
        })).toEqual({
221
            type: 'SET_COLUMNS',
222
            columns: colsWithIds,
223
            stateKey,
224
            stateful: true
225
        });
226
227
    });
228
229
});
230
231
describe('The setSortDirection actions', () => {
232
233
    const columns = [
234
        {
235
            name: 'col1',
236
            dataIndex: 'col1',
237
            id: 'uniqueID'
238
        },
239
        {
240
            name: 'col2',
241
            dataIndex: 'col2',
242
            id: 'anotherId'
243
        }
244
    ];
245
246
    const stateKey = 'test-grid';
247
248
    it('Should add sort direction', () => {
249
        expect(setSortDirection({
250
            columns,
251
            stateKey,
252
            sortDirection: 'DESC',
253
            id: 'uniqueID'
254
        })).toEqual({
255
            type: 'SET_SORT_DIRECTION',
256
            columns: [
257
                {
258
                    dataIndex: 'col1',
259
                    id: 'uniqueID',
260
                    name: 'col1',
261
                    sortDirection: 'DESC'
262
                },
263
                {
264
                    dataIndex: 'col2',
265
                    id: 'anotherId',
266
                    name: 'col2',
267
                    sortDirection: null
268
                }
269
            ],
270
            stateKey
271
        });
272
    });
273
274
    it('Should add sort direction and wipe previous direction', () => {
275
        expect(setSortDirection({
276
            columns: [
277
                {
278
                    name: 'col1',
279
                    dataIndex: 'col1',
280
                    id: 'uniqueID'
281
                },
282
                {
283
                    name: 'col2',
284
                    dataIndex: 'col2',
285
                    id: 'anotherId',
286
                    sortDirection: 'ASC'
287
                }
288
            ],
289
            stateKey,
290
            sortDirection: 'DESC',
291
            id: 'uniqueID'
292
        })).toEqual({
293
            type: 'SET_SORT_DIRECTION',
294
            columns: [
295
                {
296
                    dataIndex: 'col1',
297
                    id: 'uniqueID',
298
                    name: 'col1',
299
                    sortDirection: 'DESC'
300
                },
301
                {
302
                    dataIndex: 'col2',
303
                    id: 'anotherId',
304
                    name: 'col2',
305
                    sortDirection: null
306
                }
307
            ],
308
            stateKey
309
        });
310
    });
311
312
});
313
314
describe('The doLocalSort actions', () => {
315
316
    it('Should return sortObject', () => {
317
        expect(
318
            doLocalSort({
319
                stateKey: 'test-grid',
320
                data: []
321
            })
322
        ).toEqual({
323
            stateKey: 'test-grid',
324
            data: [],
325
            type: 'SORT_DATA'
326
        });
327
    });
328
329
});
330
331
describe('The setColumnVisibility actions', () => {
332
333
    it('Should hide column', () => {
334
        expect(
335
            setColumnVisibility({
336
                columns: [
337
                    {
338
                        name: 'col1',
339
                        id: '1',
340
                        dataIndex: 'col1'
341
                    },
342
                    {
343
                        name: 'col2',
344
                        id: '2',
345
                        dataIndex: 'col2'
346
                    }
347
                ],
348
                column: {
349
                    name: 'col1',
350
                    id: '1',
351
                    dataIndex: 'col1'
352
                },
353
                isHidden: true,
354
                stateKey: 'test-grid'
355
            })
356
        ).toEqual({
357
            stateKey: 'test-grid',
358
            stateful: undefined,
359
            columns: [
360
                {
361
                    name: 'col1',
362
                    id: '1',
363
                    dataIndex: 'col1',
364
                    hidden: false
365
                },
366
                {
367
                    name: 'col2',
368
                    id: '2',
369
                    dataIndex: 'col2'
370
                }
371
            ],
372
            type: 'SET_COLUMNS'
373
        });
374
    });
375
376
    it('Should show column, keeping other column hidden', () => {
377
        expect(
378
            setColumnVisibility({
379
                columns: [
380
                    {
381
                        name: 'col1',
382
                        id: '1',
383
                        dataIndex: 'col1'
384
                    },
385
                    {
386
                        name: 'col2',
387
                        id: '2',
388
                        dataIndex: 'col2',
389
                        hidden: true
390
                    }
391
                ],
392
                column: {
393
                    name: 'col1',
394
                    id: '1',
395
                    dataIndex: 'col1'
396
                },
397
                isHidden: false,
398
                stateKey: 'test-grid'
399
            })
400
        ).toEqual({
401
            stateKey: 'test-grid',
402
            stateful: undefined,
403
            columns: [
404
                {
405
                    name: 'col1',
406
                    id: '1',
407
                    dataIndex: 'col1',
408
                    hidden: true
409
                },
410
                {
411
                    name: 'col2',
412
                    id: '2',
413
                    dataIndex: 'col2',
414
                    hidden: true
415
                }
416
            ],
417
            type: 'SET_COLUMNS'
418
        });
419
    });
420
421
});
422
423
describe('The setData action', () => {
424
425
    expect(
426
        setData({
427
            stateKey: 'test-grid',
428
            data: [1]
429
        })
430
    ).toEqual({
431
        stateKey: 'test-grid',
432
        data: [1],
433
        type: 'SET_DATA'
434
    });
435
436
});
437
438
describe('The resizeColumns action', () => {
439
440
    it('Should resize column when next column is defined', () => {
441
        expect(
442
            resizeColumns({
443
                columns: [
444
                    {
445
                        name: 'col1',
446
                        id: '1',
447
                        dataIndex: 'col1'
448
                    },
449
                    {
450
                        name: 'col2',
451
                        id: '2',
452
                        dataIndex: 'col2'
453
                    }
454
                ],
455
                width: '20',
456
                id: '1',
457
                nextColumn: {
458
                    id: '2',
459
                    width: '22'
460
                },
461
                stateKey: 'test-grid'
462
            })
463
        ).toEqual({
464
            stateKey: 'test-grid',
465
            stateful: undefined,
466
            columns: [
467
                {
468
                    name: 'col1',
469
                    id: '1',
470
                    dataIndex: 'col1',
471
                    width: '20%'
472
                },
473
                {
474
                    name: 'col2',
475
                    id: '2',
476
                    dataIndex: 'col2',
477
                    width: '22%'
478
                }
479
            ],
480
            type: 'RESIZE_COLUMNS'
481
        });
482
    });
483
484
    it('Should resize column when next column is not defined', () => {
485
        expect(
486
            resizeColumns({
487
                columns: [
488
                    {
489
                        name: 'col1',
490
                        id: '1',
491
                        dataIndex: 'col1'
492
                    },
493
                    {
494
                        name: 'col2',
495
                        id: '2',
496
                        dataIndex: 'col2'
497
                    }
498
                ],
499
                width: '20',
500
                id: '1',
501
                nextColumn: {},
502
                stateKey: 'test-grid',
503
                stateful: true
504
            })
505
        ).toEqual({
506
            stateKey: 'test-grid',
507
            stateful: true,
508
            columns: [
509
                {
510
                    name: 'col1',
511
                    id: '1',
512
                    dataIndex: 'col1',
513
                    width: '20%'
514
                },
515
                {
516
                    name: 'col2',
517
                    id: '2',
518
                    dataIndex: 'col2'
519
                }
520
            ],
521
            type: 'RESIZE_COLUMNS'
522
        });
523
    });
524
525
    describe('The setTreeNodeVisibility action', () => {
526
527
        it('Should return default hide action', () => {
528
            expect(setTreeNodeVisibility({
529
                id: 'someId',
530
                type: 'SET_TREE_NODE_VISIBILITY',
531
                stateKey: 'tree-grid',
532
                showTreeRootNode: false
533
            })).toEqual({
534
                id: 'someId',
535
                type: 'SET_TREE_NODE_VISIBILITY',
536
                stateKey: 'tree-grid',
537
                showTreeRootNode: false,
538
                visible: undefined
539
            });
540
        });
541
542
        it('Should pass visiblity state', () => {
543
            expect(setTreeNodeVisibility({
544
                id: 'someId',
545
                type: 'SET_TREE_NODE_VISIBILITY',
546
                stateKey: 'tree-grid',
547
                showTreeRootNode: false,
548
                visible: true
549
            })).toEqual({
550
                id: 'someId',
551
                type: 'SET_TREE_NODE_VISIBILITY',
552
                stateKey: 'tree-grid',
553
                showTreeRootNode: false,
554
                visible: true
555
            });
556
        });
557
558
    });
559
560
    describe('The setTreeData action', () => {
561
562
        const data = {
563
            root: {
564
                id: -1,
565
                children: [
566
                    {
567
                        id: 1,
568
                        parentId: -1,
569
                        children: [
570
                            {
571
                                id: 11,
572
                                parentId: 1
573
                            },
574
                            {
575
                                id: 12,
576
                                parentId: 1,
577
                                children: [
578
                                    {
579
                                        id: 121,
580
                                        parentId: 12,
581
                                        children: [
582
                                            {
583
                                                id: 1211,
584
                                                parentId: 121
585
                                            }
586
                                        ]
587
                                    }
588
                                ]
589
                            }
590
                        ]
591
                    },
592
                    {
593
                        id: 2,
594
                        parentId: -1,
595
                        children: [
596
                            {
597
                                id: 21,
598
                                parentId: 2
599
                            }
600
                        ]
601
                    }
602
                ]
603
            }
604
        };
605
606
        it('Should return action with root node', () => {
607
608
            expect(setTreeData({
609
                data,
610
                stateKey: 'tree-grid',
611
                showTreeRootNode: true
612
            })).toEqual({
613
                type: 'SET_DATA',
614
                data: [
615
                    {
616
                        _depth: 0,
617
                        _hideChildren: undefined,
618
                        _isExpanded: true,
619
                        _hasChildren: true,
620
                        _id: -1,
621
                        _leaf: false,
622
                        _parentId: 'root'
623
                    },
624
                    {
625
                        _depth: 1,
626
                        _hideChildren: undefined,
627
                        _isExpanded: true,
628
                        _hasChildren: true,
629
                        _id: 1,
630
                        _leaf: false, _parentId: -1
631
                    },
632
                    {
633
                        _depth: 2,
634
                        _hideChildren: undefined,
635
                        _hasChildren: undefined,
636
                        _isExpanded: undefined,
637
                        _id: 11,
638
                        _leaf: true,
639
                        _parentId: 1
640
                    },
641
                    {
642
                        _depth: 2,
643
                        _hideChildren: undefined,
644
                        _isExpanded: true,
645
                        _hasChildren: true,
646
                        _id: 12,
647
                        _leaf: false,
648
                        _parentId: 1
649
                    },
650
                    {
651
                        _depth: 3,
652
                        _hideChildren: undefined,
653
                        _isExpanded: true,
654
                        _hasChildren: true,
655
                        _id: 121,
656
                        _leaf: false,
657
                        _parentId: 12
658
                    },
659
                    {
660
                        _depth: 4,
661
                        _hideChildren: undefined,
662
                        _hasChildren: undefined,
663
                        _isExpanded: undefined,
664
                        _id: 1211,
665
                        _leaf: true,
666
                        _parentId: 121
667
                    },
668
                    {
669
                        _depth: 1,
670
                        _hideChildren: undefined,
671
                        _hasChildren: true,
672
                        _isExpanded: true,
673
                        _id: 2,
674
                        _leaf: false,
675
                        _parentId: -1
676
                    },
677
                    {
678
                        _depth: 2,
679
                        _hideChildren: undefined,
680
                        _hasChildren: undefined,
681
                        _isExpanded: undefined,
682
                        _id: 21,
683
                        _leaf: true,
684
                        _parentId: 2
685
                    }
686
                ],
687
                stateKey: 'tree-grid',
688
                gridType: 'tree',
689
                treeData: data
690
            });
691
692
        });
693
694
        it('Should return action without root node', () => {
695
696
            expect(setTreeData({
697
                data,
698
                stateKey: 'tree-grid',
699
                showTreeRootNode: false
700
            })).toEqual({
701
                type: 'SET_DATA',
702
                data: [
703
                    {
704
                        _depth: 1,
705
                        _hideChildren: undefined,
706
                        _hasChildren: true,
707
                        _isExpanded: true,
708
                        _id: 1,
709
                        _leaf: false, _parentId: -1
710
                    },
711
                    {
712
                        _depth: 2,
713
                        _hideChildren: undefined,
714
                        _hasChildren: undefined,
715
                        _isExpanded: undefined,
716
                        _id: 11,
717
                        _leaf: true,
718
                        _parentId: 1
719
                    },
720
                    {
721
                        _depth: 2,
722
                        _hideChildren: undefined,
723
                        _hasChildren: true,
724
                        _isExpanded: true,
725
                        _id: 12,
726
                        _leaf: false,
727
                        _parentId: 1
728
                    },
729
                    {
730
                        _depth: 3,
731
                        _hideChildren: undefined,
732
                        _hasChildren: true,
733
                        _isExpanded: true,
734
                        _id: 121,
735
                        _leaf: false,
736
                        _parentId: 12
737
                    },
738
                    {
739
                        _depth: 4,
740
                        _hideChildren: undefined,
741
                        _hasChildren: undefined,
742
                        _isExpanded: undefined,
743
                        _id: 1211,
744
                        _leaf: true,
745
                        _parentId: 121
746
                    },
747
                    {
748
                        _depth: 1,
749
                        _hideChildren: undefined,
750
                        _hasChildren: true,
751
                        _isExpanded: true,
752
                        _id: 2,
753
                        _leaf: false,
754
                        _parentId: -1
755
                    },
756
                    {
757
                        _depth: 2,
758
                        _hideChildren: undefined,
759
                        _hasChildren: undefined,
760
                        _isExpanded: undefined,
761
                        _id: 21,
762
                        _leaf: true,
763
                        _parentId: 2
764
                    }
765
                ],
766
                stateKey: 'tree-grid',
767
                gridType: 'tree',
768
                treeData: data
769
            });
770
771
        });
772
773
    });
774
775
});
776
777