File size: 8,013 Bytes
c2b7eb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import type { UnknownAction } from 'redux'
import type { SerializedError } from '../../src'
import {
  createAction,
  createAsyncThunk,
  isAllOf,
  isAnyOf,
  isAsyncThunkAction,
  isFulfilled,
  isPending,
  isRejected,
  isRejectedWithValue,
} from '../../src'

const action: UnknownAction = { type: 'foo' }

describe('type tests', () => {
  describe('isAnyOf', () => {
    test('isAnyOf correctly narrows types when used with action creators', () => {
      const actionA = createAction('a', () => {
        return {
          payload: {
            prop1: 1,
            prop3: 2,
          },
        }
      })

      const actionB = createAction('b', () => {
        return {
          payload: {
            prop1: 1,
            prop2: 2,
          },
        }
      })

      if (isAnyOf(actionA, actionB)(action)) {
        expectTypeOf(action.payload).toHaveProperty('prop1')

        expectTypeOf(action.payload).not.toHaveProperty('prop2')

        expectTypeOf(action.payload).not.toHaveProperty('prop3')
      }
    })

    test('isAnyOf correctly narrows types when used with async thunks', () => {
      const asyncThunk1 = createAsyncThunk<{ prop1: number; prop3: number }>(
        'asyncThunk1',

        async () => {
          return {
            prop1: 1,
            prop3: 3,
          }
        },
      )

      const asyncThunk2 = createAsyncThunk<{ prop1: number; prop2: number }>(
        'asyncThunk2',

        async () => {
          return {
            prop1: 1,
            prop2: 2,
          }
        },
      )

      if (isAnyOf(asyncThunk1.fulfilled, asyncThunk2.fulfilled)(action)) {
        expectTypeOf(action.payload).toHaveProperty('prop1')

        expectTypeOf(action.payload).not.toHaveProperty('prop2')

        expectTypeOf(action.payload).not.toHaveProperty('prop3')
      }
    })

    test('isAnyOf correctly narrows types when used with type guards', () => {
      interface ActionA {
        type: 'a'
        payload: {
          prop1: 1
          prop3: 2
        }
      }

      interface ActionB {
        type: 'b'
        payload: {
          prop1: 1
          prop2: 2
        }
      }

      const guardA = (v: any): v is ActionA => {
        return v.type === 'a'
      }

      const guardB = (v: any): v is ActionB => {
        return v.type === 'b'
      }

      if (isAnyOf(guardA, guardB)(action)) {
        expectTypeOf(action.payload).toHaveProperty('prop1')

        expectTypeOf(action.payload).not.toHaveProperty('prop2')

        expectTypeOf(action.payload).not.toHaveProperty('prop3')
      }
    })
  })

  describe('isAllOf', () => {
    interface SpecialAction {
      payload: {
        special: boolean
      }
    }

    const isSpecialAction = (v: any): v is SpecialAction => {
      return v.meta.isSpecial
    }

    test('isAllOf correctly narrows types when used with action creators and type guards', () => {
      const actionA = createAction('a', () => {
        return {
          payload: {
            prop1: 1,
            prop3: 2,
          },
        }
      })

      if (isAllOf(actionA, isSpecialAction)(action)) {
        expectTypeOf(action.payload).toHaveProperty('prop1')

        expectTypeOf(action.payload).not.toHaveProperty('prop2')

        expectTypeOf(action.payload).toHaveProperty('prop3')

        expectTypeOf(action.payload).toHaveProperty('special')
      }
    })

    test('isAllOf correctly narrows types when used with async thunks and type guards', () => {
      const asyncThunk1 = createAsyncThunk<{ prop1: number; prop3: number }>(
        'asyncThunk1',

        async () => {
          return {
            prop1: 1,
            prop3: 3,
          }
        },
      )

      if (isAllOf(asyncThunk1.fulfilled, isSpecialAction)(action)) {
        expectTypeOf(action.payload).toHaveProperty('prop1')

        expectTypeOf(action.payload).not.toHaveProperty('prop2')

        expectTypeOf(action.payload).toHaveProperty('prop3')

        expectTypeOf(action.payload).toHaveProperty('special')
      }
    })

    test('isAnyOf correctly narrows types when used with type guards', () => {
      interface ActionA {
        type: 'a'
        payload: {
          prop1: 1
          prop3: 2
        }
      }

      const guardA = (v: any): v is ActionA => {
        return v.type === 'a'
      }

      if (isAllOf(guardA, isSpecialAction)(action)) {
        expectTypeOf(action.payload).toHaveProperty('prop1')

        expectTypeOf(action.payload).not.toHaveProperty('prop2')

        expectTypeOf(action.payload).toHaveProperty('prop3')

        expectTypeOf(action.payload).toHaveProperty('special')
      }
    })

    test('isPending correctly narrows types', () => {
      if (isPending(action)) {
        expectTypeOf(action.payload).toBeUndefined()

        expectTypeOf(action).not.toHaveProperty('error')
      }

      const thunk = createAsyncThunk<string>('a', () => 'result')

      if (isPending(thunk)(action)) {
        expectTypeOf(action.payload).toBeUndefined()

        expectTypeOf(action).not.toHaveProperty('error')
      }
    })

    test('isRejected correctly narrows types', () => {
      if (isRejected(action)) {
        // might be there if rejected with payload
        expectTypeOf(action.payload).toBeUnknown()

        expectTypeOf(action.error).toEqualTypeOf<SerializedError>()
      }

      const thunk = createAsyncThunk<string>('a', () => 'result')

      if (isRejected(thunk)(action)) {
        // might be there if rejected with payload
        expectTypeOf(action.payload).toBeUnknown()

        expectTypeOf(action.error).toEqualTypeOf<SerializedError>()
      }
    })

    test('isFulfilled correctly narrows types', () => {
      if (isFulfilled(action)) {
        expectTypeOf(action.payload).toBeUnknown()

        expectTypeOf(action).not.toHaveProperty('error')
      }

      const thunk = createAsyncThunk<string>('a', () => 'result')
      if (isFulfilled(thunk)(action)) {
        expectTypeOf(action.payload).toBeString()

        expectTypeOf(action).not.toHaveProperty('error')
      }
    })

    test('isAsyncThunkAction correctly narrows types', () => {
      if (isAsyncThunkAction(action)) {
        expectTypeOf(action.payload).toBeUnknown()

        // do not expect an error property because pending/fulfilled lack it
        expectTypeOf(action).not.toHaveProperty('error')
      }

      const thunk = createAsyncThunk<string>('a', () => 'result')
      if (isAsyncThunkAction(thunk)(action)) {
        // we should expect the payload to be available, but of unknown type because the action may be pending/rejected
        expectTypeOf(action.payload).toBeUnknown()

        // do not expect an error property because pending/fulfilled lack it
        expectTypeOf(action).not.toHaveProperty('error')
      }
    })

    test('isRejectedWithValue correctly narrows types', () => {
      if (isRejectedWithValue(action)) {
        expectTypeOf(action.payload).toBeUnknown()

        expectTypeOf(action.error).toEqualTypeOf<SerializedError>()
      }

      const thunk = createAsyncThunk<
        string,
        void,
        { rejectValue: { message: string } }
      >('a', () => 'result')
      if (isRejectedWithValue(thunk)(action)) {
        expectTypeOf(action.payload).toEqualTypeOf({ message: '' as string })

        expectTypeOf(action.error).toEqualTypeOf<SerializedError>()
      }
    })
  })

  test('matchersAcceptSpreadArguments', () => {
    const thunk1 = createAsyncThunk('a', () => 'a')
    const thunk2 = createAsyncThunk('b', () => 'b')
    const interestingThunks = [thunk1, thunk2]
    const interestingPendingThunks = interestingThunks.map(
      (thunk) => thunk.pending,
    )
    const interestingFulfilledThunks = interestingThunks.map(
      (thunk) => thunk.fulfilled,
    )

    const isLoading = isAnyOf(...interestingPendingThunks)
    const isNotLoading = isAnyOf(...interestingFulfilledThunks)

    const isAllLoading = isAllOf(...interestingPendingThunks)
  })
})