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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
import { noop } from '@internal/listenerMiddleware/utils'
import { delay, promiseWithResolvers } from '@internal/utils'
import type { CreateAsyncThunkFunction, UnknownAction } from '@reduxjs/toolkit'
import {
  configureStore,
  createAsyncThunk,
  createReducer,
  miniSerializeError,
  unwrapResult,
} from '@reduxjs/toolkit'

declare global {
  interface Window {
    AbortController: AbortController
  }
}

describe('createAsyncThunk', () => {
  it('creates the action types', () => {
    const thunkActionCreator = createAsyncThunk('testType', async () => 42)

    expect(thunkActionCreator.fulfilled.type).toBe('testType/fulfilled')
    expect(thunkActionCreator.pending.type).toBe('testType/pending')
    expect(thunkActionCreator.rejected.type).toBe('testType/rejected')
  })

  it('exposes the typePrefix it was created with', () => {
    const thunkActionCreator = createAsyncThunk('testType', async () => 42)

    expect(thunkActionCreator.typePrefix).toBe('testType')
  })

  it('includes a settled matcher', () => {
    const thunkActionCreator = createAsyncThunk('testType', async () => 42)
    expect(thunkActionCreator.settled).toEqual(expect.any(Function))
    expect(thunkActionCreator.settled(thunkActionCreator.pending(''))).toBe(
      false,
    )
    expect(
      thunkActionCreator.settled(thunkActionCreator.rejected(null, '')),
    ).toBe(true)
    expect(
      thunkActionCreator.settled(thunkActionCreator.fulfilled(42, '')),
    ).toBe(true)
  })

  it('works without passing arguments to the payload creator', async () => {
    const thunkActionCreator = createAsyncThunk('testType', async () => 42)

    let timesReducerCalled = 0

    const reducer = () => {
      timesReducerCalled++
    }

    const store = configureStore({
      reducer,
    })

    // reset from however many times the store called it
    timesReducerCalled = 0

    await store.dispatch(thunkActionCreator())

    expect(timesReducerCalled).toBe(2)
  })

  it('accepts arguments and dispatches the actions on resolve', async () => {
    const dispatch = vi.fn()

    let passedArg: any

    const result = 42
    const args = 123
    let generatedRequestId = ''

    const thunkActionCreator = createAsyncThunk(
      'testType',
      async (arg: number, { requestId }) => {
        passedArg = arg
        generatedRequestId = requestId
        return result
      },
    )

    const thunkFunction = thunkActionCreator(args)

    const thunkPromise = thunkFunction(dispatch, () => {}, undefined)

    expect(thunkPromise.requestId).toBe(generatedRequestId)
    expect(thunkPromise.arg).toBe(args)

    await thunkPromise

    expect(passedArg).toBe(args)

    expect(dispatch).toHaveBeenNthCalledWith(
      1,
      thunkActionCreator.pending(generatedRequestId, args),
    )

    expect(dispatch).toHaveBeenNthCalledWith(
      2,
      thunkActionCreator.fulfilled(result, generatedRequestId, args),
    )
  })

  it('accepts arguments and dispatches the actions on reject', async () => {
    const dispatch = vi.fn()

    const args = 123
    let generatedRequestId = ''

    const error = new Error('Panic!')

    const thunkActionCreator = createAsyncThunk(
      'testType',
      async (args: number, { requestId }) => {
        generatedRequestId = requestId
        throw error
      },
    )

    const thunkFunction = thunkActionCreator(args)

    try {
      await thunkFunction(dispatch, () => {}, undefined)
    } catch (e) {}

    expect(dispatch).toHaveBeenNthCalledWith(
      1,
      thunkActionCreator.pending(generatedRequestId, args),
    )

    expect(dispatch).toHaveBeenCalledTimes(2)

    // Have to check the bits of the action separately since the error was processed
    const errorAction = dispatch.mock.calls[1][0]
    expect(errorAction.error).toEqual(miniSerializeError(error))
    expect(errorAction.meta.requestId).toBe(generatedRequestId)
    expect(errorAction.meta.arg).toBe(args)
  })

  it('dispatches an empty error when throwing a random object without serializedError properties', async () => {
    const dispatch = vi.fn()

    const args = 123
    let generatedRequestId = ''

    const errorObject = { wny: 'dothis' }

    const thunkActionCreator = createAsyncThunk(
      'testType',
      async (args: number, { requestId }) => {
        generatedRequestId = requestId
        throw errorObject
      },
    )

    const thunkFunction = thunkActionCreator(args)

    try {
      await thunkFunction(dispatch, () => {}, undefined)
    } catch (e) {}

    expect(dispatch).toHaveBeenNthCalledWith(
      1,
      thunkActionCreator.pending(generatedRequestId, args),
    )

    expect(dispatch).toHaveBeenCalledTimes(2)

    const errorAction = dispatch.mock.calls[1][0]
    expect(errorAction.error).toEqual({})
    expect(errorAction.meta.requestId).toBe(generatedRequestId)
    expect(errorAction.meta.arg).toBe(args)
  })

  it('dispatches an action with a formatted error when throwing an object with known error keys', async () => {
    const dispatch = vi.fn()

    const args = 123
    let generatedRequestId = ''

    const errorObject = {
      name: 'Custom thrown error',
      message: 'This is not necessary',
      code: '400',
    }

    const thunkActionCreator = createAsyncThunk(
      'testType',
      async (args: number, { requestId }) => {
        generatedRequestId = requestId
        throw errorObject
      },
    )

    const thunkFunction = thunkActionCreator(args)

    try {
      await thunkFunction(dispatch, () => {}, undefined)
    } catch (e) {}

    expect(dispatch).toHaveBeenNthCalledWith(
      1,
      thunkActionCreator.pending(generatedRequestId, args),
    )

    expect(dispatch).toHaveBeenCalledTimes(2)

    // Have to check the bits of the action separately since the error was processed
    const errorAction = dispatch.mock.calls[1][0]
    expect(errorAction.error).toEqual(miniSerializeError(errorObject))
    expect(Object.keys(errorAction.error)).not.toContain('stack')
    expect(errorAction.meta.requestId).toBe(generatedRequestId)
    expect(errorAction.meta.arg).toBe(args)
  })

  it('dispatches a rejected action with a customized payload when a user returns rejectWithValue()', async () => {
    const dispatch = vi.fn()

    const args = 123
    let generatedRequestId = ''

    const errorPayload = {
      errorMessage:
        'I am a fake server-provided 400 payload with validation details',
      errors: [
        { field_one: 'Must be a string' },
        { field_two: 'Must be a number' },
      ],
    }

    const thunkActionCreator = createAsyncThunk(
      'testType',
      async (args: number, { requestId, rejectWithValue }) => {
        generatedRequestId = requestId

        return rejectWithValue(errorPayload)
      },
    )

    const thunkFunction = thunkActionCreator(args)

    try {
      await thunkFunction(dispatch, () => {}, undefined)
    } catch (e) {}

    expect(dispatch).toHaveBeenNthCalledWith(
      1,
      thunkActionCreator.pending(generatedRequestId, args),
    )

    expect(dispatch).toHaveBeenCalledTimes(2)

    // Have to check the bits of the action separately since the error was processed
    const errorAction = dispatch.mock.calls[1][0]

    expect(errorAction.error.message).toEqual('Rejected')
    expect(errorAction.payload).toBe(errorPayload)
    expect(errorAction.meta.arg).toBe(args)
  })

  it('dispatches a rejected action with a customized payload when a user throws rejectWithValue()', async () => {
    const dispatch = vi.fn()

    const args = 123
    let generatedRequestId = ''

    const errorPayload = {
      errorMessage:
        'I am a fake server-provided 400 payload with validation details',
      errors: [
        { field_one: 'Must be a string' },
        { field_two: 'Must be a number' },
      ],
    }

    const thunkActionCreator = createAsyncThunk(
      'testType',
      async (args: number, { requestId, rejectWithValue }) => {
        generatedRequestId = requestId

        throw rejectWithValue(errorPayload)
      },
    )

    const thunkFunction = thunkActionCreator(args)

    try {
      await thunkFunction(dispatch, () => {}, undefined)
    } catch (e) {}

    expect(dispatch).toHaveBeenNthCalledWith(
      1,
      thunkActionCreator.pending(generatedRequestId, args),
    )

    expect(dispatch).toHaveBeenCalledTimes(2)

    // Have to check the bits of the action separately since the error was processed
    const errorAction = dispatch.mock.calls[1][0]

    expect(errorAction.error.message).toEqual('Rejected')
    expect(errorAction.payload).toBe(errorPayload)
    expect(errorAction.meta.arg).toBe(args)
  })

  it('dispatches a rejected action with a miniSerializeError when rejectWithValue conditions are not satisfied', async () => {
    const dispatch = vi.fn()

    const args = 123
    let generatedRequestId = ''

    const error = new Error('Panic!')

    const errorPayload = {
      errorMessage:
        'I am a fake server-provided 400 payload with validation details',
      errors: [
        { field_one: 'Must be a string' },
        { field_two: 'Must be a number' },
      ],
    }

    const thunkActionCreator = createAsyncThunk(
      'testType',
      async (args: number, { requestId, rejectWithValue }) => {
        generatedRequestId = requestId

        try {
          throw error
        } catch (err) {
          if (!(err as any).response) {
            throw err
          }
          return rejectWithValue(errorPayload)
        }
      },
    )

    const thunkFunction = thunkActionCreator(args)

    try {
      await thunkFunction(dispatch, () => {}, undefined)
    } catch (e) {}

    expect(dispatch).toHaveBeenNthCalledWith(
      1,
      thunkActionCreator.pending(generatedRequestId, args),
    )

    expect(dispatch).toHaveBeenCalledTimes(2)

    // Have to check the bits of the action separately since the error was processed
    const errorAction = dispatch.mock.calls[1][0]
    expect(errorAction.error).toEqual(miniSerializeError(error))
    expect(errorAction.payload).toEqual(undefined)
    expect(errorAction.meta.requestId).toBe(generatedRequestId)
    expect(errorAction.meta.arg).toBe(args)
  })
})

