|
| 1 | +// tslint:disable-next-line: no-implicit-dependencies |
| 2 | +import * as Sentry from '@sentry/minimal'; |
| 3 | +import { Scope } from '@sentry/types'; |
| 4 | +import * as Redux from 'redux'; |
| 5 | + |
| 6 | +import { createReduxEnhancer } from '../src/redux'; |
| 7 | + |
| 8 | +const mockAddBreadcrumb = jest.fn(); |
| 9 | +const mockSetContext = jest.fn(); |
| 10 | + |
| 11 | +jest.mock('@sentry/minimal', () => ({ |
| 12 | + configureScope: (callback: (scope: any) => Partial<Scope>) => |
| 13 | + callback({ |
| 14 | + addBreadcrumb: mockAddBreadcrumb, |
| 15 | + setContext: mockSetContext, |
| 16 | + }), |
| 17 | +})); |
| 18 | + |
| 19 | +afterEach(() => { |
| 20 | + mockAddBreadcrumb.mockReset(); |
| 21 | + mockSetContext.mockReset(); |
| 22 | +}); |
| 23 | + |
| 24 | +describe('createReduxEnhancer', () => { |
| 25 | + it('logs redux action as breadcrumb', () => { |
| 26 | + const enhancer = createReduxEnhancer(); |
| 27 | + |
| 28 | + const initialState = {}; |
| 29 | + |
| 30 | + const store = Redux.createStore(() => initialState, enhancer); |
| 31 | + |
| 32 | + const action = { type: 'TEST_ACTION' }; |
| 33 | + store.dispatch(action); |
| 34 | + |
| 35 | + expect(mockAddBreadcrumb).toBeCalledWith({ |
| 36 | + category: 'redux.action', |
| 37 | + data: action, |
| 38 | + type: 'info', |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + it('sets latest state on to scope', () => { |
| 43 | + const enhancer = createReduxEnhancer(); |
| 44 | + |
| 45 | + const initialState = { |
| 46 | + value: 'initial', |
| 47 | + }; |
| 48 | + const ACTION_TYPE = 'UPDATE_VALUE'; |
| 49 | + |
| 50 | + const store = Redux.createStore((state: object = initialState, action: { type: string; newValue: any }) => { |
| 51 | + if (action.type === ACTION_TYPE) { |
| 52 | + return { |
| 53 | + ...state, |
| 54 | + value: action.newValue, |
| 55 | + }; |
| 56 | + } |
| 57 | + return state; |
| 58 | + }, enhancer); |
| 59 | + |
| 60 | + const updateAction = { type: ACTION_TYPE, newValue: 'updated' }; |
| 61 | + store.dispatch(updateAction); |
| 62 | + |
| 63 | + expect(mockSetContext).toBeCalledWith('redux.state', { |
| 64 | + value: 'updated', |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('transformers', () => { |
| 69 | + it('transforms state', () => { |
| 70 | + const enhancer = createReduxEnhancer({ |
| 71 | + stateTransformer: state => ({ |
| 72 | + ...state, |
| 73 | + superSecret: 'REDACTED', |
| 74 | + }), |
| 75 | + }); |
| 76 | + |
| 77 | + const initialState = { |
| 78 | + superSecret: 'SECRET!', |
| 79 | + value: 123, |
| 80 | + }; |
| 81 | + |
| 82 | + Redux.createStore((state = initialState) => state, enhancer); |
| 83 | + |
| 84 | + expect(mockSetContext).toBeCalledWith('redux.state', { |
| 85 | + superSecret: 'REDACTED', |
| 86 | + value: 123, |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + it('clears state if transformer returns null', () => { |
| 91 | + const enhancer = createReduxEnhancer({ |
| 92 | + stateTransformer: () => null, |
| 93 | + }); |
| 94 | + |
| 95 | + const initialState = { |
| 96 | + superSecret: 'SECRET!', |
| 97 | + value: 123, |
| 98 | + }; |
| 99 | + |
| 100 | + Redux.createStore((state = initialState) => state, enhancer); |
| 101 | + |
| 102 | + // Check that state is cleared |
| 103 | + expect(mockSetContext).toBeCalledWith('redux.state', null); |
| 104 | + }); |
| 105 | + |
| 106 | + it('transforms actions', () => { |
| 107 | + const ACTION_TYPES = { |
| 108 | + SAFE: 'SAFE_ACTION', |
| 109 | + SECRET: 'SUPER_SECRET_ACTION', |
| 110 | + }; |
| 111 | + |
| 112 | + const enhancer = createReduxEnhancer({ |
| 113 | + actionTransformer: action => { |
| 114 | + if (action.type === ACTION_TYPES.SECRET) { |
| 115 | + return { |
| 116 | + ...action, |
| 117 | + secret: 'I love pizza', |
| 118 | + }; |
| 119 | + } |
| 120 | + return action; |
| 121 | + }, |
| 122 | + }); |
| 123 | + |
| 124 | + const initialState = {}; |
| 125 | + |
| 126 | + const store = Redux.createStore(() => initialState, enhancer); |
| 127 | + |
| 128 | + store.dispatch({ |
| 129 | + secret: 'The Nuclear Launch Code is: Pizza', |
| 130 | + type: ACTION_TYPES.SECRET, |
| 131 | + }); |
| 132 | + |
| 133 | + expect(mockAddBreadcrumb).toBeCalledWith({ |
| 134 | + category: 'redux.action', |
| 135 | + data: { |
| 136 | + secret: 'I love pizza', |
| 137 | + type: ACTION_TYPES.SECRET, |
| 138 | + }, |
| 139 | + type: 'info', |
| 140 | + }); |
| 141 | + |
| 142 | + const safeAction = { |
| 143 | + secret: 'Not really a secret am I', |
| 144 | + type: ACTION_TYPES.SAFE, |
| 145 | + }; |
| 146 | + store.dispatch(safeAction); |
| 147 | + |
| 148 | + expect(mockAddBreadcrumb).toBeCalledWith({ |
| 149 | + category: 'redux.action', |
| 150 | + data: safeAction, |
| 151 | + type: 'info', |
| 152 | + }); |
| 153 | + |
| 154 | + // first time is redux initialize |
| 155 | + expect(mockAddBreadcrumb).toBeCalledTimes(3); |
| 156 | + }); |
| 157 | + |
| 158 | + it("doesn't send action if transformer returns null", () => { |
| 159 | + const enhancer = createReduxEnhancer({ |
| 160 | + actionTransformer: action => { |
| 161 | + if (action.type === 'COCA_COLA_RECIPE') { |
| 162 | + return null; |
| 163 | + } |
| 164 | + return action; |
| 165 | + }, |
| 166 | + }); |
| 167 | + |
| 168 | + const initialState = {}; |
| 169 | + |
| 170 | + const store = Redux.createStore((state = initialState) => state, enhancer); |
| 171 | + |
| 172 | + const safeAction = { |
| 173 | + type: 'SAFE', |
| 174 | + }; |
| 175 | + store.dispatch(safeAction); |
| 176 | + |
| 177 | + const secretAction = { |
| 178 | + cocaColaRecipe: { |
| 179 | + everythingElse: '10ml', |
| 180 | + sugar: '990ml', |
| 181 | + }, |
| 182 | + type: 'COCA_COLA_RECIPE', |
| 183 | + }; |
| 184 | + store.dispatch(secretAction); |
| 185 | + |
| 186 | + // first time is redux initialize |
| 187 | + expect(mockAddBreadcrumb).toBeCalledTimes(2); |
| 188 | + expect(mockAddBreadcrumb).toBeCalledWith({ |
| 189 | + category: 'redux.action', |
| 190 | + data: safeAction, |
| 191 | + type: 'info', |
| 192 | + }); |
| 193 | + }); |
| 194 | + }); |
| 195 | + |
| 196 | + it('configureScopeWithState is passed latest state', () => { |
| 197 | + const configureScopeWithState = jest.fn(); |
| 198 | + const enhancer = createReduxEnhancer({ |
| 199 | + configureScopeWithState, |
| 200 | + }); |
| 201 | + |
| 202 | + const initialState = { |
| 203 | + value: 'outdated', |
| 204 | + }; |
| 205 | + |
| 206 | + const UPDATE_VALUE = 'UPDATE_VALUE'; |
| 207 | + |
| 208 | + const store = Redux.createStore((state: object = initialState, action: { type: string; value: any }) => { |
| 209 | + if (action.type === UPDATE_VALUE) { |
| 210 | + return { |
| 211 | + ...state, |
| 212 | + value: action.value, |
| 213 | + }; |
| 214 | + } |
| 215 | + return state; |
| 216 | + }, enhancer); |
| 217 | + |
| 218 | + store.dispatch({ |
| 219 | + type: UPDATE_VALUE, |
| 220 | + value: 'latest', |
| 221 | + }); |
| 222 | + |
| 223 | + let scopeRef; |
| 224 | + Sentry.configureScope(scope => (scopeRef = scope)); |
| 225 | + |
| 226 | + expect(configureScopeWithState).toBeCalledWith(scopeRef, { |
| 227 | + value: 'latest', |
| 228 | + }); |
| 229 | + }); |
| 230 | +}); |
0 commit comments