File size: 6,833 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import { createSelector, setGlobalDevModeChecks } from 'reselect'
import type { LocalTestContext, RootState } from './testUtils'
import { localTest } from './testUtils'

describe('identityFunctionCheck', () => {
  const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
  const identityFunction = vi.fn(<T>(state: T) => state)
  let badSelector = createSelector(
    [(state: RootState) => state],
    identityFunction
  )

  afterEach(() => {
    consoleSpy.mockClear()
    identityFunction.mockClear()
    badSelector = createSelector(
      [(state: RootState) => state],
      identityFunction
    )
  })
  afterAll(() => {
    consoleSpy.mockRestore()
  })
  localTest(
    'calls the result function twice, and warns to console if result is the same as argument',
    ({ state }) => {
      const goodSelector = createSelector(
        [(state: RootState) => state],
        state => state.todos
      )

      expect(goodSelector(state)).toBe(state.todos)

      expect(consoleSpy).not.toHaveBeenCalled()

      expect(badSelector(state)).toBe(state)

      expect(identityFunction).toHaveBeenCalledTimes(2)

      expect(consoleSpy).toHaveBeenCalledOnce()
    }
  )

  localTest('includes stack with warning', ({ state }) => {
    expect(badSelector(state)).toBe(state)

    expect(identityFunction).toHaveBeenCalledTimes(2)

    expect(consoleSpy).toHaveBeenCalledWith(
      expect.stringContaining(
        'The result function returned its own inputs without modification'
      ),
      {
        stack: expect.any(String)
      }
    )
  })

  localTest('disables check if global setting is set to never', ({ state }) => {
    setGlobalDevModeChecks({ identityFunctionCheck: 'never' })

    expect(badSelector(state)).toBe(state)

    expect(identityFunction).toHaveBeenCalledOnce()

    expect(consoleSpy).not.toHaveBeenCalled()

    setGlobalDevModeChecks({ identityFunctionCheck: 'once' })
  })

  localTest(
    'disables check if specified in the selector options',
    ({ state }) => {
      const badSelector = createSelector(
        [(state: RootState) => state],
        identityFunction,
        { devModeChecks: { identityFunctionCheck: 'never' } }
      )

      expect(badSelector(state)).toBe(state)

      expect(identityFunction).toHaveBeenCalledOnce()

      expect(consoleSpy).not.toHaveBeenCalled()
    }
  )

  localTest('disables check in production', ({ state }) => {
    const originalEnv = process.env.NODE_ENV
    process.env.NODE_ENV = 'production'

    expect(badSelector(state)).toBe(state)

    expect(identityFunction).toHaveBeenCalledOnce()

    expect(consoleSpy).not.toHaveBeenCalled()

    process.env.NODE_ENV = originalEnv
  })

  localTest('allows running the check only once', ({ state }) => {
    const badSelector = createSelector(
      [(state: RootState) => state],
      identityFunction,
      { devModeChecks: { identityFunctionCheck: 'once' } }
    )
    expect(badSelector(state)).toBe(state)

    expect(identityFunction).toHaveBeenCalledTimes(2)

    expect(consoleSpy).toHaveBeenCalledOnce()

    const newState = { ...state }

    expect(badSelector(newState)).toBe(newState)

    expect(identityFunction).toHaveBeenCalledTimes(3)

    expect(consoleSpy).toHaveBeenCalledOnce()
  })

  localTest('allows always running the check', () => {
    const badSelector = createSelector([state => state], identityFunction, {
      devModeChecks: { identityFunctionCheck: 'always' }
    })

    const state = {}

    expect(badSelector(state)).toBe(state)

    expect(identityFunction).toHaveBeenCalledTimes(2)

    expect(consoleSpy).toHaveBeenCalledOnce()

    expect(badSelector({ ...state })).toStrictEqual(state)

    expect(identityFunction).toHaveBeenCalledTimes(4)

    expect(consoleSpy).toHaveBeenCalledTimes(2)

    expect(badSelector(state)).toBe(state)

    expect(identityFunction).toHaveBeenCalledTimes(4)

    expect(consoleSpy).toHaveBeenCalledTimes(2)
  })

  localTest('runs once when devModeChecks is an empty object', ({ state }) => {
    const badSelector = createSelector(
      [(state: RootState) => state],
      identityFunction,
      { devModeChecks: {} }
    )
    expect(badSelector(state)).toBe(state)

    expect(identityFunction).toHaveBeenCalledTimes(2)

    expect(consoleSpy).toHaveBeenCalledOnce()

    const newState = { ...state }

    expect(badSelector(newState)).toBe(newState)

    expect(identityFunction).toHaveBeenCalledTimes(3)

    expect(consoleSpy).toHaveBeenCalledOnce()
  })

  localTest('uses the memoize provided', ({ state }) => {
    const badSelector = createSelector(
      [(state: RootState) => state.todos],
      identityFunction
    )
    expect(badSelector(state)).toBe(state.todos)

    expect(identityFunction).toHaveBeenCalledTimes(2)

    expect(consoleSpy).toHaveBeenCalledOnce()

    expect(badSelector({ ...state })).not.toBe(state)

    expect(consoleSpy).toHaveBeenCalledOnce()
  })

  localTest(
    'does not warn if result function is not identity function (case 1)',
    ({ state }) => {
      // This test demonstrates why in some cases it can be useful to compare the first argument of the result
      // function with the returned value (and not just checking for an identity function by passing `{}` to the result
      // function).
      const getFirstAlertIfMessageIsEmpty = createSelector(
        [(state: RootState) => state.alerts[0]],
        firstAlert => (!firstAlert.message ? firstAlert : null)
      )

      expect(getFirstAlertIfMessageIsEmpty(state)).toBeNull()

      expect(consoleSpy).not.toHaveBeenCalled()
    }
  )

  localTest(
    'does not warn if result function is not identity function (case 2)',
    ({ state }) => {
      // This test demonstrates why in some cases it can be useful to pass `{}` into the result function and compare it
      // with the returned value (and not just checking for an identity function by passing the first argument to the
      // result function).
      const getFirstAlertIfMessageIsNotEmpty = createSelector(
        [(state: RootState) => state.alerts[0]],
        firstAlert => (firstAlert.message ? firstAlert : null)
      )

      expect(getFirstAlertIfMessageIsNotEmpty(state)).toBe(state.alerts[0])

      expect(consoleSpy).not.toHaveBeenCalled()
    }
  )

  localTest(
    'does not warn if result function is passed more than one argument',
    ({ state }) => {
      const getAllNotificationsIfSmsNotEnabled = createSelector(
        [
          (state: RootState) => state.alerts,
          (state: RootState) =>
            state.users.user.details.preferences.notifications.sms
        ],
        (alerts, smsEnabled) => (!smsEnabled ? alerts : [])
      )

      expect(getAllNotificationsIfSmsNotEnabled(state)).toBe(state.alerts)

      expect(consoleSpy).not.toHaveBeenCalled()
    }
  )
})