@@ -20,182 +20,170 @@ const defaultDispatch = (() => {}) as ThunkDispatch<{}, any, UnknownAction>
20
20
const unknownAction = { type : 'foo' } as UnknownAction
21
21
22
22
describe ( 'type tests' , ( ) => {
23
- test ( 'basic usage' , ( ) => {
24
- ; ( async function ( ) {
25
- const asyncThunk = createAsyncThunk ( 'test' , ( id : number ) =>
26
- Promise . resolve ( id * 2 ) ,
27
- )
23
+ test ( 'basic usage' , async ( ) => {
24
+ const asyncThunk = createAsyncThunk ( 'test' , ( id : number ) =>
25
+ Promise . resolve ( id * 2 ) ,
26
+ )
28
27
29
- const reducer = createReducer ( { } , ( builder ) =>
30
- builder
31
- . addCase ( asyncThunk . pending , ( _ , action ) => {
32
- expectTypeOf ( action ) . toEqualTypeOf <
33
- ReturnType < ( typeof asyncThunk ) [ 'pending' ] >
34
- > ( )
35
- } )
28
+ const reducer = createReducer ( { } , ( builder ) =>
29
+ builder
30
+ . addCase ( asyncThunk . pending , ( _ , action ) => {
31
+ expectTypeOf ( action ) . toEqualTypeOf <
32
+ ReturnType < ( typeof asyncThunk ) [ 'pending' ] >
33
+ > ( )
34
+ } )
36
35
37
- . addCase ( asyncThunk . fulfilled , ( _ , action ) => {
38
- expectTypeOf ( action ) . toEqualTypeOf <
39
- ReturnType < ( typeof asyncThunk ) [ 'fulfilled' ] >
40
- > ( )
36
+ . addCase ( asyncThunk . fulfilled , ( _ , action ) => {
37
+ expectTypeOf ( action ) . toEqualTypeOf <
38
+ ReturnType < ( typeof asyncThunk ) [ 'fulfilled' ] >
39
+ > ( )
41
40
42
- expectTypeOf ( action . payload ) . toBeNumber ( )
43
- } )
41
+ expectTypeOf ( action . payload ) . toBeNumber ( )
42
+ } )
44
43
45
- . addCase ( asyncThunk . rejected , ( _ , action ) => {
46
- expectTypeOf ( action ) . toEqualTypeOf <
47
- ReturnType < ( typeof asyncThunk ) [ 'rejected' ] >
48
- > ( )
44
+ . addCase ( asyncThunk . rejected , ( _ , action ) => {
45
+ expectTypeOf ( action ) . toEqualTypeOf <
46
+ ReturnType < ( typeof asyncThunk ) [ 'rejected' ] >
47
+ > ( )
49
48
50
- expectTypeOf ( action . error ) . toMatchTypeOf <
51
- Partial < Error > | undefined
52
- > ( )
53
- } ) ,
54
- )
49
+ expectTypeOf ( action . error ) . toMatchTypeOf < Partial < Error > | undefined > ( )
50
+ } ) ,
51
+ )
55
52
56
- const promise = defaultDispatch ( asyncThunk ( 3 ) )
53
+ const promise = defaultDispatch ( asyncThunk ( 3 ) )
57
54
58
- expectTypeOf ( promise . requestId ) . toBeString ( )
55
+ expectTypeOf ( promise . requestId ) . toBeString ( )
59
56
60
- expectTypeOf ( promise . arg ) . toBeNumber ( )
57
+ expectTypeOf ( promise . arg ) . toBeNumber ( )
61
58
62
- expectTypeOf ( promise . abort ) . toEqualTypeOf < ( reason ?: string ) => void > ( )
59
+ expectTypeOf ( promise . abort ) . toEqualTypeOf < ( reason ?: string ) => void > ( )
63
60
64
- const result = await promise
61
+ const result = await promise
65
62
66
- if ( asyncThunk . fulfilled . match ( result ) ) {
67
- expectTypeOf ( result ) . toEqualTypeOf <
68
- ReturnType < ( typeof asyncThunk ) [ 'fulfilled' ] >
69
- > ( )
70
- } else {
71
- expectTypeOf ( result ) . toEqualTypeOf <
72
- ReturnType < ( typeof asyncThunk ) [ 'rejected' ] >
73
- > ( )
74
- }
63
+ if ( asyncThunk . fulfilled . match ( result ) ) {
64
+ expectTypeOf ( result ) . toEqualTypeOf <
65
+ ReturnType < ( typeof asyncThunk ) [ 'fulfilled' ] >
66
+ > ( )
67
+ } else {
68
+ expectTypeOf ( result ) . toEqualTypeOf <
69
+ ReturnType < ( typeof asyncThunk ) [ 'rejected' ] >
70
+ > ( )
71
+ }
75
72
76
- promise
77
- . then ( unwrapResult )
78
- . then ( ( result ) => {
79
- expectTypeOf ( result ) . toBeNumber ( )
73
+ promise
74
+ . then ( unwrapResult )
75
+ . then ( ( result ) => {
76
+ expectTypeOf ( result ) . toBeNumber ( )
80
77
81
- expectTypeOf ( result ) . not . toMatchTypeOf < Error > ( )
82
- } )
83
- . catch ( ( error ) => {
84
- // catch is always any-typed, nothing we can do here
85
- expectTypeOf ( error ) . toBeAny ( )
86
- } )
87
- } ) ( )
78
+ expectTypeOf ( result ) . not . toMatchTypeOf < Error > ( )
79
+ } )
80
+ . catch ( ( error ) => {
81
+ // catch is always any-typed, nothing we can do here
82
+ expectTypeOf ( error ) . toBeAny ( )
83
+ } )
88
84
} )
89
85
90
86
test ( 'More complex usage of thunk args' , ( ) => {
91
- ; ( async function ( ) {
92
- interface BookModel {
93
- id : string
94
- title : string
95
- }
87
+ interface BookModel {
88
+ id : string
89
+ title : string
90
+ }
96
91
97
- type BooksState = BookModel [ ]
98
-
99
- const fakeBooks : BookModel [ ] = [
100
- { id : 'b' , title : 'Second' } ,
101
- { id : 'a' , title : 'First' } ,
102
- ]
103
-
104
- const correctDispatch = ( ( ) => { } ) as ThunkDispatch <
105
- BookModel [ ] ,
106
- { userAPI : Function } ,
107
- UnknownAction
108
- >
109
-
110
- // Verify that the the first type args to createAsyncThunk line up right
111
- const fetchBooksTAC = createAsyncThunk <
112
- BookModel [ ] ,
113
- number ,
114
- {
115
- state : BooksState
116
- extra : { userAPI : Function }
117
- }
118
- > (
119
- 'books/fetch' ,
120
- async ( arg , { getState, dispatch, extra, requestId, signal } ) => {
121
- const state = getState ( )
92
+ type BooksState = BookModel [ ]
122
93
123
- expectTypeOf ( arg ) . toBeNumber ( )
94
+ const fakeBooks : BookModel [ ] = [
95
+ { id : 'b' , title : 'Second' } ,
96
+ { id : 'a' , title : 'First' } ,
97
+ ]
124
98
125
- expectTypeOf ( state ) . toEqualTypeOf < BookModel [ ] > ( )
99
+ const correctDispatch = ( ( ) => { } ) as ThunkDispatch <
100
+ BookModel [ ] ,
101
+ { userAPI : Function } ,
102
+ UnknownAction
103
+ >
126
104
127
- expectTypeOf ( extra ) . toEqualTypeOf < { userAPI : Function } > ( )
105
+ // Verify that the the first type args to createAsyncThunk line up right
106
+ const fetchBooksTAC = createAsyncThunk <
107
+ BookModel [ ] ,
108
+ number ,
109
+ {
110
+ state : BooksState
111
+ extra : { userAPI : Function }
112
+ }
113
+ > (
114
+ 'books/fetch' ,
115
+ async ( arg , { getState, dispatch, extra, requestId, signal } ) => {
116
+ const state = getState ( )
128
117
129
- return fakeBooks
130
- } ,
131
- )
118
+ expectTypeOf ( arg ) . toBeNumber ( )
132
119
133
- correctDispatch ( fetchBooksTAC ( 1 ) )
134
- // @ts -expect-error
135
- defaultDispatch ( fetchBooksTAC ( 1 ) )
136
- } ) ( )
137
- } )
120
+ expectTypeOf ( state ) . toEqualTypeOf < BookModel [ ] > ( )
138
121
139
- test ( 'returning a rejected action from the promise creator is possible' , ( ) => {
140
- ; ( async ( ) => {
141
- type ReturnValue = { data : 'success' }
142
- type RejectValue = { data : 'error' }
122
+ expectTypeOf ( extra ) . toEqualTypeOf < { userAPI : Function } > ( )
143
123
144
- const fetchBooksTAC = createAsyncThunk <
145
- ReturnValue ,
146
- number ,
147
- {
148
- rejectValue : RejectValue
149
- }
150
- > ( 'books/fetch' , async ( arg , { rejectWithValue } ) => {
151
- return rejectWithValue ( { data : 'error' } )
152
- } )
124
+ return fakeBooks
125
+ } ,
126
+ )
153
127
154
- const returned = await defaultDispatch ( fetchBooksTAC ( 1 ) )
155
- if ( fetchBooksTAC . rejected . match ( returned ) ) {
156
- expectTypeOf ( returned . payload ) . toEqualTypeOf < undefined | RejectValue > ( )
128
+ correctDispatch ( fetchBooksTAC ( 1 ) )
129
+ // @ts -expect-error
130
+ defaultDispatch ( fetchBooksTAC ( 1 ) )
131
+ } )
157
132
158
- expectTypeOf ( returned . payload ) . toBeNullable ( )
159
- } else {
160
- expectTypeOf ( returned . payload ) . toEqualTypeOf < ReturnValue > ( )
133
+ test ( 'returning a rejected action from the promise creator is possible' , async ( ) => {
134
+ type ReturnValue = { data : 'success' }
135
+ type RejectValue = { data : 'error' }
136
+
137
+ const fetchBooksTAC = createAsyncThunk <
138
+ ReturnValue ,
139
+ number ,
140
+ {
141
+ rejectValue : RejectValue
161
142
}
143
+ > ( 'books/fetch' , async ( arg , { rejectWithValue } ) => {
144
+ return rejectWithValue ( { data : 'error' } )
145
+ } )
162
146
163
- expectTypeOf ( unwrapResult ( returned ) ) . toEqualTypeOf < ReturnValue > ( )
147
+ const returned = await defaultDispatch ( fetchBooksTAC ( 1 ) )
148
+ if ( fetchBooksTAC . rejected . match ( returned ) ) {
149
+ expectTypeOf ( returned . payload ) . toEqualTypeOf < undefined | RejectValue > ( )
164
150
165
- expectTypeOf ( unwrapResult ( returned ) ) . not . toMatchTypeOf < RejectValue > ( )
166
- } ) ( )
151
+ expectTypeOf ( returned . payload ) . toBeNullable ( )
152
+ } else {
153
+ expectTypeOf ( returned . payload ) . toEqualTypeOf < ReturnValue > ( )
154
+ }
155
+
156
+ expectTypeOf ( unwrapResult ( returned ) ) . toEqualTypeOf < ReturnValue > ( )
157
+
158
+ expectTypeOf ( unwrapResult ( returned ) ) . not . toMatchTypeOf < RejectValue > ( )
167
159
} )
168
160
169
161
test ( 'regression #1156: union return values fall back to allowing only single member' , ( ) => {
170
- ; ( async ( ) => {
171
- const fn = createAsyncThunk ( 'session/isAdmin' , async ( ) => {
172
- const response : boolean = false
173
- return response
174
- } )
175
- } ) ( )
162
+ const fn = createAsyncThunk ( 'session/isAdmin' , async ( ) => {
163
+ const response : boolean = false
164
+ return response
165
+ } )
176
166
} )
177
167
178
168
test ( 'Should handle reject with value within a try catch block. Note: this is a sample code taken from #1605' , ( ) => {
179
- ; ( async ( ) => {
180
- type ResultType = {
181
- text : string
169
+ type ResultType = {
170
+ text : string
171
+ }
172
+ const demoPromise = async ( ) : Promise < ResultType > =>
173
+ new Promise ( ( resolve , _ ) => resolve ( { text : '' } ) )
174
+ const thunk = createAsyncThunk ( 'thunk' , async ( args , thunkAPI ) => {
175
+ try {
176
+ const result = await demoPromise ( )
177
+ return result
178
+ } catch ( error ) {
179
+ return thunkAPI . rejectWithValue ( error )
182
180
}
183
- const demoPromise = async ( ) : Promise < ResultType > =>
184
- new Promise ( ( resolve , _ ) => resolve ( { text : '' } ) )
185
- const thunk = createAsyncThunk ( 'thunk' , async ( args , thunkAPI ) => {
186
- try {
187
- const result = await demoPromise ( )
188
- return result
189
- } catch ( error ) {
190
- return thunkAPI . rejectWithValue ( error )
191
- }
192
- } )
193
- createReducer ( { } , ( builder ) =>
194
- builder . addCase ( thunk . fulfilled , ( s , action ) => {
195
- expectTypeOf ( action . payload ) . toEqualTypeOf < ResultType > ( )
196
- } ) ,
197
- )
198
- } ) ( )
181
+ } )
182
+ createReducer ( { } , ( builder ) =>
183
+ builder . addCase ( thunk . fulfilled , ( s , action ) => {
184
+ expectTypeOf ( action . payload ) . toEqualTypeOf < ResultType > ( )
185
+ } ) ,
186
+ )
199
187
} )
200
188
201
189
test ( 'reject with value' , ( ) => {
0 commit comments