describe('createAsyncThunk with abortController', () => {
  const asyncThunk = createAsyncThunk(
    'test',
    function abortablePayloadCreator(_: any, { signal }) {
      return new Promise((resolve, reject) => {
        if (signal.aborted) {
          reject(
            new DOMException(
              'This should never be reached as it should already be handled.',
              'AbortError',
            ),
          )
        }
        signal.addEventListener('abort', () => {
          reject(new DOMException('Was aborted while running', 'AbortError'))
        })
        setTimeout(resolve, 100)
      })
    },
  )

  let store = configureStore({
    reducer(store: UnknownAction[] = []) {
      return store
    },
  })

  beforeEach(() => {
    store = configureStore({
      reducer(store: UnknownAction[] = [], action) {
        return [...store, action]
      },
    })
  })

  test('normal usage', async () => {
    await store.dispatch(asyncThunk({}))
    expect(store.getState()).toEqual([
      expect.any(Object),
      expect.objectContaining({ type: 'test/pending' }),
      expect.objectContaining({ type: 'test/fulfilled' }),
    ])
  })

  test('abort after dispatch', async () => {
    const promise = store.dispatch(asyncThunk({}))
    promise.abort('AbortReason')
    const result = await promise
    const expectedAbortedAction = {
      type: 'test/rejected',
      error: {
        message: 'AbortReason',
        name: 'AbortError',
      },
      meta: { aborted: true, requestId: promise.requestId },
    }

    // abortedAction with reason is dispatched after test/pending is dispatched
    expect(store.getState()).toMatchObject([
      {},
      { type: 'test/pending' },
      expectedAbortedAction,
    ])

    // same abortedAction is returned, but with the AbortError from the abortablePayloadCreator
    expect(result).toMatchObject(expectedAbortedAction)

    // calling unwrapResult on the returned object re-throws the error from the abortablePayloadCreator
    expect(() => unwrapResult(result)).toThrowError(
      expect.objectContaining(expectedAbortedAction.error),
    )
  })

  test('even when the payloadCreator does not directly support the signal, no further actions are dispatched', async () => {
    const unawareAsyncThunk = createAsyncThunk('unaware', async () => {
      await new Promise((resolve) => setTimeout(resolve, 100))
      return 'finished'
    })

    const promise = store.dispatch(unawareAsyncThunk())
    promise.abort('AbortReason')
    const result = await promise

    const expectedAbortedAction = {
      type: 'unaware/rejected',
      error: {
        message: 'AbortReason',
        name: 'AbortError',
      },
    }

    // abortedAction with reason is dispatched after test/pending is dispatched
    expect(store.getState()).toEqual([
      expect.any(Object),
      expect.objectContaining({ type: 'unaware/pending' }),
      expect.objectContaining(expectedAbortedAction),
    ])

    // same abortedAction is returned, but with the AbortError from the abortablePayloadCreator
    expect(result).toMatchObject(expectedAbortedAction)

    // calling unwrapResult on the returned object re-throws the error from the abortablePayloadCreator
    expect(() => unwrapResult(result)).toThrowError(
      expect.objectContaining(expectedAbortedAction.error),
    )
  })

  test('dispatch(asyncThunk) returns on abort and does not wait for the promiseProvider to finish', async () => {
    let running = false
    const longRunningAsyncThunk = createAsyncThunk('longRunning', async () => {
      running = true
      await new Promise((resolve) => setTimeout(resolve, 30000))
      running = false
    })

    const promise = store.dispatch(longRunningAsyncThunk())
    expect(running).toBeTruthy()
    promise.abort()
    const result = await promise
    expect(running).toBeTruthy()
    expect(result).toMatchObject({
      type: 'longRunning/rejected',
      error: { message: 'Aborted', name: 'AbortError' },
      meta: { aborted: true },
    })
  })

  describe('behavior with missing AbortController', () => {
    let keepAbortController: (typeof window)['AbortController']
    let freshlyLoadedModule: typeof import('../createAsyncThunk')

    beforeEach(async () => {
      keepAbortController = window.AbortController
      delete (window as any).AbortController
      vi.resetModules()
      freshlyLoadedModule = await import('../createAsyncThunk')
      vi.stubEnv('NODE_ENV', 'development')
    })

    afterEach(() => {
      vi.unstubAllEnvs()
      vi.clearAllMocks()
      vi.stubGlobal('AbortController', keepAbortController)
      vi.resetModules()
    })

    test('calling a thunk made with createAsyncThunk should fail if no global abortController is not available', async () => {
      const longRunningAsyncThunk = freshlyLoadedModule.createAsyncThunk(
        'longRunning',
        async () => {
          await new Promise((resolve) => setTimeout(resolve, 30000))
        },
      )

      expect(longRunningAsyncThunk()).toThrow('AbortController is not defined')
    })
  })
})

