Skip to content

Commit 449b73f

Browse files
committed
clean up currentStateIndex tests
1 parent 913c10f commit 449b73f

File tree

1 file changed

+62
-30
lines changed

1 file changed

+62
-30
lines changed

test/instrument.spec.js

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ describe('instrument', () => {
359359
spy.restore();
360360
});
361361

362-
it('should auto-commit after hot reload', () => {
362+
it('should auto-commit actions after hot reload fixes error', () => {
363363
let spy = spyOn(console, 'error');
364364

365365
let storeWithBug = createStore(counterWithBug, instrument(undefined, { maxAge: 3 }));
@@ -385,50 +385,82 @@ describe('instrument', () => {
385385

386386
it('should update currentStateIndex when auto-committing', () => {
387387
let spy = spyOn(console, 'error');
388+
let liftedStoreState, currentComputedState;
388389

389390
configuredStore.dispatch({ type: 'INCREMENT' });
390391
configuredStore.dispatch({ type: 'INCREMENT' });
391-
392-
let liftedStoreState = configuredLiftedStore.getState();
393-
expect(liftedStoreState.currentStateIndex).toBe(2);
394-
395-
configuredStore.dispatch({ type: 'INCREMENT' });
396-
397392
liftedStoreState = configuredLiftedStore.getState();
398-
let currentComputedState = liftedStoreState.computedStates[liftedStoreState.currentStateIndex];
399-
// currentStateIndex stays at 2 when an action is committed
400393
expect(liftedStoreState.currentStateIndex).toBe(2);
401-
expect(currentComputedState.state).toBe(3);
402394

403-
configuredStore.replaceReducer(counterWithBug);
404-
configuredStore.dispatch({ type: 'DECREMENT' });
405-
configuredStore.dispatch({ type: 'DECREMENT' });
406-
configuredStore.dispatch({ type: 'DECREMENT' });
407-
configuredStore.dispatch({ type: 'INCREMENT' });
395+
// currentStateIndex should stay at 2 as actions are committed
408396
configuredStore.dispatch({ type: 'INCREMENT' });
409397
liftedStoreState = configuredLiftedStore.getState();
410398
currentComputedState = liftedStoreState.computedStates[liftedStoreState.currentStateIndex];
411-
// currentStateIndex continues to increment while non-committed action causes error
412-
expect(liftedStoreState.currentStateIndex).toBe(5);
399+
expect(liftedStoreState.currentStateIndex).toBe(2);
413400
expect(currentComputedState.state).toBe(3);
414-
expect(currentComputedState.error).toExist();
415401

416-
configuredStore.replaceReducer(counterWithAnotherBug);
417-
liftedStoreState = configuredLiftedStore.getState();
418-
currentComputedState = liftedStoreState.computedStates[liftedStoreState.currentStateIndex];
419-
// currentStateIndex adjusts accordingly when multiple actions are committed
420-
expect(liftedStoreState.currentStateIndex).toBe(2);
402+
spy.restore();
403+
});
404+
405+
it('should continue to increment currentStateIndex while error blocks commit', () => {
406+
let spy = spyOn(console, 'error');
407+
408+
let storeWithBug = createStore(counterWithBug, instrument(undefined, { maxAge: 3 }));
409+
let liftedStoreWithBug = storeWithBug.liftedStore;
410+
411+
storeWithBug.dispatch({ type: 'DECREMENT' });
412+
storeWithBug.dispatch({ type: 'DECREMENT' });
413+
storeWithBug.dispatch({ type: 'DECREMENT' });
414+
storeWithBug.dispatch({ type: 'DECREMENT' });
415+
416+
let liftedStoreState = liftedStoreWithBug.getState();
417+
let currentComputedState = liftedStoreState.computedStates[liftedStoreState.currentStateIndex];
418+
expect(liftedStoreState.currentStateIndex).toBe(4);
421419
expect(currentComputedState.state).toBe(0);
422420
expect(currentComputedState.error).toExist();
423421

424-
configuredLiftedStore.dispatch(ActionCreators.jumpToState(0));
425-
configuredStore.replaceReducer(counter);
426-
liftedStoreState = configuredLiftedStore.getState();
427-
// currentStateIndex doesn't go below 0
428-
currentComputedState = liftedStoreState.computedStates[liftedStoreState.currentStateIndex];
422+
spy.restore();
423+
});
424+
425+
it('should adjust currentStateIndex correctly when multiple actions are committed', () => {
426+
let spy = spyOn(console, 'error');
427+
428+
let storeWithBug = createStore(counterWithBug, instrument(undefined, { maxAge: 3 }));
429+
let liftedStoreWithBug = storeWithBug.liftedStore;
430+
431+
storeWithBug.dispatch({ type: 'DECREMENT' });
432+
storeWithBug.dispatch({ type: 'DECREMENT' });
433+
storeWithBug.dispatch({ type: 'DECREMENT' });
434+
storeWithBug.dispatch({ type: 'DECREMENT' });
435+
436+
// Auto-commit 2 actions by "fixing" reducer bug.
437+
storeWithBug.replaceReducer(counter);
438+
let liftedStoreState = liftedStoreWithBug.getState();
439+
let currentComputedState = liftedStoreState.computedStates[liftedStoreState.currentStateIndex];
440+
expect(liftedStoreState.currentStateIndex).toBe(2);
441+
expect(currentComputedState.state).toBe(-4);
442+
443+
spy.restore();
444+
});
445+
446+
it('should not allow currentStateIndex to drop below 0', () => {
447+
let spy = spyOn(console, 'error');
448+
449+
let storeWithBug = createStore(counterWithBug, instrument(undefined, { maxAge: 3 }));
450+
let liftedStoreWithBug = storeWithBug.liftedStore;
451+
452+
storeWithBug.dispatch({ type: 'DECREMENT' });
453+
storeWithBug.dispatch({ type: 'DECREMENT' });
454+
storeWithBug.dispatch({ type: 'DECREMENT' });
455+
storeWithBug.dispatch({ type: 'DECREMENT' });
456+
liftedStoreWithBug.dispatch(ActionCreators.jumpToState(1));
457+
458+
// Auto-commit 2 actions by "fixing" reducer bug.
459+
storeWithBug.replaceReducer(counter);
460+
let liftedStoreState = liftedStoreWithBug.getState();
461+
let currentComputedState = liftedStoreState.computedStates[liftedStoreState.currentStateIndex];
429462
expect(liftedStoreState.currentStateIndex).toBe(0);
430-
expect(currentComputedState.state).toBe(0);
431-
expect(currentComputedState.error).toNotExist();
463+
expect(currentComputedState.state).toBe(-2);
432464

433465
spy.restore();
434466
});

0 commit comments

Comments
 (0)