2015-09-19 23:05:05 +03:00
|
|
|
import expect, { spyOn } from 'expect';
|
2016-02-02 19:07:58 +03:00
|
|
|
import { createStore, compose } from 'redux';
|
2015-09-28 18:38:33 +03:00
|
|
|
import instrument, { ActionCreators } from '../src/instrument';
|
2015-09-19 23:05:05 +03:00
|
|
|
|
|
|
|
function counter(state = 0, action) {
|
|
|
|
switch (action.type) {
|
|
|
|
case 'INCREMENT': return state + 1;
|
|
|
|
case 'DECREMENT': return state - 1;
|
|
|
|
default: return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function counterWithBug(state = 0, action) {
|
|
|
|
switch (action.type) {
|
|
|
|
case 'INCREMENT': return state + 1;
|
|
|
|
case 'DECREMENT': return mistake - 1; // eslint-disable-line no-undef
|
2015-09-24 21:18:58 +03:00
|
|
|
case 'SET_UNDEFINED': return undefined;
|
2015-09-19 23:05:05 +03:00
|
|
|
default: return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-10 04:39:58 +03:00
|
|
|
function counterWithAnotherBug(state = 0, action) {
|
|
|
|
switch (action.type) {
|
|
|
|
case 'INCREMENT': return mistake + 1; // eslint-disable-line no-undef
|
|
|
|
case 'DECREMENT': return state - 1;
|
|
|
|
case 'SET_UNDEFINED': return undefined;
|
|
|
|
default: return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-19 23:05:05 +03:00
|
|
|
function doubleCounter(state = 0, action) {
|
|
|
|
switch (action.type) {
|
|
|
|
case 'INCREMENT': return state + 2;
|
|
|
|
case 'DECREMENT': return state - 2;
|
|
|
|
default: return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-28 18:38:33 +03:00
|
|
|
describe('instrument', () => {
|
2015-09-19 23:05:05 +03:00
|
|
|
let store;
|
2015-10-17 02:21:07 +03:00
|
|
|
let liftedStore;
|
2015-09-19 23:05:05 +03:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2016-02-02 20:52:24 +03:00
|
|
|
store = createStore(counter, instrument());
|
2015-10-17 02:21:07 +03:00
|
|
|
liftedStore = store.liftedStore;
|
2015-09-19 23:05:05 +03:00
|
|
|
});
|
|
|
|
|
2015-09-23 22:43:55 +03:00
|
|
|
it('should perform actions', () => {
|
2015-09-19 23:05:05 +03:00
|
|
|
expect(store.getState()).toBe(0);
|
|
|
|
store.dispatch({ type: 'INCREMENT' });
|
|
|
|
expect(store.getState()).toBe(1);
|
|
|
|
store.dispatch({ type: 'INCREMENT' });
|
|
|
|
expect(store.getState()).toBe(2);
|
|
|
|
});
|
|
|
|
|
2015-09-23 22:43:55 +03:00
|
|
|
it('should rollback state to the last committed state', () => {
|
2015-09-19 23:05:05 +03:00
|
|
|
store.dispatch({ type: 'INCREMENT' });
|
|
|
|
store.dispatch({ type: 'INCREMENT' });
|
|
|
|
expect(store.getState()).toBe(2);
|
|
|
|
|
2015-10-17 02:21:07 +03:00
|
|
|
liftedStore.dispatch(ActionCreators.commit());
|
2015-09-19 23:05:05 +03:00
|
|
|
expect(store.getState()).toBe(2);
|
|
|
|
|
|
|
|
store.dispatch({ type: 'INCREMENT' });
|
|
|
|
store.dispatch({ type: 'INCREMENT' });
|
|
|
|
expect(store.getState()).toBe(4);
|
|
|
|
|
2015-10-17 02:21:07 +03:00
|
|
|
liftedStore.dispatch(ActionCreators.rollback());
|
2015-09-19 23:05:05 +03:00
|
|
|
expect(store.getState()).toBe(2);
|
|
|
|
|
|
|
|
store.dispatch({ type: 'DECREMENT' });
|
|
|
|
expect(store.getState()).toBe(1);
|
|
|
|
|
2015-10-17 02:21:07 +03:00
|
|
|
liftedStore.dispatch(ActionCreators.rollback());
|
2015-09-19 23:05:05 +03:00
|
|
|
expect(store.getState()).toBe(2);
|
|
|
|
});
|
|
|
|
|
2015-09-23 22:43:55 +03:00
|
|
|
it('should reset to initial state', () => {
|
2015-09-19 23:05:05 +03:00
|
|
|
store.dispatch({ type: 'INCREMENT' });
|
|
|
|
expect(store.getState()).toBe(1);
|
|
|
|
|
2015-10-17 02:21:07 +03:00
|
|
|
liftedStore.dispatch(ActionCreators.commit());
|
2015-09-19 23:05:05 +03:00
|
|
|
expect(store.getState()).toBe(1);
|
|
|
|
|
|
|
|
store.dispatch({ type: 'INCREMENT' });
|
|
|
|
expect(store.getState()).toBe(2);
|
|
|
|
|
2015-10-17 02:21:07 +03:00
|
|
|
liftedStore.dispatch(ActionCreators.rollback());
|
2015-09-19 23:05:05 +03:00
|
|
|
expect(store.getState()).toBe(1);
|
|
|
|
|
|
|
|
store.dispatch({ type: 'INCREMENT' });
|
|
|
|
expect(store.getState()).toBe(2);
|
|
|
|
|
2015-10-17 02:21:07 +03:00
|
|
|
liftedStore.dispatch(ActionCreators.reset());
|
2015-09-19 23:05:05 +03:00
|
|
|
expect(store.getState()).toBe(0);
|
|
|
|
});
|
|
|
|
|
2015-09-23 22:43:55 +03:00
|
|
|
it('should toggle an action', () => {
|
2015-10-17 03:53:43 +03:00
|
|
|
// actionId 0 = @@INIT
|
2015-09-19 23:05:05 +03:00
|
|
|
store.dispatch({ type: 'INCREMENT' });
|
|
|
|
store.dispatch({ type: 'DECREMENT' });
|
|
|
|
store.dispatch({ type: 'INCREMENT' });
|
|
|
|
expect(store.getState()).toBe(1);
|
|
|
|
|
2015-10-17 02:21:07 +03:00
|
|
|
liftedStore.dispatch(ActionCreators.toggleAction(2));
|
2015-09-19 23:05:05 +03:00
|
|
|
expect(store.getState()).toBe(2);
|
|
|
|
|
2015-10-17 02:21:07 +03:00
|
|
|
liftedStore.dispatch(ActionCreators.toggleAction(2));
|
2015-09-19 23:05:05 +03:00
|
|
|
expect(store.getState()).toBe(1);
|
|
|
|
});
|
|
|
|
|
2015-09-24 20:11:11 +03:00
|
|
|
it('should sweep disabled actions', () => {
|
2015-10-17 03:53:43 +03:00
|
|
|
// actionId 0 = @@INIT
|
2015-09-19 23:05:05 +03:00
|
|
|
store.dispatch({ type: 'INCREMENT' });
|
|
|
|
store.dispatch({ type: 'DECREMENT' });
|
|
|
|
store.dispatch({ type: 'INCREMENT' });
|
|
|
|
store.dispatch({ type: 'INCREMENT' });
|
2015-10-17 15:44:30 +03:00
|
|
|
|
2015-09-19 23:05:05 +03:00
|
|
|
expect(store.getState()).toBe(2);
|
2015-10-17 15:44:30 +03:00
|
|
|
expect(liftedStore.getState().stagedActionIds).toEqual([0, 1, 2, 3, 4]);
|
|
|
|
expect(liftedStore.getState().skippedActionIds).toEqual([]);
|
2015-09-19 23:05:05 +03:00
|
|
|
|
2015-10-17 02:21:07 +03:00
|
|
|
liftedStore.dispatch(ActionCreators.toggleAction(2));
|
2015-09-19 23:05:05 +03:00
|
|
|
expect(store.getState()).toBe(3);
|
2015-10-17 15:44:30 +03:00
|
|
|
expect(liftedStore.getState().stagedActionIds).toEqual([0, 1, 2, 3, 4]);
|
|
|
|
expect(liftedStore.getState().skippedActionIds).toEqual([2]);
|
2015-09-19 23:05:05 +03:00
|
|
|
|
2015-10-17 02:21:07 +03:00
|
|
|
liftedStore.dispatch(ActionCreators.sweep());
|
2015-09-19 23:05:05 +03:00
|
|
|
expect(store.getState()).toBe(3);
|
2015-10-17 15:44:30 +03:00
|
|
|
expect(liftedStore.getState().stagedActionIds).toEqual([0, 1, 3, 4]);
|
|
|
|
expect(liftedStore.getState().skippedActionIds).toEqual([]);
|
2015-09-19 23:05:05 +03:00
|
|
|
});
|
|
|
|
|
2015-09-23 22:43:55 +03:00
|
|
|
it('should jump to state', () => {
|
2015-09-19 23:05:05 +03:00
|
|
|
store.dispatch({ type: 'INCREMENT' });
|
|
|
|
store.dispatch({ type: 'DECREMENT' });
|
|
|
|
store.dispatch({ type: 'INCREMENT' });
|
|
|
|
expect(store.getState()).toBe(1);
|
|
|
|
|
2015-10-17 02:21:07 +03:00
|
|
|
liftedStore.dispatch(ActionCreators.jumpToState(0));
|
2015-09-19 23:05:05 +03:00
|
|
|
expect(store.getState()).toBe(0);
|
|
|
|
|
2015-10-17 02:21:07 +03:00
|
|
|
liftedStore.dispatch(ActionCreators.jumpToState(1));
|
2015-09-19 23:05:05 +03:00
|
|
|
expect(store.getState()).toBe(1);
|
|
|
|
|
2015-10-17 02:21:07 +03:00
|
|
|
liftedStore.dispatch(ActionCreators.jumpToState(2));
|
2015-09-19 23:05:05 +03:00
|
|
|
expect(store.getState()).toBe(0);
|
|
|
|
|
|
|
|
store.dispatch({ type: 'INCREMENT' });
|
|
|
|
expect(store.getState()).toBe(0);
|
|
|
|
|
2015-10-17 02:21:07 +03:00
|
|
|
liftedStore.dispatch(ActionCreators.jumpToState(4));
|
2015-09-19 23:05:05 +03:00
|
|
|
expect(store.getState()).toBe(2);
|
|
|
|
});
|
|
|
|
|
2015-09-23 22:43:55 +03:00
|
|
|
it('should replace the reducer', () => {
|
2015-09-19 23:05:05 +03:00
|
|
|
store.dispatch({ type: 'INCREMENT' });
|
|
|
|
store.dispatch({ type: 'DECREMENT' });
|
|
|
|
store.dispatch({ type: 'INCREMENT' });
|
|
|
|
expect(store.getState()).toBe(1);
|
|
|
|
|
|
|
|
store.replaceReducer(doubleCounter);
|
|
|
|
expect(store.getState()).toBe(2);
|
|
|
|
});
|
|
|
|
|
2015-09-23 22:43:55 +03:00
|
|
|
it('should catch and record errors', () => {
|
2015-09-19 23:05:05 +03:00
|
|
|
let spy = spyOn(console, 'error');
|
2016-02-02 20:52:24 +03:00
|
|
|
let storeWithBug = createStore(counterWithBug, instrument());
|
2015-09-19 23:05:05 +03:00
|
|
|
|
|
|
|
storeWithBug.dispatch({ type: 'INCREMENT' });
|
|
|
|
storeWithBug.dispatch({ type: 'DECREMENT' });
|
|
|
|
storeWithBug.dispatch({ type: 'INCREMENT' });
|
|
|
|
|
2015-10-17 02:21:07 +03:00
|
|
|
let { computedStates } = storeWithBug.liftedStore.getState();
|
|
|
|
expect(computedStates[2].error).toMatch(
|
2015-09-19 23:05:05 +03:00
|
|
|
/ReferenceError/
|
|
|
|
);
|
2015-10-17 02:21:07 +03:00
|
|
|
expect(computedStates[3].error).toMatch(
|
2015-09-19 23:05:05 +03:00
|
|
|
/Interrupted by an error up the chain/
|
|
|
|
);
|
|
|
|
expect(spy.calls[0].arguments[0]).toMatch(
|
|
|
|
/ReferenceError/
|
|
|
|
);
|
2015-09-20 13:49:37 +03:00
|
|
|
|
|
|
|
spy.restore();
|
|
|
|
});
|
|
|
|
|
2016-01-25 21:40:54 +03:00
|
|
|
it('should catch invalid action type', () => {
|
|
|
|
expect(() => {
|
|
|
|
store.dispatch({ type: undefined });
|
|
|
|
}).toThrow(
|
2016-02-02 20:52:24 +03:00
|
|
|
'Actions may not have an undefined "type" property. ' +
|
|
|
|
'Have you misspelled a constant?'
|
2016-01-25 21:40:54 +03:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-09-24 21:18:58 +03:00
|
|
|
it('should return the last non-undefined state from getState', () => {
|
2016-02-02 20:52:24 +03:00
|
|
|
let storeWithBug = createStore(counterWithBug, instrument());
|
2015-09-24 21:18:58 +03:00
|
|
|
storeWithBug.dispatch({ type: 'INCREMENT' });
|
|
|
|
storeWithBug.dispatch({ type: 'INCREMENT' });
|
|
|
|
expect(storeWithBug.getState()).toBe(2);
|
2015-09-20 13:49:37 +03:00
|
|
|
|
2015-09-24 21:18:58 +03:00
|
|
|
storeWithBug.dispatch({ type: 'SET_UNDEFINED' });
|
|
|
|
expect(storeWithBug.getState()).toBe(2);
|
2015-09-19 23:05:05 +03:00
|
|
|
});
|
2015-09-27 20:13:38 +03:00
|
|
|
|
|
|
|
it('should not recompute states on every action', () => {
|
|
|
|
let reducerCalls = 0;
|
2016-02-02 20:52:24 +03:00
|
|
|
let monitoredStore = createStore(() => reducerCalls++, instrument());
|
2015-09-27 20:13:38 +03:00
|
|
|
expect(reducerCalls).toBe(1);
|
|
|
|
monitoredStore.dispatch({ type: 'INCREMENT' });
|
|
|
|
monitoredStore.dispatch({ type: 'INCREMENT' });
|
|
|
|
monitoredStore.dispatch({ type: 'INCREMENT' });
|
|
|
|
expect(reducerCalls).toBe(4);
|
|
|
|
});
|
|
|
|
|
2015-10-17 15:44:30 +03:00
|
|
|
it('should not recompute old states when toggling an action', () => {
|
|
|
|
let reducerCalls = 0;
|
2016-02-02 20:52:24 +03:00
|
|
|
let monitoredStore = createStore(() => reducerCalls++, instrument());
|
2015-10-17 15:44:30 +03:00
|
|
|
let monitoredLiftedStore = monitoredStore.liftedStore;
|
|
|
|
|
|
|
|
expect(reducerCalls).toBe(1);
|
|
|
|
// actionId 0 = @@INIT
|
|
|
|
monitoredStore.dispatch({ type: 'INCREMENT' });
|
|
|
|
monitoredStore.dispatch({ type: 'INCREMENT' });
|
|
|
|
monitoredStore.dispatch({ type: 'INCREMENT' });
|
|
|
|
expect(reducerCalls).toBe(4);
|
|
|
|
|
|
|
|
monitoredLiftedStore.dispatch(ActionCreators.toggleAction(3));
|
|
|
|
expect(reducerCalls).toBe(4);
|
|
|
|
|
|
|
|
monitoredLiftedStore.dispatch(ActionCreators.toggleAction(3));
|
|
|
|
expect(reducerCalls).toBe(5);
|
|
|
|
|
|
|
|
monitoredLiftedStore.dispatch(ActionCreators.toggleAction(2));
|
|
|
|
expect(reducerCalls).toBe(6);
|
|
|
|
|
|
|
|
monitoredLiftedStore.dispatch(ActionCreators.toggleAction(2));
|
|
|
|
expect(reducerCalls).toBe(8);
|
|
|
|
|
|
|
|
monitoredLiftedStore.dispatch(ActionCreators.toggleAction(1));
|
|
|
|
expect(reducerCalls).toBe(10);
|
|
|
|
|
|
|
|
monitoredLiftedStore.dispatch(ActionCreators.toggleAction(2));
|
|
|
|
expect(reducerCalls).toBe(11);
|
|
|
|
|
|
|
|
monitoredLiftedStore.dispatch(ActionCreators.toggleAction(3));
|
|
|
|
expect(reducerCalls).toBe(11);
|
|
|
|
|
|
|
|
monitoredLiftedStore.dispatch(ActionCreators.toggleAction(1));
|
|
|
|
expect(reducerCalls).toBe(12);
|
|
|
|
|
|
|
|
monitoredLiftedStore.dispatch(ActionCreators.toggleAction(3));
|
|
|
|
expect(reducerCalls).toBe(13);
|
|
|
|
|
|
|
|
monitoredLiftedStore.dispatch(ActionCreators.toggleAction(2));
|
|
|
|
expect(reducerCalls).toBe(15);
|
|
|
|
});
|
|
|
|
|
2015-09-27 20:13:38 +03:00
|
|
|
it('should not recompute states when jumping to state', () => {
|
|
|
|
let reducerCalls = 0;
|
2016-02-02 20:52:24 +03:00
|
|
|
let monitoredStore = createStore(() => reducerCalls++, instrument());
|
2015-10-17 15:44:30 +03:00
|
|
|
let monitoredLiftedStore = monitoredStore.liftedStore;
|
2015-09-27 20:13:38 +03:00
|
|
|
|
|
|
|
expect(reducerCalls).toBe(1);
|
|
|
|
monitoredStore.dispatch({ type: 'INCREMENT' });
|
|
|
|
monitoredStore.dispatch({ type: 'INCREMENT' });
|
|
|
|
monitoredStore.dispatch({ type: 'INCREMENT' });
|
|
|
|
expect(reducerCalls).toBe(4);
|
|
|
|
|
2015-10-17 16:00:20 +03:00
|
|
|
let savedComputedStates = monitoredLiftedStore.getState().computedStates;
|
|
|
|
|
2015-10-17 15:44:30 +03:00
|
|
|
monitoredLiftedStore.dispatch(ActionCreators.jumpToState(0));
|
|
|
|
expect(reducerCalls).toBe(4);
|
|
|
|
|
|
|
|
monitoredLiftedStore.dispatch(ActionCreators.jumpToState(1));
|
|
|
|
expect(reducerCalls).toBe(4);
|
|
|
|
|
|
|
|
monitoredLiftedStore.dispatch(ActionCreators.jumpToState(3));
|
|
|
|
expect(reducerCalls).toBe(4);
|
2015-10-17 16:00:20 +03:00
|
|
|
|
|
|
|
expect(monitoredLiftedStore.getState().computedStates).toBe(savedComputedStates);
|
2015-10-17 15:44:30 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not recompute states on monitor actions', () => {
|
|
|
|
let reducerCalls = 0;
|
2016-02-02 20:52:24 +03:00
|
|
|
let monitoredStore = createStore(() => reducerCalls++, instrument());
|
2015-10-17 15:44:30 +03:00
|
|
|
let monitoredLiftedStore = monitoredStore.liftedStore;
|
|
|
|
|
|
|
|
expect(reducerCalls).toBe(1);
|
|
|
|
monitoredStore.dispatch({ type: 'INCREMENT' });
|
|
|
|
monitoredStore.dispatch({ type: 'INCREMENT' });
|
|
|
|
monitoredStore.dispatch({ type: 'INCREMENT' });
|
2015-09-27 20:13:38 +03:00
|
|
|
expect(reducerCalls).toBe(4);
|
|
|
|
|
2015-10-17 16:00:20 +03:00
|
|
|
let savedComputedStates = monitoredLiftedStore.getState().computedStates;
|
|
|
|
|
2015-10-17 15:44:30 +03:00
|
|
|
monitoredLiftedStore.dispatch({ type: 'lol' });
|
2015-09-27 20:13:38 +03:00
|
|
|
expect(reducerCalls).toBe(4);
|
|
|
|
|
2015-10-17 15:44:30 +03:00
|
|
|
monitoredLiftedStore.dispatch({ type: 'wat' });
|
2015-09-27 20:13:38 +03:00
|
|
|
expect(reducerCalls).toBe(4);
|
2015-10-17 16:00:20 +03:00
|
|
|
|
|
|
|
expect(monitoredLiftedStore.getState().computedStates).toBe(savedComputedStates);
|
2015-09-27 20:13:38 +03:00
|
|
|
});
|
2015-10-17 02:34:46 +03:00
|
|
|
|
2016-02-04 22:41:45 +03:00
|
|
|
describe('maxAge option', () => {
|
|
|
|
let configuredStore;
|
|
|
|
let configuredLiftedStore;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2016-02-05 19:26:23 +03:00
|
|
|
configuredStore = createStore(counter, instrument(undefined, { maxAge: 3 }));
|
2016-02-04 22:41:45 +03:00
|
|
|
configuredLiftedStore = configuredStore.liftedStore;
|
|
|
|
});
|
|
|
|
|
2016-02-05 19:26:23 +03:00
|
|
|
it('should auto-commit earliest non-@@INIT action when maxAge is reached', () => {
|
|
|
|
configuredStore.dispatch({ type: 'INCREMENT' });
|
2016-02-04 22:41:45 +03:00
|
|
|
configuredStore.dispatch({ type: 'INCREMENT' });
|
|
|
|
let liftedStoreState = configuredLiftedStore.getState();
|
|
|
|
|
2016-02-05 19:26:23 +03:00
|
|
|
expect(configuredStore.getState()).toBe(2);
|
|
|
|
expect(Object.keys(liftedStoreState.actionsById).length).toBe(3);
|
2016-02-04 22:41:45 +03:00
|
|
|
expect(liftedStoreState.committedState).toBe(undefined);
|
2016-02-05 19:26:23 +03:00
|
|
|
expect(liftedStoreState.stagedActionIds).toInclude(1);
|
2016-02-04 22:41:45 +03:00
|
|
|
|
2016-02-05 19:26:23 +03:00
|
|
|
// Triggers auto-commit.
|
2016-02-04 22:41:45 +03:00
|
|
|
configuredStore.dispatch({ type: 'INCREMENT' });
|
|
|
|
liftedStoreState = configuredLiftedStore.getState();
|
|
|
|
|
2016-02-05 19:26:23 +03:00
|
|
|
expect(configuredStore.getState()).toBe(3);
|
|
|
|
expect(Object.keys(liftedStoreState.actionsById).length).toBe(3);
|
|
|
|
expect(liftedStoreState.stagedActionIds).toExclude(1);
|
2016-02-04 22:41:45 +03:00
|
|
|
expect(liftedStoreState.computedStates[0].state).toBe(1);
|
|
|
|
expect(liftedStoreState.committedState).toBe(1);
|
2016-02-05 19:26:23 +03:00
|
|
|
expect(liftedStoreState.currentStateIndex).toBe(2);
|
2016-02-04 22:41:45 +03:00
|
|
|
});
|
|
|
|
|
2016-02-10 20:36:35 +03:00
|
|
|
it('should remove skipped actions once committed', () => {
|
2016-02-04 22:41:45 +03:00
|
|
|
configuredStore.dispatch({ type: 'INCREMENT' });
|
|
|
|
configuredLiftedStore.dispatch(ActionCreators.toggleAction(1));
|
|
|
|
configuredStore.dispatch({ type: 'INCREMENT' });
|
|
|
|
expect(configuredLiftedStore.getState().skippedActionIds).toInclude(1);
|
|
|
|
configuredStore.dispatch({ type: 'INCREMENT' });
|
|
|
|
expect(configuredLiftedStore.getState().skippedActionIds).toExclude(1);
|
|
|
|
});
|
2016-02-10 04:39:58 +03:00
|
|
|
|
|
|
|
it('should not auto-commit errors', () => {
|
|
|
|
let spy = spyOn(console, 'error');
|
|
|
|
|
|
|
|
let storeWithBug = createStore(counterWithBug, instrument(undefined, { maxAge: 3 }));
|
|
|
|
let liftedStoreWithBug = storeWithBug.liftedStore;
|
|
|
|
storeWithBug.dispatch({ type: 'DECREMENT' });
|
|
|
|
storeWithBug.dispatch({ type: 'INCREMENT' });
|
|
|
|
expect(liftedStoreWithBug.getState().stagedActionIds.length).toBe(3);
|
|
|
|
|
|
|
|
storeWithBug.dispatch({ type: 'INCREMENT' });
|
|
|
|
expect(liftedStoreWithBug.getState().stagedActionIds.length).toBe(4);
|
|
|
|
|
|
|
|
spy.restore();
|
|
|
|
});
|
|
|
|
|
2016-02-13 02:27:55 +03:00
|
|
|
it('should auto-commit actions after hot reload fixes error', () => {
|
2016-02-10 04:39:58 +03:00
|
|
|
let spy = spyOn(console, 'error');
|
|
|
|
|
|
|
|
let storeWithBug = createStore(counterWithBug, instrument(undefined, { maxAge: 3 }));
|
|
|
|
let liftedStoreWithBug = storeWithBug.liftedStore;
|
|
|
|
storeWithBug.dispatch({ type: 'DECREMENT' });
|
|
|
|
storeWithBug.dispatch({ type: 'DECREMENT' });
|
|
|
|
storeWithBug.dispatch({ type: 'INCREMENT' });
|
|
|
|
storeWithBug.dispatch({ type: 'DECREMENT' });
|
|
|
|
storeWithBug.dispatch({ type: 'DECREMENT' });
|
|
|
|
storeWithBug.dispatch({ type: 'DECREMENT' });
|
|
|
|
expect(liftedStoreWithBug.getState().stagedActionIds.length).toBe(7);
|
|
|
|
|
|
|
|
// should auto-commit only 2 non-error actions
|
|
|
|
storeWithBug.replaceReducer(counterWithAnotherBug);
|
|
|
|
expect(liftedStoreWithBug.getState().stagedActionIds.length).toBe(5);
|
|
|
|
|
|
|
|
// should auto-commit down to 3 actions
|
|
|
|
storeWithBug.replaceReducer(counter);
|
|
|
|
expect(liftedStoreWithBug.getState().stagedActionIds.length).toBe(3);
|
|
|
|
|
|
|
|
spy.restore();
|
|
|
|
});
|
2016-02-10 20:36:35 +03:00
|
|
|
|
|
|
|
it('should update currentStateIndex when auto-committing', () => {
|
|
|
|
let spy = spyOn(console, 'error');
|
2016-02-13 02:27:55 +03:00
|
|
|
let liftedStoreState, currentComputedState;
|
2016-02-10 20:36:35 +03:00
|
|
|
|
|
|
|
configuredStore.dispatch({ type: 'INCREMENT' });
|
|
|
|
configuredStore.dispatch({ type: 'INCREMENT' });
|
|
|
|
liftedStoreState = configuredLiftedStore.getState();
|
|
|
|
expect(liftedStoreState.currentStateIndex).toBe(2);
|
|
|
|
|
2016-02-13 02:27:55 +03:00
|
|
|
// currentStateIndex should stay at 2 as actions are committed
|
2016-02-10 20:36:35 +03:00
|
|
|
configuredStore.dispatch({ type: 'INCREMENT' });
|
|
|
|
liftedStoreState = configuredLiftedStore.getState();
|
|
|
|
currentComputedState = liftedStoreState.computedStates[liftedStoreState.currentStateIndex];
|
2016-02-13 02:27:55 +03:00
|
|
|
expect(liftedStoreState.currentStateIndex).toBe(2);
|
2016-02-10 20:36:35 +03:00
|
|
|
expect(currentComputedState.state).toBe(3);
|
|
|
|
|
2016-02-13 02:27:55 +03:00
|
|
|
spy.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should continue to increment currentStateIndex while error blocks commit', () => {
|
|
|
|
let spy = spyOn(console, 'error');
|
|
|
|
|
|
|
|
let storeWithBug = createStore(counterWithBug, instrument(undefined, { maxAge: 3 }));
|
|
|
|
let liftedStoreWithBug = storeWithBug.liftedStore;
|
|
|
|
|
|
|
|
storeWithBug.dispatch({ type: 'DECREMENT' });
|
|
|
|
storeWithBug.dispatch({ type: 'DECREMENT' });
|
|
|
|
storeWithBug.dispatch({ type: 'DECREMENT' });
|
|
|
|
storeWithBug.dispatch({ type: 'DECREMENT' });
|
|
|
|
|
|
|
|
let liftedStoreState = liftedStoreWithBug.getState();
|
|
|
|
let currentComputedState = liftedStoreState.computedStates[liftedStoreState.currentStateIndex];
|
|
|
|
expect(liftedStoreState.currentStateIndex).toBe(4);
|
2016-02-10 20:36:35 +03:00
|
|
|
expect(currentComputedState.state).toBe(0);
|
2016-02-10 20:42:42 +03:00
|
|
|
expect(currentComputedState.error).toExist();
|
2016-02-10 20:36:35 +03:00
|
|
|
|
2016-02-13 02:27:55 +03:00
|
|
|
spy.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should adjust currentStateIndex correctly when multiple actions are committed', () => {
|
|
|
|
let spy = spyOn(console, 'error');
|
|
|
|
|
|
|
|
let storeWithBug = createStore(counterWithBug, instrument(undefined, { maxAge: 3 }));
|
|
|
|
let liftedStoreWithBug = storeWithBug.liftedStore;
|
|
|
|
|
|
|
|
storeWithBug.dispatch({ type: 'DECREMENT' });
|
|
|
|
storeWithBug.dispatch({ type: 'DECREMENT' });
|
|
|
|
storeWithBug.dispatch({ type: 'DECREMENT' });
|
|
|
|
storeWithBug.dispatch({ type: 'DECREMENT' });
|
|
|
|
|
|
|
|
// Auto-commit 2 actions by "fixing" reducer bug.
|
|
|
|
storeWithBug.replaceReducer(counter);
|
|
|
|
let liftedStoreState = liftedStoreWithBug.getState();
|
|
|
|
let currentComputedState = liftedStoreState.computedStates[liftedStoreState.currentStateIndex];
|
|
|
|
expect(liftedStoreState.currentStateIndex).toBe(2);
|
|
|
|
expect(currentComputedState.state).toBe(-4);
|
|
|
|
|
|
|
|
spy.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not allow currentStateIndex to drop below 0', () => {
|
|
|
|
let spy = spyOn(console, 'error');
|
|
|
|
|
|
|
|
let storeWithBug = createStore(counterWithBug, instrument(undefined, { maxAge: 3 }));
|
|
|
|
let liftedStoreWithBug = storeWithBug.liftedStore;
|
|
|
|
|
|
|
|
storeWithBug.dispatch({ type: 'DECREMENT' });
|
|
|
|
storeWithBug.dispatch({ type: 'DECREMENT' });
|
|
|
|
storeWithBug.dispatch({ type: 'DECREMENT' });
|
|
|
|
storeWithBug.dispatch({ type: 'DECREMENT' });
|
|
|
|
liftedStoreWithBug.dispatch(ActionCreators.jumpToState(1));
|
|
|
|
|
|
|
|
// Auto-commit 2 actions by "fixing" reducer bug.
|
|
|
|
storeWithBug.replaceReducer(counter);
|
|
|
|
let liftedStoreState = liftedStoreWithBug.getState();
|
|
|
|
let currentComputedState = liftedStoreState.computedStates[liftedStoreState.currentStateIndex];
|
2016-02-10 20:36:35 +03:00
|
|
|
expect(liftedStoreState.currentStateIndex).toBe(0);
|
2016-02-13 02:27:55 +03:00
|
|
|
expect(currentComputedState.state).toBe(-2);
|
2016-02-10 20:36:35 +03:00
|
|
|
|
|
|
|
spy.restore();
|
|
|
|
});
|
2016-02-04 22:41:45 +03:00
|
|
|
});
|
|
|
|
|
2015-10-17 02:34:46 +03:00
|
|
|
describe('Import State', () => {
|
|
|
|
let monitoredStore;
|
|
|
|
let monitoredLiftedStore;
|
|
|
|
let exportedState;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2016-02-02 20:52:24 +03:00
|
|
|
monitoredStore = createStore(counter, instrument());
|
2015-10-17 02:34:46 +03:00
|
|
|
monitoredLiftedStore = monitoredStore.liftedStore;
|
|
|
|
// Set up state to export
|
|
|
|
monitoredStore.dispatch({ type: 'INCREMENT' });
|
|
|
|
monitoredStore.dispatch({ type: 'INCREMENT' });
|
|
|
|
monitoredStore.dispatch({ type: 'INCREMENT' });
|
|
|
|
|
|
|
|
exportedState = monitoredLiftedStore.getState();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should replay all the steps when a state is imported', () => {
|
2016-02-02 20:52:24 +03:00
|
|
|
let importMonitoredStore = createStore(counter, instrument());
|
2015-10-17 02:34:46 +03:00
|
|
|
let importMonitoredLiftedStore = importMonitoredStore.liftedStore;
|
2015-10-17 02:35:58 +03:00
|
|
|
|
2015-10-17 02:34:46 +03:00
|
|
|
importMonitoredLiftedStore.dispatch(ActionCreators.importState(exportedState));
|
|
|
|
expect(importMonitoredLiftedStore.getState()).toEqual(exportedState);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should replace the existing action log with the one imported', () => {
|
2016-02-02 20:52:24 +03:00
|
|
|
let importMonitoredStore = createStore(counter, instrument());
|
2015-10-17 02:34:46 +03:00
|
|
|
let importMonitoredLiftedStore = importMonitoredStore.liftedStore;
|
|
|
|
|
|
|
|
importMonitoredStore.dispatch({ type: 'DECREMENT' });
|
|
|
|
importMonitoredStore.dispatch({ type: 'DECREMENT' });
|
|
|
|
|
|
|
|
importMonitoredLiftedStore.dispatch(ActionCreators.importState(exportedState));
|
|
|
|
expect(importMonitoredLiftedStore.getState()).toEqual(exportedState);
|
|
|
|
});
|
|
|
|
});
|
2016-01-30 16:52:01 +03:00
|
|
|
|
|
|
|
it('throws if reducer is not a function', () => {
|
|
|
|
expect(() =>
|
2016-02-02 20:52:24 +03:00
|
|
|
createStore(undefined, instrument())
|
|
|
|
).toThrow('Expected the reducer to be a function.');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('warns if the reducer is not a function but has a default field that is', () => {
|
|
|
|
expect(() =>
|
2016-02-02 20:59:55 +03:00
|
|
|
createStore(({ 'default': () => {} }), instrument())
|
2016-02-02 20:52:24 +03:00
|
|
|
).toThrow(
|
|
|
|
'Expected the reducer to be a function. ' +
|
|
|
|
'Instead got an object with a "default" field. ' +
|
|
|
|
'Did you pass a module instead of the default export? ' +
|
|
|
|
'Try passing require(...).default instead.'
|
|
|
|
);
|
2016-01-30 16:52:01 +03:00
|
|
|
});
|
2016-02-02 20:52:24 +03:00
|
|
|
|
2016-02-02 19:07:58 +03:00
|
|
|
it('throws if there are more than one instrument enhancer included', () => {
|
|
|
|
expect(() => {
|
2016-02-02 20:59:55 +03:00
|
|
|
createStore(counter, compose(instrument(), instrument()));
|
2016-02-02 20:52:24 +03:00
|
|
|
}).toThrow(
|
|
|
|
'DevTools instrumentation should not be applied more than once. ' +
|
|
|
|
'Check your store configuration.'
|
|
|
|
);
|
2016-02-02 19:07:58 +03:00
|
|
|
});
|
2015-09-19 23:05:05 +03:00
|
|
|
});
|