redux-devtools/test/devTools.spec.js

247 lines
7.4 KiB
JavaScript
Raw Normal View History

2015-09-19 23:05:05 +03:00
import expect, { spyOn } from 'expect';
import { createStore } from 'redux';
import devTools, { ActionCreators } from '../src/devTools';
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;
}
}
function doubleCounter(state = 0, action) {
switch (action.type) {
case 'INCREMENT': return state + 2;
case 'DECREMENT': return state - 2;
default: return state;
}
}
describe('devTools', () => {
let store;
let devToolsStore;
beforeEach(() => {
store = devTools()(createStore)(counter);
devToolsStore = store.devToolsStore;
});
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);
devToolsStore.dispatch(ActionCreators.commit());
expect(store.getState()).toBe(2);
store.dispatch({ type: 'INCREMENT' });
store.dispatch({ type: 'INCREMENT' });
expect(store.getState()).toBe(4);
devToolsStore.dispatch(ActionCreators.rollback());
expect(store.getState()).toBe(2);
store.dispatch({ type: 'DECREMENT' });
expect(store.getState()).toBe(1);
devToolsStore.dispatch(ActionCreators.rollback());
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);
devToolsStore.dispatch(ActionCreators.commit());
expect(store.getState()).toBe(1);
store.dispatch({ type: 'INCREMENT' });
expect(store.getState()).toBe(2);
devToolsStore.dispatch(ActionCreators.rollback());
expect(store.getState()).toBe(1);
store.dispatch({ type: 'INCREMENT' });
expect(store.getState()).toBe(2);
devToolsStore.dispatch(ActionCreators.reset());
expect(store.getState()).toBe(0);
});
2015-09-23 22:43:55 +03:00
it('should toggle an action', () => {
2015-09-19 23:05:05 +03:00
// stateIndex 0 = @@INIT
store.dispatch({ type: 'INCREMENT' });
store.dispatch({ type: 'DECREMENT' });
store.dispatch({ type: 'INCREMENT' });
expect(store.getState()).toBe(1);
devToolsStore.dispatch(ActionCreators.toggleAction(2));
expect(store.getState()).toBe(2);
devToolsStore.dispatch(ActionCreators.toggleAction(2));
expect(store.getState()).toBe(1);
});
2015-09-24 20:11:11 +03:00
it('should sweep disabled actions', () => {
2015-09-19 23:05:05 +03:00
// stateIndex 0 = @@INIT
store.dispatch({ type: 'INCREMENT' });
store.dispatch({ type: 'DECREMENT' });
store.dispatch({ type: 'INCREMENT' });
store.dispatch({ type: 'INCREMENT' });
expect(store.getState()).toBe(2);
devToolsStore.dispatch(ActionCreators.toggleAction(2));
expect(store.getState()).toBe(3);
devToolsStore.dispatch(ActionCreators.sweep());
expect(store.getState()).toBe(3);
devToolsStore.dispatch(ActionCreators.toggleAction(2));
expect(store.getState()).toBe(2);
devToolsStore.dispatch(ActionCreators.sweep());
expect(store.getState()).toBe(2);
});
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);
devToolsStore.dispatch(ActionCreators.jumpToState(0));
expect(store.getState()).toBe(0);
devToolsStore.dispatch(ActionCreators.jumpToState(1));
expect(store.getState()).toBe(1);
devToolsStore.dispatch(ActionCreators.jumpToState(2));
expect(store.getState()).toBe(0);
store.dispatch({ type: 'INCREMENT' });
expect(store.getState()).toBe(0);
devToolsStore.dispatch(ActionCreators.jumpToState(4));
expect(store.getState()).toBe(2);
});
2015-09-23 22:43:55 +03:00
it('should set monitor state', () => {
2015-09-19 23:05:05 +03:00
devToolsStore.dispatch(ActionCreators.setMonitorState({test: 'test'}));
expect(devToolsStore.getState().monitorState.test).toBe('test');
});
2015-09-23 22:43:55 +03:00
it('should recompute', () => {
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-09-24 20:12:24 +03:00
let stagedActions = [...store.devToolsStore.getState().stagedActions];
// replace DECREMENT with INCREMENT (stagedAction[0] is @@INIT)
stagedActions[2] = { type: 'INCREMENT' };
const committedState = 10;
2015-09-19 23:05:05 +03:00
devToolsStore.dispatch(ActionCreators.recomputeStates(
2015-09-24 20:12:24 +03:00
committedState,
stagedActions
2015-09-19 23:05:05 +03:00
));
expect(store.getState()).toBe(13);
});
2015-09-23 22:43:55 +03:00
it('should get the reducer', () => {
2015-09-19 23:05:05 +03:00
expect(store.getReducer()).toBe(counter);
});
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');
let storeWithBug = devTools()(createStore)(counterWithBug);
storeWithBug.dispatch({ type: 'INCREMENT' });
storeWithBug.dispatch({ type: 'DECREMENT' });
storeWithBug.dispatch({ type: 'INCREMENT' });
let devStoreState = storeWithBug.devToolsStore.getState();
expect(devStoreState.computedStates[2].error).toMatch(
/ReferenceError/
);
expect(devStoreState.computedStates[3].error).toMatch(
/Interrupted by an error up the chain/
);
expect(spy.calls[0].arguments[0]).toMatch(
/ReferenceError/
);
spy.restore();
});
2015-09-24 21:18:58 +03:00
it('should return the last non-undefined state from getState', () => {
let storeWithBug = devTools()(createStore)(counterWithBug);
storeWithBug.dispatch({ type: 'INCREMENT' });
storeWithBug.dispatch({ type: 'INCREMENT' });
expect(storeWithBug.getState()).toBe(2);
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;
let monitoredStore = devTools()(createStore)(() => reducerCalls++);
expect(reducerCalls).toBe(1);
monitoredStore.dispatch({ type: 'INCREMENT' });
monitoredStore.dispatch({ type: 'INCREMENT' });
monitoredStore.dispatch({ type: 'INCREMENT' });
expect(reducerCalls).toBe(4);
});
it('should not recompute states when jumping to state', () => {
let reducerCalls = 0;
let monitoredStore = devTools()(createStore)(() => reducerCalls++);
let monitoredDevToolsStore = monitoredStore.devToolsStore;
expect(reducerCalls).toBe(1);
monitoredStore.dispatch({ type: 'INCREMENT' });
monitoredStore.dispatch({ type: 'INCREMENT' });
monitoredStore.dispatch({ type: 'INCREMENT' });
expect(reducerCalls).toBe(4);
monitoredDevToolsStore.dispatch(ActionCreators.jumpToState(0));
expect(reducerCalls).toBe(4);
monitoredDevToolsStore.dispatch(ActionCreators.jumpToState(1));
expect(reducerCalls).toBe(4);
monitoredDevToolsStore.dispatch(ActionCreators.jumpToState(3));
expect(reducerCalls).toBe(4);
});
2015-09-19 23:05:05 +03:00
});