redux-devtools/test/persistState.spec.js

118 lines
3.5 KiB
JavaScript
Raw Permalink Normal View History

2015-09-24 21:20:02 +03:00
import expect from 'expect';
2015-09-28 18:38:33 +03:00
import { instrument, persistState } from '../src';
2015-09-24 21:20:02 +03:00
import { compose, createStore } from 'redux';
describe('persistState', () => {
let savedLocalStorage = global.localStorage;
beforeEach(() => {
global.localStorage = {
store: {},
getItem(key) {
return this.store[key] || null;
},
setItem(key, value) {
this.store[key] = value;
},
removeItem(key) {
delete this.store[key];
},
clear() {
this.store = {};
}
};
});
after(() => {
global.localStorage = savedLocalStorage;
});
const reducer = (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
};
it('should persist state', () => {
const store = createStore(reducer, compose(instrument(), persistState('id')));
2015-09-24 21:20:02 +03:00
expect(store.getState()).toBe(0);
store.dispatch({ type: 'INCREMENT' });
store.dispatch({ type: 'INCREMENT' });
expect(store.getState()).toBe(2);
const store2 = createStore(reducer, compose(instrument(), persistState('id')));
2015-09-24 21:20:02 +03:00
expect(store2.getState()).toBe(2);
});
it('should not persist state if no session id', () => {
const store = createStore(reducer, compose(instrument(), persistState()));
2015-09-24 21:20:02 +03:00
expect(store.getState()).toBe(0);
store.dispatch({ type: 'INCREMENT' });
store.dispatch({ type: 'INCREMENT' });
expect(store.getState()).toBe(2);
const store2 = createStore(reducer, compose(instrument(), persistState()));
2015-09-24 21:20:02 +03:00
expect(store2.getState()).toBe(0);
});
it('should run with a custom state deserializer', () => {
const oneLess = state => state === undefined ? -1 : state - 1;
const store = createStore(reducer, compose(instrument(), persistState('id', oneLess)));
2015-09-24 21:20:02 +03:00
expect(store.getState()).toBe(0);
store.dispatch({ type: 'INCREMENT' });
store.dispatch({ type: 'INCREMENT' });
expect(store.getState()).toBe(2);
const store2 = createStore(reducer, compose(instrument(), persistState('id', oneLess)));
2015-09-24 21:20:02 +03:00
expect(store2.getState()).toBe(1);
});
it('should run with a custom action deserializer', () => {
const incToDec = action => action.type === 'INCREMENT' ? { type: 'DECREMENT' } : action;
const store = createStore(reducer, compose(instrument(), persistState('id', undefined, incToDec)));
2015-09-24 21:20:02 +03:00
expect(store.getState()).toBe(0);
store.dispatch({ type: 'INCREMENT' });
store.dispatch({ type: 'INCREMENT' });
expect(store.getState()).toBe(2);
const store2 = createStore(reducer, compose(instrument(), persistState('id', undefined, incToDec)));
2015-09-24 21:20:02 +03:00
expect(store2.getState()).toBe(-2);
});
it('should warn if read from localStorage fails', () => {
const spy = expect.spyOn(console, 'warn');
delete global.localStorage.getItem;
2016-02-02 20:59:55 +03:00
createStore(reducer, compose(instrument(), persistState('id')));
2015-09-24 21:20:02 +03:00
expect(spy.calls).toContain(
/Could not read debug session from localStorage/,
(call, errMsg) => call.arguments[0].match(errMsg)
);
spy.restore();
});
2015-09-24 21:20:02 +03:00
it('should warn if write to localStorage fails', () => {
const spy = expect.spyOn(console, 'warn');
delete global.localStorage.setItem;
const store = createStore(reducer, compose(instrument(), persistState('id')));
2015-09-24 21:20:02 +03:00
store.dispatch({ type: 'INCREMENT' });
expect(spy.calls).toContain(
/Could not write debug session to localStorage/,
(call, errMsg) => call.arguments[0].match(errMsg)
);
spy.restore();
});
});