|
| 1 | +import expect from 'expect'; |
| 2 | +import devTools from '../src/devTools'; |
| 3 | +import persistState from '../src/persistState'; |
| 4 | +import { compose, createStore } from 'redux'; |
| 5 | + |
| 6 | +describe('persistState', () => { |
| 7 | + let savedLocalStorage = global.localStorage; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + global.localStorage = { |
| 11 | + store: {}, |
| 12 | + getItem(key) { |
| 13 | + return this.store[key] || null; |
| 14 | + }, |
| 15 | + setItem(key, value) { |
| 16 | + this.store[key] = value; |
| 17 | + }, |
| 18 | + removeItem(key) { |
| 19 | + delete this.store[key]; |
| 20 | + }, |
| 21 | + clear() { |
| 22 | + this.store = {}; |
| 23 | + } |
| 24 | + }; |
| 25 | + }); |
| 26 | + |
| 27 | + after(() => { |
| 28 | + global.localStorage = savedLocalStorage; |
| 29 | + }); |
| 30 | + |
| 31 | + const reducer = (state = 0, action) => { |
| 32 | + switch (action.type) { |
| 33 | + case 'INCREMENT': |
| 34 | + return state + 1; |
| 35 | + case 'DECREMENT': |
| 36 | + return state - 1; |
| 37 | + default: |
| 38 | + return state; |
| 39 | + } |
| 40 | + }; |
| 41 | + |
| 42 | + it('should persist state', () => { |
| 43 | + const finalCreateStore = compose(devTools(), persistState('id'))(createStore); |
| 44 | + const store = finalCreateStore(reducer); |
| 45 | + expect(store.getState()).toBe(0); |
| 46 | + |
| 47 | + store.dispatch({ type: 'INCREMENT' }); |
| 48 | + store.dispatch({ type: 'INCREMENT' }); |
| 49 | + expect(store.getState()).toBe(2); |
| 50 | + |
| 51 | + const store2 = finalCreateStore(reducer); |
| 52 | + expect(store2.getState()).toBe(2); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should not persist state if no session id', () => { |
| 56 | + const finalCreateStore = compose(devTools(), persistState())(createStore); |
| 57 | + const store = finalCreateStore(reducer); |
| 58 | + expect(store.getState()).toBe(0); |
| 59 | + |
| 60 | + store.dispatch({ type: 'INCREMENT' }); |
| 61 | + store.dispatch({ type: 'INCREMENT' }); |
| 62 | + expect(store.getState()).toBe(2); |
| 63 | + |
| 64 | + const store2 = finalCreateStore(reducer); |
| 65 | + expect(store2.getState()).toBe(0); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should run with a custom state deserializer', () => { |
| 69 | + const oneLess = state => state === undefined ? -1 : state - 1; |
| 70 | + const finalCreateStore = compose(devTools(), persistState('id', oneLess))(createStore); |
| 71 | + const store = finalCreateStore(reducer); |
| 72 | + expect(store.getState()).toBe(0); |
| 73 | + |
| 74 | + store.dispatch({ type: 'INCREMENT' }); |
| 75 | + store.dispatch({ type: 'INCREMENT' }); |
| 76 | + expect(store.getState()).toBe(2); |
| 77 | + |
| 78 | + const store2 = finalCreateStore(reducer); |
| 79 | + expect(store2.getState()).toBe(1); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should run with a custom action deserializer', () => { |
| 83 | + const incToDec = action => action.type === 'INCREMENT' ? { type: 'DECREMENT' } : action; |
| 84 | + const finalCreateStore = compose(devTools(), persistState('id', null, incToDec))(createStore); |
| 85 | + const store = finalCreateStore(reducer); |
| 86 | + expect(store.getState()).toBe(0); |
| 87 | + |
| 88 | + store.dispatch({ type: 'INCREMENT' }); |
| 89 | + store.dispatch({ type: 'INCREMENT' }); |
| 90 | + expect(store.getState()).toBe(2); |
| 91 | + |
| 92 | + const store2 = finalCreateStore(reducer); |
| 93 | + expect(store2.getState()).toBe(-2); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should warn if read from localStorage fails', () => { |
| 97 | + const spy = expect.spyOn(console, 'warn'); |
| 98 | + const finalCreateStore = compose(devTools(), persistState('id'))(createStore); |
| 99 | + delete global.localStorage.getItem; |
| 100 | + finalCreateStore(reducer); |
| 101 | + |
| 102 | + expect(spy.calls).toContain( |
| 103 | + /Could not read debug session from localStorage/, |
| 104 | + (call, errMsg) => call.arguments[0].match(errMsg) |
| 105 | + ); |
| 106 | + |
| 107 | + spy.restore(); |
| 108 | + }); |
| 109 | + it('should warn if write to localStorage fails', () => { |
| 110 | + const spy = expect.spyOn(console, 'warn'); |
| 111 | + const finalCreateStore = compose(devTools(), persistState('id'))(createStore); |
| 112 | + delete global.localStorage.setItem; |
| 113 | + const store = finalCreateStore(reducer); |
| 114 | + |
| 115 | + store.dispatch({ type: 'INCREMENT' }); |
| 116 | + expect(spy.calls).toContain( |
| 117 | + /Could not write debug session to localStorage/, |
| 118 | + (call, errMsg) => call.arguments[0].match(errMsg) |
| 119 | + ); |
| 120 | + |
| 121 | + spy.restore(); |
| 122 | + }); |
| 123 | +}); |
0 commit comments