redux-devtools/test/instrument.spec.js

255 lines
7.9 KiB
JavaScript
Raw Normal View History

2015-09-19 23:05:05 +03:00
import expect, { spyOn } from 'expect';
import { createStore } 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;
}
}
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(() => {
2015-09-28 18:38:33 +03:00
store = instrument()(createStore)(counter);
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-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);
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-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);
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 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 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.sweep());
2015-09-19 23:05:05 +03:00
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);
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');
2015-09-28 18:38:33 +03:00
let storeWithBug = instrument()(createStore)(counterWithBug);
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/
);
spy.restore();
});
2015-09-24 21:18:58 +03:00
it('should return the last non-undefined state from getState', () => {
2015-09-28 18:38:33 +03:00
let storeWithBug = instrument()(createStore)(counterWithBug);
2015-09-24 21:18:58 +03:00
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;
2015-09-28 18:38:33 +03:00
let monitoredStore = instrument()(createStore)(() => reducerCalls++);
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);
});
it('should not recompute states when jumping to state', () => {
let reducerCalls = 0;
2015-09-28 18:38:33 +03:00
let monitoredStore = instrument()(createStore)(() => reducerCalls++);
2015-10-17 02:21:07 +03:00
let monitoredInstrumentedStore = 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-09-28 18:38:33 +03:00
monitoredInstrumentedStore.dispatch(ActionCreators.jumpToState(0));
2015-09-27 20:13:38 +03:00
expect(reducerCalls).toBe(4);
2015-09-28 18:38:33 +03:00
monitoredInstrumentedStore.dispatch(ActionCreators.jumpToState(1));
2015-09-27 20:13:38 +03:00
expect(reducerCalls).toBe(4);
2015-09-28 18:38:33 +03:00
monitoredInstrumentedStore.dispatch(ActionCreators.jumpToState(3));
2015-09-27 20:13:38 +03:00
expect(reducerCalls).toBe(4);
});
2015-10-17 02:34:46 +03:00
describe('Import State', () => {
let monitoredStore;
let monitoredLiftedStore;
let exportedState;
beforeEach(() => {
monitoredStore = instrument()(createStore)(counter);
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', () => {
let importMonitoredStore = instrument()(createStore)(counter);
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', () => {
let importMonitoredStore = instrument()(createStore)(counter);
let importMonitoredLiftedStore = importMonitoredStore.liftedStore;
importMonitoredStore.dispatch({ type: 'DECREMENT' });
importMonitoredStore.dispatch({ type: 'DECREMENT' });
importMonitoredLiftedStore.dispatch(ActionCreators.importState(exportedState));
expect(importMonitoredLiftedStore.getState()).toEqual(exportedState);
});
});
2015-09-19 23:05:05 +03:00
});