test('non-serializable arguments are ignored by serializableStateInvariantMiddleware', async () => {
  const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(noop)
  const nonSerializableValue = new Map()
  const asyncThunk = createAsyncThunk('test', (arg: Map<any, any>) => {})

  configureStore({
    reducer: () => 0,
  }).dispatch(asyncThunk(nonSerializableValue))

  expect(consoleErrorSpy).not.toHaveBeenCalled()

  consoleErrorSpy.mockRestore()
})

describe('conditional skipping of asyncThunks', () => {
  const arg = {}
  const getState = vi.fn(() => ({}))
  const dispatch = vi.fn((x: any) => x)
  const payloadCreator = vi.fn((x: typeof arg) => 10)
  const condition = vi.fn(() => false)
  const extra = {}

  beforeEach(() => {
    getState.mockClear()
    dispatch.mockClear()
    payloadCreator.mockClear()
    condition.mockClear()
  })

  test('returning false from condition skips payloadCreator and returns a rejected action', async () => {
    const asyncThunk = createAsyncThunk('test', payloadCreator, { condition })
    const result = await asyncThunk(arg)(dispatch, getState, extra)

    expect(condition).toHaveBeenCalled()
    expect(payloadCreator).not.toHaveBeenCalled()
    expect(asyncThunk.rejected.match(result)).toBe(true)
    expect((result as any).meta.condition).toBe(true)
  })

  test('return falsy from condition does not skip payload creator', async () => {
    // Override TS's expectation that this is a boolean
    condition.mockReturnValueOnce(undefined as unknown as boolean)
    const asyncThunk = createAsyncThunk('test', payloadCreator, { condition })
    const result = await asyncThunk(arg)(dispatch, getState, extra)

    expect(condition).toHaveBeenCalled()
    expect(payloadCreator).toHaveBeenCalled()
    expect(asyncThunk.fulfilled.match(result)).toBe(true)
    expect(result.payload).toBe(10)
  })

  test('returning true from condition executes payloadCreator', async () => {
    condition.mockReturnValueOnce(true)
    const asyncThunk = createAsyncThunk('test', payloadCreator, { condition })
    const result = await asyncThunk(arg)(dispatch, getState, extra)

    expect(condition).toHaveBeenCalled()
    expect(payloadCreator).toHaveBeenCalled()
    expect(asyncThunk.fulfilled.match(result)).toBe(true)
    expect(result.payload).toBe(10)
  })

  test('condition is called with arg, getState and extra', async () => {
    const asyncThunk = createAsyncThunk('test', payloadCreator, { condition })
    await asyncThunk(arg)(dispatch, getState, extra)

    expect(condition).toHaveBeenCalledOnce()
    expect(condition).toHaveBeenLastCalledWith(
      arg,
      expect.objectContaining({ getState, extra }),
    )
  })

  test('pending is dispatched synchronously if condition is synchronous', async () => {
    const condition = () => true
    const asyncThunk = createAsyncThunk('test', payloadCreator, { condition })
    const thunkCallPromise = asyncThunk(arg)(dispatch, getState, extra)
    expect(dispatch).toHaveBeenCalledOnce()
    await thunkCallPromise
    expect(dispatch).toHaveBeenCalledTimes(2)
  })

  test('async condition', async () => {
    const condition = () => Promise.resolve(false)
    const asyncThunk = createAsyncThunk('test', payloadCreator, { condition })
    await asyncThunk(arg)(dispatch, getState, extra)
    expect(dispatch).not.toHaveBeenCalled()
  })

  test('async condition with rejected promise', async () => {
    const condition = () => Promise.reject()
    const asyncThunk = createAsyncThunk('test', payloadCreator, { condition })
    await asyncThunk(arg)(dispatch, getState, extra)
    expect(dispatch).toHaveBeenCalledOnce()
    expect(dispatch).toHaveBeenLastCalledWith(
      expect.objectContaining({ type: 'test/rejected' }),
    )
  })

  test('async condition with AbortController signal first', async () => {
    const condition = async () => {
      await delay(25)
      return true
    }
    const asyncThunk = createAsyncThunk('test', payloadCreator, { condition })

    try {
      const thunkPromise = asyncThunk(arg)(dispatch, getState, extra)
      thunkPromise.abort()
      await thunkPromise
    } catch (err) {}
    expect(dispatch).not.toHaveBeenCalled()
  })

  test('rejected action is not dispatched by default', async () => {
    const asyncThunk = createAsyncThunk('test', payloadCreator, { condition })
    await asyncThunk(arg)(dispatch, getState, extra)

    expect(dispatch).not.toHaveBeenCalled()
  })

  test('does not fail when attempting to abort a canceled promise', async () => {
    const asyncPayloadCreator = vi.fn(async (x: typeof arg) => {
      await delay(200)
      return 10
    })

    const asyncThunk = createAsyncThunk('test', asyncPayloadCreator, {
      condition,
    })
    const promise = asyncThunk(arg)(dispatch, getState, extra)
    promise.abort(
      `If the promise was 1. somehow canceled, 2. in a 'started' state and 3. we attempted to abort, this would crash the tests`,
    )
  })

  test('rejected action can be dispatched via option', async () => {
    const asyncThunk = createAsyncThunk('test', payloadCreator, {
      condition,
      dispatchConditionRejection: true,
    })
    await asyncThunk(arg)(dispatch, getState, extra)

    expect(dispatch).toHaveBeenCalledOnce()
    expect(dispatch).toHaveBeenLastCalledWith(
      expect.objectContaining({
        error: {
          message: 'Aborted due to condition callback returning false.',
          name: 'ConditionError',
        },
        meta: {
          aborted: false,
          arg,
          rejectedWithValue: false,
          condition: true,
          requestId: expect.stringContaining(''),
          requestStatus: 'rejected',
        },
        payload: undefined,
        type: 'test/rejected',
      }),
    )
  })
})

