@@ -97,194 +97,189 @@ describe('type tests', () => {
97
97
} )
98
98
} )
99
99
100
- describe ( 'createAction()' , ( ) => {
101
- test ( 'createAction() has type parameter for the action payload.' , ( ) => {
102
- const increment = createAction < number , 'increment' > ( 'increment' )
100
+ test ( 'createAction() has type parameter for the action payload.' , ( ) => {
101
+ const increment = createAction < number , 'increment' > ( 'increment' )
103
102
104
- expectTypeOf ( increment ) . parameter ( 0 ) . toBeNumber ( )
103
+ expectTypeOf ( increment ) . parameter ( 0 ) . toBeNumber ( )
105
104
106
- expectTypeOf ( increment ) . parameter ( 0 ) . not . toBeString ( )
107
- } )
105
+ expectTypeOf ( increment ) . parameter ( 0 ) . not . toBeString ( )
106
+ } )
108
107
109
- test ( 'createAction() type parameter is required, not inferred (defaults to `void`).' , ( ) => {
110
- const increment = createAction ( 'increment' )
108
+ test ( 'createAction() type parameter is required, not inferred (defaults to `void`).' , ( ) => {
109
+ const increment = createAction ( 'increment' )
111
110
112
- expectTypeOf ( increment ) . parameter ( 0 ) . not . toBeNumber ( )
111
+ expectTypeOf ( increment ) . parameter ( 0 ) . not . toBeNumber ( )
113
112
114
- expectTypeOf ( increment ( ) . payload ) . not . toBeNumber ( )
115
- } )
113
+ expectTypeOf ( increment ( ) . payload ) . not . toBeNumber ( )
114
+ } )
116
115
117
- test ( 'createAction().type is a string literal.' , ( ) => {
118
- const increment = createAction < number , 'increment' > ( 'increment' )
116
+ test ( 'createAction().type is a string literal.' , ( ) => {
117
+ const increment = createAction < number , 'increment' > ( 'increment' )
119
118
120
- expectTypeOf ( increment ( 1 ) . type ) . toBeString ( )
119
+ expectTypeOf ( increment ( 1 ) . type ) . toBeString ( )
121
120
122
- expectTypeOf ( increment ( 1 ) . type ) . toEqualTypeOf < 'increment' > ( )
121
+ expectTypeOf ( increment ( 1 ) . type ) . toEqualTypeOf < 'increment' > ( )
123
122
124
- expectTypeOf ( increment ( 1 ) . type ) . not . toMatchTypeOf < 'other' > ( )
123
+ expectTypeOf ( increment ( 1 ) . type ) . not . toMatchTypeOf < 'other' > ( )
125
124
126
- expectTypeOf ( increment ( 1 ) . type ) . not . toBeNumber ( )
127
- } )
125
+ expectTypeOf ( increment ( 1 ) . type ) . not . toBeNumber ( )
126
+ } )
128
127
129
- test ( 'type still present when using prepareAction' , ( ) => {
130
- const strLenAction = createAction ( 'strLen' , ( payload : string ) => ( {
131
- payload : payload . length ,
132
- } ) )
128
+ test ( 'type still present when using prepareAction' , ( ) => {
129
+ const strLenAction = createAction ( 'strLen' , ( payload : string ) => ( {
130
+ payload : payload . length ,
131
+ } ) )
133
132
134
- expectTypeOf ( strLenAction ( 'test' ) . type ) . toBeString ( )
135
- } )
133
+ expectTypeOf ( strLenAction ( 'test' ) . type ) . toBeString ( )
134
+ } )
136
135
137
- test ( 'changing payload type with prepareAction' , ( ) => {
138
- const strLenAction = createAction ( 'strLen' , ( payload : string ) => ( {
139
- payload : payload . length ,
140
- } ) )
136
+ test ( 'changing payload type with prepareAction' , ( ) => {
137
+ const strLenAction = createAction ( 'strLen' , ( payload : string ) => ( {
138
+ payload : payload . length ,
139
+ } ) )
141
140
142
- expectTypeOf ( strLenAction ( 'test' ) . payload ) . toBeNumber ( )
141
+ expectTypeOf ( strLenAction ( 'test' ) . payload ) . toBeNumber ( )
143
142
144
- expectTypeOf ( strLenAction ( 'test' ) . payload ) . not . toBeString ( )
143
+ expectTypeOf ( strLenAction ( 'test' ) . payload ) . not . toBeString ( )
145
144
146
- expectTypeOf ( strLenAction ( 'test' ) ) . not . toHaveProperty ( 'error' )
147
- } )
145
+ expectTypeOf ( strLenAction ( 'test' ) ) . not . toHaveProperty ( 'error' )
146
+ } )
148
147
149
- test ( 'adding metadata with prepareAction' , ( ) => {
150
- const strLenMetaAction = createAction (
151
- 'strLenMeta' ,
152
- ( payload : string ) => ( {
153
- payload,
154
- meta : payload . length ,
155
- } ) ,
156
- )
148
+ test ( 'adding metadata with prepareAction' , ( ) => {
149
+ const strLenMetaAction = createAction ( 'strLenMeta' , ( payload : string ) => ( {
150
+ payload,
151
+ meta : payload . length ,
152
+ } ) )
157
153
158
- expectTypeOf ( strLenMetaAction ( 'test' ) . meta ) . toBeNumber ( )
154
+ expectTypeOf ( strLenMetaAction ( 'test' ) . meta ) . toBeNumber ( )
159
155
160
- expectTypeOf ( strLenMetaAction ( 'test' ) . meta ) . not . toBeString ( )
156
+ expectTypeOf ( strLenMetaAction ( 'test' ) . meta ) . not . toBeString ( )
161
157
162
- expectTypeOf ( strLenMetaAction ( 'test' ) ) . not . toHaveProperty ( 'error' )
163
- } )
158
+ expectTypeOf ( strLenMetaAction ( 'test' ) ) . not . toHaveProperty ( 'error' )
159
+ } )
164
160
165
- test ( 'adding boolean error with prepareAction' , ( ) => {
166
- const boolErrorAction = createAction ( 'boolError' , ( payload : string ) => ( {
167
- payload,
168
- error : true ,
169
- } ) )
161
+ test ( 'adding boolean error with prepareAction' , ( ) => {
162
+ const boolErrorAction = createAction ( 'boolError' , ( payload : string ) => ( {
163
+ payload,
164
+ error : true ,
165
+ } ) )
170
166
171
- expectTypeOf ( boolErrorAction ( 'test' ) . error ) . toBeBoolean ( )
167
+ expectTypeOf ( boolErrorAction ( 'test' ) . error ) . toBeBoolean ( )
172
168
173
- expectTypeOf ( boolErrorAction ( 'test' ) . error ) . not . toBeString ( )
174
- } )
169
+ expectTypeOf ( boolErrorAction ( 'test' ) . error ) . not . toBeString ( )
170
+ } )
175
171
176
- test ( 'adding string error with prepareAction' , ( ) => {
177
- const strErrorAction = createAction ( 'strError' , ( payload : string ) => ( {
178
- payload,
179
- error : 'this is an error' ,
180
- } ) )
172
+ test ( 'adding string error with prepareAction' , ( ) => {
173
+ const strErrorAction = createAction ( 'strError' , ( payload : string ) => ( {
174
+ payload,
175
+ error : 'this is an error' ,
176
+ } ) )
181
177
182
- expectTypeOf ( strErrorAction ( 'test' ) . error ) . toBeString ( )
178
+ expectTypeOf ( strErrorAction ( 'test' ) . error ) . toBeString ( )
183
179
184
- expectTypeOf ( strErrorAction ( 'test' ) . error ) . not . toBeBoolean ( )
185
- } )
180
+ expectTypeOf ( strErrorAction ( 'test' ) . error ) . not . toBeBoolean ( )
181
+ } )
186
182
187
- test ( 'regression test for https://github.com/reduxjs/redux-toolkit/issues/214' , ( ) => {
188
- const action = createAction < { input ?: string } > ( 'ACTION' )
183
+ test ( 'regression test for https://github.com/reduxjs/redux-toolkit/issues/214' , ( ) => {
184
+ const action = createAction < { input ?: string } > ( 'ACTION' )
189
185
190
- expectTypeOf ( action ( { input : '' } ) . payload . input ) . toEqualTypeOf <
191
- string | undefined
192
- > ( )
186
+ expectTypeOf ( action ( { input : '' } ) . payload . input ) . toEqualTypeOf <
187
+ string | undefined
188
+ > ( )
193
189
194
- expectTypeOf ( action ( { input : '' } ) . payload . input ) . not . toBeNumber ( )
190
+ expectTypeOf ( action ( { input : '' } ) . payload . input ) . not . toBeNumber ( )
195
191
196
- expectTypeOf ( action ) . parameter ( 0 ) . not . toMatchTypeOf ( { input : 3 } )
197
- } )
192
+ expectTypeOf ( action ) . parameter ( 0 ) . not . toMatchTypeOf ( { input : 3 } )
193
+ } )
198
194
199
- test ( 'regression test for https://github.com/reduxjs/redux-toolkit/issues/224' , ( ) => {
200
- const oops = createAction ( 'oops' , ( x : any ) => ( {
201
- payload : x ,
202
- error : x ,
203
- meta : x ,
204
- } ) )
195
+ test ( 'regression test for https://github.com/reduxjs/redux-toolkit/issues/224' , ( ) => {
196
+ const oops = createAction ( 'oops' , ( x : any ) => ( {
197
+ payload : x ,
198
+ error : x ,
199
+ meta : x ,
200
+ } ) )
205
201
206
- expectTypeOf ( oops ( '' ) . payload ) . toBeAny ( )
202
+ expectTypeOf ( oops ( '' ) . payload ) . toBeAny ( )
207
203
208
- expectTypeOf ( oops ( '' ) . error ) . toBeAny ( )
204
+ expectTypeOf ( oops ( '' ) . error ) . toBeAny ( )
209
205
210
- expectTypeOf ( oops ( '' ) . meta ) . toBeAny ( )
211
- } )
206
+ expectTypeOf ( oops ( '' ) . meta ) . toBeAny ( )
207
+ } )
212
208
213
- describe ( 'createAction.match()' , ( ) => {
214
- test ( 'simple use case' , ( ) => {
215
- const actionCreator = createAction < string , 'test' > ( 'test' )
209
+ describe ( 'createAction.match()' , ( ) => {
210
+ test ( 'simple use case' , ( ) => {
211
+ const actionCreator = createAction < string , 'test' > ( 'test' )
216
212
217
- const x : Action < string > = { } as any
213
+ const x : Action < string > = { } as any
218
214
219
- if ( actionCreator . match ( x ) ) {
220
- expectTypeOf ( x . type ) . toEqualTypeOf < 'test' > ( )
215
+ if ( actionCreator . match ( x ) ) {
216
+ expectTypeOf ( x . type ) . toEqualTypeOf < 'test' > ( )
221
217
222
- expectTypeOf ( x . payload ) . toBeString ( )
223
- } else {
224
- expectTypeOf ( x . type ) . not . toMatchTypeOf < 'test' > ( )
218
+ expectTypeOf ( x . payload ) . toBeString ( )
219
+ } else {
220
+ expectTypeOf ( x . type ) . not . toMatchTypeOf < 'test' > ( )
225
221
226
- expectTypeOf ( x ) . not . toHaveProperty ( 'payload' )
227
- }
228
- } )
222
+ expectTypeOf ( x ) . not . toHaveProperty ( 'payload' )
223
+ }
224
+ } )
229
225
230
- test ( 'special case: optional argument' , ( ) => {
231
- const actionCreator = createAction < string | undefined , 'test' > ( 'test' )
226
+ test ( 'special case: optional argument' , ( ) => {
227
+ const actionCreator = createAction < string | undefined , 'test' > ( 'test' )
232
228
233
- const x : Action < string > = { } as any
229
+ const x : Action < string > = { } as any
234
230
235
- if ( actionCreator . match ( x ) ) {
236
- expectTypeOf ( x . type ) . toEqualTypeOf < 'test' > ( )
231
+ if ( actionCreator . match ( x ) ) {
232
+ expectTypeOf ( x . type ) . toEqualTypeOf < 'test' > ( )
237
233
238
- expectTypeOf ( x . payload ) . toEqualTypeOf < string | undefined > ( )
239
- }
240
- } )
234
+ expectTypeOf ( x . payload ) . toEqualTypeOf < string | undefined > ( )
235
+ }
236
+ } )
241
237
242
- test ( 'special case: without argument' , ( ) => {
243
- const actionCreator = createAction ( 'test' )
238
+ test ( 'special case: without argument' , ( ) => {
239
+ const actionCreator = createAction ( 'test' )
244
240
245
- const x : Action < string > = { } as any
241
+ const x : Action < string > = { } as any
246
242
247
- if ( actionCreator . match ( x ) ) {
248
- expectTypeOf ( x . type ) . toEqualTypeOf < 'test' > ( )
243
+ if ( actionCreator . match ( x ) ) {
244
+ expectTypeOf ( x . type ) . toEqualTypeOf < 'test' > ( )
249
245
250
- expectTypeOf ( x . payload ) . not . toMatchTypeOf < { } > ( )
251
- }
252
- } )
246
+ expectTypeOf ( x . payload ) . not . toMatchTypeOf < { } > ( )
247
+ }
248
+ } )
253
249
254
- test ( 'special case: with prepareAction' , ( ) => {
255
- const actionCreator = createAction ( 'test' , ( ) => ( {
256
- payload : '' ,
257
- meta : '' ,
258
- error : false ,
259
- } ) )
250
+ test ( 'special case: with prepareAction' , ( ) => {
251
+ const actionCreator = createAction ( 'test' , ( ) => ( {
252
+ payload : '' ,
253
+ meta : '' ,
254
+ error : false ,
255
+ } ) )
260
256
261
- const x : Action < string > = { } as any
257
+ const x : Action < string > = { } as any
262
258
263
- if ( actionCreator . match ( x ) ) {
264
- expectTypeOf ( x . type ) . toEqualTypeOf < 'test' > ( )
259
+ if ( actionCreator . match ( x ) ) {
260
+ expectTypeOf ( x . type ) . toEqualTypeOf < 'test' > ( )
265
261
266
- expectTypeOf ( x . payload ) . toBeString ( )
262
+ expectTypeOf ( x . payload ) . toBeString ( )
267
263
268
- expectTypeOf ( x . meta ) . toBeString ( )
264
+ expectTypeOf ( x . meta ) . toBeString ( )
269
265
270
- expectTypeOf ( x . error ) . toBeBoolean ( )
266
+ expectTypeOf ( x . error ) . toBeBoolean ( )
271
267
272
- expectTypeOf ( x . payload ) . not . toBeNumber ( )
268
+ expectTypeOf ( x . payload ) . not . toBeNumber ( )
273
269
274
- expectTypeOf ( x . meta ) . not . toBeNumber ( )
270
+ expectTypeOf ( x . meta ) . not . toBeNumber ( )
275
271
276
- expectTypeOf ( x . error ) . not . toBeNumber ( )
277
- }
278
- } )
279
- test ( 'potential use: as array filter' , ( ) => {
280
- const actionCreator = createAction < string , 'test' > ( 'test' )
272
+ expectTypeOf ( x . error ) . not . toBeNumber ( )
273
+ }
274
+ } )
275
+ test ( 'potential use: as array filter' , ( ) => {
276
+ const actionCreator = createAction < string , 'test' > ( 'test' )
281
277
282
- const x : Action < string > [ ] = [ ]
278
+ const x : Action < string > [ ] = [ ]
283
279
284
- expectTypeOf ( x . filter ( actionCreator . match ) ) . toEqualTypeOf <
285
- PayloadAction < string , 'test' > [ ]
286
- > ( )
287
- } )
280
+ expectTypeOf ( x . filter ( actionCreator . match ) ) . toEqualTypeOf <
281
+ PayloadAction < string , 'test' > [ ]
282
+ > ( )
288
283
} )
289
284
} )
290
285
0 commit comments