-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathindex.spec.js
753 lines (685 loc) · 28.2 KB
/
index.spec.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
import { describe, expect, it, beforeAll } from 'vitest'
import { createStore } from 'redux'
import undoable, { ActionCreators, ActionTypes, excludeAction, includeAction, isHistory } from '../src/index'
const decrementActions = ['DECREMENT']
runTests('Default config')
runTests('Never skip reducer', {
undoableConfig: {
neverSkipReducer: true
}
})
runTests('No Init types', {
undoableConfig: {
initTypes: []
},
testConfig: {
checkSlices: true
}
})
runTests('Initial State equals 100', {
undoableConfig: {
limit: 200
},
initialStoreState: 100,
testConfig: {
checkSlices: true
}
})
runTests('Initial State that looks like a history', {
undoableConfig: {},
initialStoreState: { present: 0 },
testConfig: {
checkSlices: true
}
})
runTests('Filter (Include Actions)', {
undoableConfig: {
filter: includeAction(decrementActions)
},
testConfig: {
includeActions: decrementActions
}
})
runTests('Initial History and Filter (Exclude Actions)', {
undoableConfig: {
limit: 100,
initTypes: 'RE-INITIALIZE',
filter: excludeAction(decrementActions)
},
initialStoreState: {
past: [0, 1, 2, 3],
present: 4,
future: [5, 6, 7]
},
testConfig: {
excludedActions: decrementActions,
checkSlices: true
}
})
runTests('Initial State and Init types', {
undoableConfig: {
limit: 1024,
initTypes: 'RE-INITIALIZE'
},
initialStoreState: {
past: [123],
present: 5,
future: [-1, -2, -3]
},
testConfig: {
checkSlices: true
}
})
runTests('Array as clearHistoryType', {
undoableConfig: {
clearHistoryType: ['TYPE_1', 'TYPE_2']
},
testConfig: {
checkSlices: true
}
})
runTests('Erroneous configuration', {
undoableConfig: {
limit: -1,
initTypes: []
},
initialStoreState: {
past: [5, {}, 3, null, 1],
present: Math.pow(2, 32),
future: []
},
testConfig: {
checkSlices: true
}
})
runTests('Get Slices', {
testConfig: {
checkSlices: true
}
})
runTests('Group By', {
undoableConfig: {
groupBy: (action) => action.group || null
},
testConfig: {
checkSlices: true
}
})
// Test undoable reducers as a function of a configuration object
// `label` describes the nature of the configuration object used to run a test
function runTests (label, { undoableConfig = {}, initialStoreState, testConfig } = {}) {
describe('Undoable: ' + label, () => {
let wasCalled = false
const countReducer = (state = 0, action = {}) => {
switch (action.type) {
case ActionTypes.UNDO:
case ActionTypes.REDO:
wasCalled = true
return state
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
}
let mockUndoableReducer
let mockInitialState
let incrementedState
let store
beforeAll(() => {
// undoableConfig.debug = true
mockUndoableReducer = undoable(countReducer, undoableConfig)
store = createStore(mockUndoableReducer, initialStoreState)
mockInitialState = mockUndoableReducer(undefined, {})
incrementedState = mockUndoableReducer(mockInitialState, { type: 'INCREMENT' })
console.info(' Beginning Test! Good luck!')
console.info(' initialStoreState: ', initialStoreState)
console.info(' store.getState(): ', store.getState())
console.info(' mockInitialState: ', mockInitialState)
console.info(' incrementedState: ', incrementedState)
console.info('')
expect(store.getState()).to.deep.equal(mockInitialState, 'mockInitialState should be the same as our store\'s state')
})
describe('Initial state', () => {
it('should be initialized with the value of the default `initialState` of the reducer if there is no `initialState` set on the store', () => {
if (initialStoreState === undefined) {
expect(mockInitialState.present).to.equal(countReducer())
}
})
it('should be initialized with the the store\'s initial `history` if provided', () => {
if (initialStoreState !== undefined && isHistory(initialStoreState)) {
const expected = {
past: mockInitialState.past,
present: mockInitialState.present,
future: mockInitialState.future
}
const actual = {
past: initialStoreState.past,
present: initialStoreState.present,
future: initialStoreState.future
}
expect(expected).to.deep.equal(actual)
}
})
it('should be initialized with the the store\'s initial `state` if provided', () => {
if (initialStoreState !== undefined && !isHistory(initialStoreState)) {
expect(mockInitialState).to.deep.equal({
past: [],
present: initialStoreState,
_latestUnfiltered: initialStoreState,
future: [],
group: null,
index: 0,
limit: 1
})
}
})
})
describe('Replace reducers on the fly', () => {
const tenfoldReducer = (state = 10, action = {}) => {
switch (action.type) {
case 'INCREMENT':
return state + 10
case 'DECREMENT':
return state - 10
default:
return state
}
}
it('should preserve state when reducers are replaced', () => {
store.replaceReducer(undoable(tenfoldReducer, undoableConfig))
expect(store.getState()).to.deep.equal(mockInitialState)
// swap back for other tests
store.replaceReducer(mockUndoableReducer)
expect(store.getState()).to.deep.equal(mockInitialState)
})
it('should use replaced reducer for new actions', () => {
store.replaceReducer(undoable(tenfoldReducer, undoableConfig))
// Increment and check result
let expectedResult = tenfoldReducer(store.getState().present, { type: 'INCREMENT' })
store.dispatch({ type: 'INCREMENT' })
expect(store.getState().present).to.equal(expectedResult)
// swap back for other tests
store.replaceReducer(mockUndoableReducer)
// Increment and check result again
expectedResult = countReducer(store.getState().present, { type: 'INCREMENT' })
store.dispatch({ type: 'INCREMENT' })
expect(store.getState().present).to.equal(expectedResult)
})
})
describe('Actions', () => {
it('should not record unwanted actions', () => {
if (testConfig && testConfig.excludedActions) {
const excludedAction = { type: testConfig.excludedActions[0] }
const includedAction = { type: 'INCREMENT' }
const notFilteredReducer = undoable(countReducer, { ...undoableConfig, filter: null })
let expected = notFilteredReducer(mockInitialState, includedAction)
// should store state with included actions
let actual = mockUndoableReducer(mockInitialState, includedAction)
expect(actual).to.deep.equal(expected)
// but not this one... (keeping the presents caused by filtered actions out of the past)
// (Below, move forward by two filtered actions)
expected = {
...expected,
present: notFilteredReducer(
notFilteredReducer(expected, excludedAction),
excludedAction
).present
}
actual = mockUndoableReducer(
mockUndoableReducer(actual, excludedAction),
excludedAction
)
expect(actual).to.deep.equal(expected)
}
if (testConfig && testConfig.includeActions) {
// should record this action's state in history
const includedAction = { type: testConfig.includeActions[0] }
const excludedAction = { type: 'INCREMENT' }
const commonInitialState = mockUndoableReducer(mockInitialState, includedAction)
const notFilteredReducer = undoable(countReducer, { ...undoableConfig, filter: null })
let expected = notFilteredReducer(commonInitialState, includedAction)
// and this one - should work with included actions just fine
let actual = mockUndoableReducer(commonInitialState, includedAction)
expect(actual).to.deep.equal(expected)
// but not this one... (keeping the presents caused by filtered actions out of the past)
expected = {
...expected,
present: notFilteredReducer(expected, excludedAction).present
}
actual = mockUndoableReducer(actual, excludedAction)
expect(actual).to.deep.equal(expected)
}
})
it('should not record non state changing actions', () => {
const dummyState = mockUndoableReducer(incrementedState, { type: 'DUMMY' })
expect(dummyState).to.deep.equal(incrementedState)
})
it('should synchronize latest unfiltered state to present when filtering actions', () => {
if (testConfig && testConfig.excludedActions) {
const excludedAction = { type: testConfig.excludedActions[0] }
const synchronizedFilteredReducer = undoable(countReducer, {
...undoableConfig,
syncFilter: true
})
const unsynchronized = mockUndoableReducer(mockInitialState, excludedAction)
const synchronized = synchronizedFilteredReducer(mockInitialState, excludedAction)
expect(unsynchronized.present).to.deep.equal(synchronized.present)
expect(unsynchronized._latestUnfiltered).to.not.deep.equal(synchronized._latestUnfiltered)
expect(synchronized.present).to.deep.equal(synchronized._latestUnfiltered)
}
})
it('should not record undefined actions', () => {
const dummyState = mockUndoableReducer(incrementedState, undefined)
expect(dummyState).to.deep.equal(incrementedState)
})
it('should reset upon init actions', () => {
let reInitializedState
if (undoableConfig && undoableConfig.initTypes) {
if (undoableConfig.initTypes.length > 0) {
const initType = Array.isArray(undoableConfig.initTypes) ? undoableConfig.initTypes[0] : undoableConfig.initTypes
reInitializedState = mockUndoableReducer(incrementedState, { type: initType })
expect(reInitializedState).to.deep.equal(mockInitialState)
} else {
// No init actions exist, init should have no effect
reInitializedState = mockUndoableReducer(incrementedState, { type: '@@redux-undo/INIT' })
expect(reInitializedState).to.deep.equal(incrementedState)
}
} else {
reInitializedState = mockUndoableReducer(incrementedState, { type: '@@redux-undo/INIT' })
expect(reInitializedState).to.deep.equal(mockInitialState)
}
})
it('should increment when action is dispatched to store', () => {
const expectedResult = store.getState().present + 1
store.dispatch({ type: 'INCREMENT' })
expect(store.getState().present).to.equal(expectedResult)
})
})
describe('groupBy', () => {
it('should run normally without undo/redo', () => {
if (undoableConfig && undoableConfig.groupBy && !testConfig.excludedActions) {
const first = mockUndoableReducer(mockInitialState, {
type: 'INCREMENT',
group: 'a'
})
expect(first.past.length).to.equal(1)
const second = mockUndoableReducer(first, {
type: 'INCREMENT',
group: 'a'
})
expect(second.past.length).to.equal(first.past.length)
const third = mockUndoableReducer(second, {
type: 'INCREMENT',
group: 'a'
})
expect(third.past.length).to.equal(second.past.length)
expect(third.present).to.equal(mockInitialState.present + 3)
const fourth = mockUndoableReducer(third, {
type: 'DECREMENT',
group: 'b'
})
expect(fourth.past.length).to.equal(2)
const fifth = mockUndoableReducer(fourth, {
type: 'DECREMENT',
group: 'b'
})
expect(fifth.past.length).to.equal(fourth.past.length)
const sixth = mockUndoableReducer(fifth, {
type: 'DECREMENT',
group: 'b'
})
expect(sixth.past.length).to.equal(fifth.past.length)
expect(sixth.present).to.equal(mockInitialState.present)
const seventh = mockUndoableReducer(sixth, {
type: 'INCREMENT'
})
expect(seventh.present).to.equal(first.present)
expect(seventh.past.length).to.equal(3)
const eighth = mockUndoableReducer(seventh, {
type: 'INCREMENT'
})
expect(eighth.past.length).to.equal(4)
}
})
it('should save undo/redo', () => {
if (undoableConfig && undoableConfig.groupBy && !testConfig.excludedActions) {
const first = mockUndoableReducer(mockInitialState, {
type: 'INCREMENT',
group: 'a'
})
expect(first.past.length).to.equal(1)
const second = mockUndoableReducer(first, {
type: 'INCREMENT',
group: 'a'
})
expect(second.past.length).to.equal(first.past.length)
const third = mockUndoableReducer(second, ActionCreators.undo())
expect(third.past.length).to.equal(0)
expect(third.present).to.equal(mockInitialState.present)
const fourth = mockUndoableReducer(third, ActionCreators.redo())
expect(fourth.past.length).to.equal(second.past.length)
expect(fourth.present).to.equal(second.present)
const fifth = mockUndoableReducer(fourth, {
type: 'INCREMENT',
group: 'a'
})
expect(fifth.past.length).to.equal(fourth.past.length + 1)
const sixth = mockUndoableReducer(fifth, {
type: 'DECREMENT',
group: 'b'
})
expect(sixth.past.length).to.equal(fifth.past.length + 1)
const seventh = mockUndoableReducer(sixth, {
type: 'DECREMENT',
group: 'b'
})
expect(seventh.past.length).to.equal(sixth.past.length)
const eighth = mockUndoableReducer(seventh, ActionCreators.undo())
expect(eighth.present).to.equal(fifth.present)
const ninth = mockUndoableReducer(eighth, ActionCreators.undo())
expect(ninth.present).to.equal(fourth.present)
const tenth = mockUndoableReducer(ninth, ActionCreators.undo())
expect(tenth.present).to.equal(mockInitialState.present)
}
})
})
describe('Undo', () => {
let undoState
beforeAll(() => {
wasCalled = false
undoState = mockUndoableReducer(incrementedState, ActionCreators.undo())
})
it('should have called the reducer if neverSkipReducer is true', () => {
expect(wasCalled).to.equal(Boolean(undoableConfig.neverSkipReducer))
})
it('should change present state back by one action', () => {
if (undoableConfig && undoableConfig.limit >= 0) {
expect(undoState.present).to.equal(mockInitialState.present)
}
})
it('should change present state to last element of \'past\'', () => {
if (undoableConfig && undoableConfig.limit >= 0) {
expect(undoState.present).to.equal(incrementedState.past[incrementedState.past.length - 1])
}
})
it('should add a new element to \'future\' from last state', () => {
if (undoableConfig && undoableConfig.limit >= 0) {
expect(undoState.future[0]).to.equal(incrementedState.present)
}
})
it('should decrease length of \'past\' by one', () => {
if (undoableConfig && undoableConfig.limit >= 0) {
expect(undoState.past.length).to.equal(incrementedState.past.length - 1)
}
})
it('should increase length of \'future\' by one', () => {
if (undoableConfig && undoableConfig.limit >= 0) {
expect(undoState.future.length).to.equal(incrementedState.future.length + 1)
}
})
it('should do nothing if \'past\' is empty', () => {
const undoInitialState = mockUndoableReducer(mockInitialState, ActionCreators.undo())
if (!mockInitialState.past.length) {
expect(undoInitialState.present).to.deep.equal(mockInitialState.present)
}
})
it('should undo to last not filtered state', () => {
if (testConfig && testConfig.excludedActions) {
const excludedAction = { type: testConfig.excludedActions[0] }
const includedAction = { type: 'INCREMENT' }
// handle excluded action on a not filtered initial state
let state = mockUndoableReducer(mockInitialState, excludedAction)
// handle excluded action 2
state = mockUndoableReducer(state, excludedAction)
// handle not excluded action
const preUndoState = mockUndoableReducer(state, includedAction)
// undo
state = mockUndoableReducer(preUndoState, ActionCreators.undo())
// should undo to (not filtered) initial present
expect(state.present).to.deep.equal(preUndoState.past[preUndoState.past.length - 1])
}
})
})
describe('Redo', () => {
let undoState
let redoState
beforeAll(() => {
wasCalled = false
undoState = mockUndoableReducer(incrementedState, ActionCreators.undo())
redoState = mockUndoableReducer(undoState, ActionCreators.redo())
})
it('should have called the reducer if neverSkipReducer is true', () => {
expect(wasCalled).to.equal(Boolean(undoableConfig.neverSkipReducer))
})
it('should change present state to equal state before undo', () => {
// skip this test if steps are filtered out,
// because the action might have been was filtered it won't redo to it's state
if (testConfig && !testConfig.includeActions) {
expect(redoState.present).to.equal(incrementedState.present)
}
})
it('should change present state to first element of \'future\'', () => {
if (undoableConfig && undoableConfig.limit >= 0) {
expect(redoState.present).to.equal(undoState.future[0])
}
})
it('should add a new element to \'past\' from last state', () => {
if (undoableConfig && undoableConfig.limit >= 0) {
expect(redoState.past[redoState.past.length - 1]).to.equal(undoState.present)
}
})
it('should decrease length of \'future\' by one', () => {
if (undoableConfig && undoableConfig.limit >= 0) {
expect(redoState.future.length).to.equal(undoState.future.length - 1)
}
})
it('should increase length of \'past\' by one', () => {
if (undoableConfig && undoableConfig.limit >= 0) {
expect(redoState.past.length).to.equal(undoState.past.length + 1)
}
})
it('should do nothing if \'future\' is empty', () => {
const secondRedoState = mockUndoableReducer(redoState, ActionCreators.redo())
if (!redoState.future.length) {
expect(secondRedoState.present).to.deep.equal(redoState.present)
}
})
it('should not redo to filtered state', () => {
if (testConfig && testConfig.excludedActions) {
const excludedAction = { type: testConfig.excludedActions[0] }
// handle excluded action on a not filtered initial state
const excludedState = mockUndoableReducer(mockInitialState, excludedAction)
// undo
const postUndoState = mockUndoableReducer(excludedState, ActionCreators.undo())
// redo
const postRedoState = mockUndoableReducer(postUndoState, ActionCreators.redo())
// redo should be ignored, because future state wasn't stored
expect(mockInitialState).to.deep.equal(postRedoState)
}
})
})
describe('JumpToPast', () => {
const jumpToPastIndex = 0
let jumpToPastState
beforeAll(() => {
jumpToPastState = mockUndoableReducer(incrementedState, ActionCreators.jumpToPast(jumpToPastIndex))
})
it('should change present to a given value from past', () => {
const pastState = incrementedState.past[jumpToPastIndex]
if (pastState !== undefined) {
expect(jumpToPastState.present).to.equal(pastState)
}
})
it('should do nothing if past index is out of bounds', () => {
const jumpToOutOfBounds = mockUndoableReducer(incrementedState, ActionCreators.jumpToPast(-1))
expect(jumpToOutOfBounds).to.deep.equal(incrementedState)
})
it('should increase the length of future if successful', () => {
// skip this test if steps are filtered out,
// because the action might have been was filtered it won't be added to the future
if (testConfig && !testConfig.includeActions) {
if (incrementedState.past.length > jumpToPastIndex) {
expect(jumpToPastState.future.length).to.be.above(incrementedState.future.length)
}
}
})
it('should decrease the length of past if successful', () => {
if (incrementedState.past.length > jumpToPastIndex) {
expect(jumpToPastState.past.length).to.be.below(incrementedState.past.length)
}
})
})
describe('JumpToFuture', () => {
const jumpToFutureIndex = 2
let jumpToFutureState
beforeAll(() => {
jumpToFutureState = mockUndoableReducer(mockInitialState, ActionCreators.jumpToFuture(jumpToFutureIndex))
})
it('should change present to a given value from future', () => {
const futureState = mockInitialState.future[jumpToFutureIndex]
if (futureState !== undefined) {
expect(jumpToFutureState.present).to.equal(futureState)
}
})
it('should do nothing if future index is out of bounds', () => {
const jumpToOutOfBounds = mockUndoableReducer(mockInitialState, ActionCreators.jumpToFuture(-1))
expect(jumpToOutOfBounds).to.deep.equal(mockInitialState)
})
it('should increase the length of past if successful', () => {
if (mockInitialState.future.length > jumpToFutureIndex) {
expect(jumpToFutureState.past.length).to.be.above(mockInitialState.past.length)
}
})
it('should decrease the length of future if successful', () => {
if (mockInitialState.future.length > jumpToFutureIndex) {
expect(jumpToFutureState.future.length).to.be.below(mockInitialState.future.length)
}
})
it('should do a redo if index = 0', () => {
if (mockInitialState.future.length > 0) {
jumpToFutureState = mockUndoableReducer(mockInitialState, ActionCreators.jumpToFuture(0))
const redoState = mockUndoableReducer(mockInitialState, ActionCreators.redo())
expect(redoState).to.deep.equal(jumpToFutureState)
}
})
})
describe('Jump', () => {
const jumpStepsToPast = -2
const jumpStepsToFuture = 2
let jumpToPastState
let jumpToFutureState
let doubleUndoState
let doubleRedoState
beforeAll(() => {
const doubleIncrementedState = mockUndoableReducer(incrementedState, { type: 'INCREMENT' })
jumpToPastState = mockUndoableReducer(doubleIncrementedState, ActionCreators.jump(jumpStepsToPast))
jumpToFutureState = mockUndoableReducer(mockInitialState, ActionCreators.jump(jumpStepsToFuture))
doubleUndoState = mockUndoableReducer(doubleIncrementedState, ActionCreators.undo())
doubleUndoState = mockUndoableReducer(doubleUndoState, ActionCreators.undo())
doubleRedoState = mockUndoableReducer(mockInitialState, ActionCreators.redo())
doubleRedoState = mockUndoableReducer(doubleRedoState, ActionCreators.redo())
})
it('-2 steps should result in same state as two times undo', () => {
// skip this test if steps are filtered out,
// because the double undo would be out of bounds and thus ignored
if (testConfig && !testConfig.includeActions) {
expect(doubleUndoState).to.deep.equal(jumpToPastState)
}
})
it('+2 steps should result in same state as two times redo', () => {
expect(doubleRedoState).to.deep.equal(jumpToFutureState)
})
it('should do nothing if steps is 0', () => {
const jumpToCurrentState = mockUndoableReducer(mockInitialState, ActionCreators.jump(0))
expect(jumpToCurrentState).to.deep.equal(mockInitialState)
})
it('should do nothing if steps is out of bounds', () => {
let jumpToOutOfBounds = mockUndoableReducer(mockInitialState, ActionCreators.jump(10))
expect(jumpToOutOfBounds).to.deep.equal(mockInitialState)
jumpToOutOfBounds = mockUndoableReducer(mockInitialState, ActionCreators.jump(-10))
expect(jumpToOutOfBounds).to.deep.equal(mockInitialState)
})
})
describe('Clear History', () => {
let clearedState
beforeAll(() => {
const clearHistoryType = undoableConfig && undoableConfig.clearHistoryType
const actionType = clearHistoryType && Array.isArray(clearHistoryType) && clearHistoryType.length ? { type: clearHistoryType[0] } : ActionCreators.clearHistory()
clearedState = mockUndoableReducer(incrementedState, actionType)
})
it('should clear future and past', () => {
expect(clearedState.past.length).to.equal(0)
expect(clearedState.future.length).to.equal(0)
})
it('should preserve the present value', () => {
expect(clearedState.present).to.equal(incrementedState.present)
})
})
if (testConfig && testConfig.checkSlices) {
describe('running getSlices', () => {
const initialState = {
normalState: 0,
slice1: 100
}
const sliceReducer = (state, action, slice1) => {
switch (action.type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
case 'COPY_SLICE':
return slice1
default:
return state
}
}
const undoableSliceReducer = undoable(sliceReducer, undoableConfig)
const fullReducer = (state, action) => ({
normalState: undoableSliceReducer(state.normalState, action, state.slice1),
slice1: state.slice1
})
let secondState
let thirdState
let fourthState
let fifthState
let sixthState
let seventhState
beforeAll(() => {
secondState = fullReducer(initialState, { type: 'BOGUS' })
thirdState = fullReducer(secondState, { type: 'INCREMENT' })
fourthState = fullReducer(thirdState, { type: ActionTypes.UNDO })
fifthState = fullReducer(fourthState, { type: ActionTypes.REDO })
sixthState = fullReducer(fifthState, { type: 'COPY_SLICE' })
seventhState = fullReducer(sixthState, { type: 'DECREMENT' })
})
it('should keep same initial state on ignored action', () => {
expect(secondState.normalState.present).to.equal(initialState.normalState)
expect(secondState.slice1).to.equal(initialState.slice1)
})
it('should increment normally', () => {
expect(thirdState.normalState.present).to.equal(initialState.normalState + 1)
expect(thirdState.slice1).to.equal(initialState.slice1)
})
it('should undo normally', () => {
expect(fourthState.normalState.present).to.equal(secondState.normalState.present)
expect(fourthState.slice1).to.equal(initialState.slice1)
})
it('should redo normally', () => {
expect(fifthState.normalState.present).to.equal(thirdState.normalState.present)
expect(fifthState.slice1).to.equal(initialState.slice1)
})
it('should referenced sliced state normally', () => {
expect(sixthState.normalState.present).to.equal(sixthState.slice1)
expect(sixthState.slice1).to.equal(initialState.slice1)
})
it('should work normally after referencing slices', () => {
expect(seventhState.normalState.present).to.equal(sixthState.normalState.present - 1)
expect(seventhState.slice1).to.equal(initialState.slice1)
})
})
}
})
}