test('serializeError implementation', async () => {
  function serializeError() {
    return 'serialized!'
  }
  const errorObject = 'something else!'

  const store = configureStore({
    reducer: (state = [], action) => [...state, action],
  })

  const asyncThunk = createAsyncThunk<
    unknown,
    void,
    { serializedErrorType: string }
  >('test', () => Promise.reject(errorObject), { serializeError })
  const rejected = await store.dispatch(asyncThunk())
  if (!asyncThunk.rejected.match(rejected)) {
    throw new Error()
  }

  const expectation = {
    type: 'test/rejected',
    payload: undefined,
    error: 'serialized!',
    meta: expect.any(Object),
  }
  expect(rejected).toEqual(expectation)
  expect(store.getState()[2]).toEqual(expectation)
  expect(rejected.error).not.toEqual(miniSerializeError(errorObject))
})

describe('unwrapResult', () => {
  const getState = vi.fn(() => ({}))
  const dispatch = vi.fn((x: any) => x)
  const extra = {}
  test('fulfilled case', async () => {
    const asyncThunk = createAsyncThunk('test', () => {
      return 'fulfilled!' as const
    })

    const unwrapPromise = asyncThunk()(dispatch, getState, extra).then(
      unwrapResult,
    )

    await expect(unwrapPromise).resolves.toBe('fulfilled!')

    const unwrapPromise2 = asyncThunk()(dispatch, getState, extra)
    const res = await unwrapPromise2.unwrap()
    expect(res).toBe('fulfilled!')
  })
  test('error case', async () => {
    const error = new Error('Panic!')
    const asyncThunk = createAsyncThunk('test', () => {
      throw error
    })

    const unwrapPromise = asyncThunk()(dispatch, getState, extra).then(
      unwrapResult,
    )

    await expect(unwrapPromise).rejects.toEqual(miniSerializeError(error))

    const unwrapPromise2 = asyncThunk()(dispatch, getState, extra)
    await expect(unwrapPromise2.unwrap()).rejects.toEqual(
      miniSerializeError(error),
    )
  })
  test('rejectWithValue case', async () => {
    const asyncThunk = createAsyncThunk('test', (_, { rejectWithValue }) => {
      return rejectWithValue('rejectWithValue!')
    })

    const unwrapPromise = asyncThunk()(dispatch, getState, extra).then(
      unwrapResult,
    )

    await expect(unwrapPromise).rejects.toBe('rejectWithValue!')

    const unwrapPromise2 = asyncThunk()(dispatch, getState, extra)
    await expect(unwrapPromise2.unwrap()).rejects.toBe('rejectWithValue!')
  })
})

