|
| 1 | +import { configureStore } from './configureStore' |
| 2 | + |
| 3 | +import createSerializableStateInvariantMiddleware, { |
| 4 | + findNonSerializableValue |
| 5 | +} from './serializableStateInvariantMiddleware' |
| 6 | + |
| 7 | +describe('findNonSerializableValue', () => { |
| 8 | + it('Should return false if no matching values are found', () => { |
| 9 | + const obj = { |
| 10 | + a: 42, |
| 11 | + b: { |
| 12 | + b1: 'test' |
| 13 | + }, |
| 14 | + c: [99, { d: 123 }] |
| 15 | + } |
| 16 | + |
| 17 | + const result = findNonSerializableValue(obj) |
| 18 | + |
| 19 | + expect(result).toBe(false) |
| 20 | + }) |
| 21 | + |
| 22 | + it('Should return a keypath and the value if it finds a non-serializable value', () => { |
| 23 | + function testFunction() {} |
| 24 | + |
| 25 | + const obj = { |
| 26 | + a: 42, |
| 27 | + b: { |
| 28 | + b1: testFunction |
| 29 | + }, |
| 30 | + c: [99, { d: 123 }] |
| 31 | + } |
| 32 | + |
| 33 | + const result = findNonSerializableValue(obj) |
| 34 | + |
| 35 | + expect(result).toEqual({ keyPath: 'b.b1', value: testFunction }) |
| 36 | + }) |
| 37 | + |
| 38 | + it('Should return the first non-serializable value it finds', () => { |
| 39 | + const map = new Map() |
| 40 | + const symbol = Symbol.for('testSymbol') |
| 41 | + function testFunction() {} |
| 42 | + |
| 43 | + const obj = { |
| 44 | + a: 42, |
| 45 | + b: { |
| 46 | + b1: 1 |
| 47 | + }, |
| 48 | + c: [99, { d: 123 }, map, symbol, 'test'], |
| 49 | + d: symbol |
| 50 | + } |
| 51 | + |
| 52 | + const result = findNonSerializableValue(obj) |
| 53 | + |
| 54 | + expect(result).toEqual({ keyPath: 'c.2', value: map }) |
| 55 | + }) |
| 56 | + |
| 57 | + it('Should return a specific value if the root object is non-serializable', () => { |
| 58 | + const value = new Map() |
| 59 | + const result = findNonSerializableValue(value) |
| 60 | + |
| 61 | + expect(result).toEqual({ keyPath: '<root>', value }) |
| 62 | + }) |
| 63 | +}) |
| 64 | + |
| 65 | +describe('serializableStateInvariantMiddleware', () => { |
| 66 | + beforeEach(() => { |
| 67 | + console.error = jest.fn() |
| 68 | + }) |
| 69 | + |
| 70 | + it('Should log an error when a non-serializable action is dispatched', () => { |
| 71 | + const reducer = (state = 0, action) => state + 1 |
| 72 | + |
| 73 | + const serializableStateInvariantMiddleware = createSerializableStateInvariantMiddleware() |
| 74 | + |
| 75 | + const store = configureStore({ |
| 76 | + reducer, |
| 77 | + middleware: [serializableStateInvariantMiddleware] |
| 78 | + }) |
| 79 | + |
| 80 | + const type = Symbol.for('SOME_CONSTANT') |
| 81 | + const dispatchedAction = { type } |
| 82 | + |
| 83 | + store.dispatch(dispatchedAction) |
| 84 | + |
| 85 | + expect(console.error).toHaveBeenCalled() |
| 86 | + |
| 87 | + const [message, keyPath, value, action] = console.error.mock.calls[0] |
| 88 | + expect(message).toContain('detected in an action, in the path: `%s`') |
| 89 | + expect(keyPath).toBe('type') |
| 90 | + expect(value).toBe(type) |
| 91 | + expect(action).toBe(dispatchedAction) |
| 92 | + }) |
| 93 | + |
| 94 | + it('Should log an error when a non-serializable value is in state', () => { |
| 95 | + const ACTION_TYPE = 'TEST_ACTION' |
| 96 | + |
| 97 | + const initialState = { |
| 98 | + a: 0 |
| 99 | + } |
| 100 | + |
| 101 | + const badValue = new Map() |
| 102 | + |
| 103 | + const reducer = (state = initialState, action) => { |
| 104 | + switch (action.type) { |
| 105 | + case ACTION_TYPE: { |
| 106 | + return { |
| 107 | + a: badValue |
| 108 | + } |
| 109 | + } |
| 110 | + default: |
| 111 | + return state |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + const serializableStateInvariantMiddleware = createSerializableStateInvariantMiddleware() |
| 116 | + |
| 117 | + const store = configureStore({ |
| 118 | + reducer: { |
| 119 | + testSlice: reducer |
| 120 | + }, |
| 121 | + middleware: [serializableStateInvariantMiddleware] |
| 122 | + }) |
| 123 | + |
| 124 | + store.dispatch({ type: ACTION_TYPE }) |
| 125 | + |
| 126 | + expect(console.error).toHaveBeenCalled() |
| 127 | + |
| 128 | + const [message, keyPath, value, actionType] = console.error.mock.calls[0] |
| 129 | + expect(message).toContain('detected in the state, in the path: `%s`') |
| 130 | + expect(keyPath).toBe('testSlice.a') |
| 131 | + expect(value).toBe(badValue) |
| 132 | + expect(actionType).toBe(ACTION_TYPE) |
| 133 | + }) |
| 134 | +}) |
0 commit comments