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
Branch master (5d2753)
by Benjamin
17:29 queued 13:40
created

test/reducers/components/plugins/editor.test.js   B

Complexity

Total Complexity 51
Complexity/F 1.04

Size

Lines of Code 700
Function Count 49

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 0
wmc 51
c 3
b 0
f 1
nc 2
mnd 1
bc 47
fnc 49
dl 0
loc 700
rs 7.4418
bpm 0.9591
cpm 1.0408
noi 4

How to fix   Complexity   

Complexity

Complex classes like test/reducers/components/plugins/editor.test.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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