describe('idGenerator option', () => {
  const getState = () => ({})
  const dispatch = (x: any) => x
  const extra = {}

  test('idGenerator implementation - can customizes how request IDs are generated', async () => {
    function makeFakeIdGenerator() {
      let id = 0
      return vi.fn(() => {
        id++
        return `fake-random-id-${id}`
      })
    }

    let generatedRequestId = ''

    const idGenerator = makeFakeIdGenerator()
    const asyncThunk = createAsyncThunk(
      'test',
      async (args: void, { requestId }) => {
        generatedRequestId = requestId
      },
      { idGenerator },
    )

    // dispatching the thunks should be using the custom id generator
    const promise0 = asyncThunk()(dispatch, getState, extra)
    expect(generatedRequestId).toEqual('fake-random-id-1')
    expect(promise0.requestId).toEqual('fake-random-id-1')
    expect((await promise0).meta.requestId).toEqual('fake-random-id-1')

    const promise1 = asyncThunk()(dispatch, getState, extra)
    expect(generatedRequestId).toEqual('fake-random-id-2')
    expect(promise1.requestId).toEqual('fake-random-id-2')
    expect((await promise1).meta.requestId).toEqual('fake-random-id-2')

    const promise2 = asyncThunk()(dispatch, getState, extra)
    expect(generatedRequestId).toEqual('fake-random-id-3')
    expect(promise2.requestId).toEqual('fake-random-id-3')
    expect((await promise2).meta.requestId).toEqual('fake-random-id-3')

    generatedRequestId = ''
    const defaultAsyncThunk = createAsyncThunk(
      'test',
      async (args: void, { requestId }) => {
        generatedRequestId = requestId
      },
    )
    // dispatching the default options thunk should still generate an id,
    // but not using the custom id generator
    const promise3 = defaultAsyncThunk()(dispatch, getState, extra)
    expect(generatedRequestId).toEqual(promise3.requestId)
    expect(promise3.requestId).not.toEqual('')
    expect(promise3.requestId).not.toEqual(
      expect.stringContaining('fake-random-id'),
    )
    expect((await promise3).meta.requestId).not.toEqual(
      expect.stringContaining('fake-fandom-id'),
    )
  })

  test('idGenerator should be called with thunkArg', async () => {
    const customIdGenerator = vi.fn((seed) => `fake-unique-random-id-${seed}`)
    let generatedRequestId = ''
    const asyncThunk = createAsyncThunk(
      'test',
      async (args: any, { requestId }) => {
        generatedRequestId = requestId
      },
      { idGenerator: customIdGenerator },
    )

    const thunkArg = 1
    const expected = 'fake-unique-random-id-1'
    const asyncThunkPromise = asyncThunk(thunkArg)(dispatch, getState, extra)

    expect(customIdGenerator).toHaveBeenCalledWith(thunkArg)
    expect(asyncThunkPromise.requestId).toEqual(expected)
    expect((await asyncThunkPromise).meta.requestId).toEqual(expected)
  })
})

test('`condition` will see state changes from a synchronously invoked asyncThunk', () => {
  type State = ReturnType<typeof store.getState>
  const onStart = vi.fn()
  const asyncThunk = createAsyncThunk<
    void,
    { force?: boolean },
    { state: State }
  >('test', onStart, {
    condition({ force }, { getState }) {
      return force || !getState().started
    },
  })
  const store = configureStore({
    reducer: createReducer({ started: false }, (builder) => {
      builder.addCase(asyncThunk.pending, (state) => {
        state.started = true
      })
    }),
  })

  store.dispatch(asyncThunk({ force: false }))
  expect(onStart).toHaveBeenCalledOnce()
  store.dispatch(asyncThunk({ force: false }))
  expect(onStart).toHaveBeenCalledOnce()
  store.dispatch(asyncThunk({ force: true }))
  expect(onStart).toHaveBeenCalledTimes(2)
})

const getNewStore = () =>
  configureStore({
    reducer(actions: UnknownAction[] = [], action) {
      return [...actions, action]
    },
  })

describe('meta', () => {
  let store = getNewStore()

  beforeEach(() => {
    store = getNewStore()
  })

  test('pendingMeta', () => {
    const pendingThunk = createAsyncThunk('test', (arg: string) => {}, {
      getPendingMeta({ arg, requestId }) {
        expect(arg).toBe('testArg')
        expect(requestId).toEqual(expect.any(String))
        return { extraProp: 'foo' }
      },
    })
    const ret = store.dispatch(pendingThunk('testArg'))
    expect(store.getState()[1]).toEqual({
      meta: {
        arg: 'testArg',
        extraProp: 'foo',
        requestId: ret.requestId,
        requestStatus: 'pending',
      },
      payload: undefined,
      type: 'test/pending',
    })
  })

  test('fulfilledMeta', async () => {
    const fulfilledThunk = createAsyncThunk<
      string,
      string,
      { fulfilledMeta: { extraProp: string } }
    >('test', (arg: string, { fulfillWithValue }) => {
      return fulfillWithValue('hooray!', { extraProp: 'bar' })
    })
    const ret = store.dispatch(fulfilledThunk('testArg'))
    expect(await ret).toEqual({
      meta: {
        arg: 'testArg',
        extraProp: 'bar',
        requestId: ret.requestId,
        requestStatus: 'fulfilled',
      },
      payload: 'hooray!',
      type: 'test/fulfilled',
    })
  })

  test('rejectedMeta', async () => {
    const fulfilledThunk = createAsyncThunk<
      string,
      string,
      { rejectedMeta: { extraProp: string } }
    >('test', (arg: string, { rejectWithValue }) => {
      return rejectWithValue('damn!', { extraProp: 'baz' })
    })
    const promise = store.dispatch(fulfilledThunk('testArg'))
    const ret = await promise
    expect(ret).toEqual({
      meta: {
        arg: 'testArg',
        extraProp: 'baz',
        requestId: promise.requestId,
        requestStatus: 'rejected',
        rejectedWithValue: true,
        aborted: false,
        condition: false,
      },
      error: { message: 'Rejected' },
      payload: 'damn!',
      type: 'test/rejected',
    })

    if (ret.meta.requestStatus === 'rejected' && ret.meta.rejectedWithValue) {
    } else {
      // could be caused by a `throw`, `abort()` or `condition` - no `rejectedMeta` in that case
      // @ts-expect-error
      ret.meta.extraProp
    }
  })

  test('typed createAsyncThunk.withTypes', () => {
    const typedCAT = createAsyncThunk.withTypes<{
      state: { s: string }
      rejectValue: string
      extra: { s: string; n: number }
    }>()
    const thunk = typedCAT('a', () => 'b')
    const expectFunction = expect.any(Function)
    expect(thunk.fulfilled).toEqual(expectFunction)
    expect(thunk.pending).toEqual(expectFunction)
    expect(thunk.rejected).toEqual(expectFunction)
    expect(thunk.settled).toEqual(expectFunction)
    expect(thunk.fulfilled.type).toBe('a/fulfilled')
  })
  test('createAsyncThunkWrapper using CreateAsyncThunkFunction', async () => {
    const customSerializeError = () => 'serialized!'
    const createAppAsyncThunk: CreateAsyncThunkFunction<{
      serializedErrorType: ReturnType<typeof customSerializeError>
    }> = (prefix: string, payloadCreator: any, options: any) =>
      createAsyncThunk(prefix, payloadCreator, {
        ...options,
        serializeError: customSerializeError,
      }) as any

    const asyncThunk = createAppAsyncThunk('test', async () => {
      throw new Error('Panic!')
    })

    const promise = store.dispatch(asyncThunk())
    const result = await promise
    if (!asyncThunk.rejected.match(result)) {
      throw new Error('should have thrown')
    }
    expect(result.error).toEqual('serialized!')
  })
})

describe('dispatch config', () => {
  let store = getNewStore()

  beforeEach(() => {
    store = getNewStore()
  })
  test('accepts external signal', async () => {
    const asyncThunk = createAsyncThunk('test', async (_: void, { signal }) => {
      signal.throwIfAborted()
      const { promise, reject } = promiseWithResolvers<never>()
      signal.addEventListener('abort', () => reject(signal.reason))
      return promise
    })

    const abortController = new AbortController()
    const promise = store.dispatch(
      asyncThunk(undefined, { signal: abortController.signal }),
    )
    abortController.abort()
    await expect(promise.unwrap()).rejects.toThrow(
      'External signal was aborted',
    )
  })
  test('handles already aborted external signal', async () => {
    const asyncThunk = createAsyncThunk('test', async (_: void, { signal }) => {
      signal.throwIfAborted()
      const { promise, reject } = promiseWithResolvers<never>()
      signal.addEventListener('abort', () => reject(signal.reason))
      return promise
    })

    const signal = AbortSignal.abort()
    const promise = store.dispatch(asyncThunk(undefined, { signal }))
    await expect(promise.unwrap()).rejects.toThrow(
      'Aborted due to condition callback returning false.',
    )
  })
})