Skip to content

Commit 3c2afe0

Browse files
committed
Convert some expressions and statements to type test assertions in configureStore.test-d.ts
1 parent a5781ee commit 3c2afe0

File tree

1 file changed

+77
-49
lines changed

1 file changed

+77
-49
lines changed

packages/toolkit/src/tests/configureStore.test-d.ts

Lines changed: 77 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,9 @@ describe('type tests', () => {
288288
},
289289
})
290290

291-
const counter1: number = store.getState().counter1
292-
const counter2: number = store.getState().counter2
291+
expectTypeOf(store.getState().counter1).toBeNumber()
292+
293+
expectTypeOf(store.getState().counter2).toBeNumber()
293294
})
294295

295296
test('empty preloaded state', () => {
@@ -301,8 +302,9 @@ describe('type tests', () => {
301302
preloadedState: {},
302303
})
303304

304-
const counter1: number = store.getState().counter1
305-
const counter2: number = store.getState().counter2
305+
expectTypeOf(store.getState().counter1).toBeNumber()
306+
307+
expectTypeOf(store.getState().counter2).toBeNumber()
306308
})
307309

308310
test('excess properties in preloaded state', () => {
@@ -318,8 +320,9 @@ describe('type tests', () => {
318320
},
319321
})
320322

321-
const counter1: number = store.getState().counter1
322-
const counter2: number = store.getState().counter2
323+
expectTypeOf(store.getState().counter1).toBeNumber()
324+
325+
expectTypeOf(store.getState().counter2).toBeNumber()
323326
})
324327

325328
test('mismatching properties in preloaded state', () => {
@@ -334,8 +337,9 @@ describe('type tests', () => {
334337
},
335338
})
336339

337-
const counter1: number = store.getState().counter1
338-
const counter2: number = store.getState().counter2
340+
expectTypeOf(store.getState().counter1).toBeNumber()
341+
342+
expectTypeOf(store.getState().counter2).toBeNumber()
339343
})
340344

341345
test('string preloaded state when expecting object', () => {
@@ -348,8 +352,9 @@ describe('type tests', () => {
348352
preloadedState: 'test',
349353
})
350354

351-
const counter1: number = store.getState().counter1
352-
const counter2: number = store.getState().counter2
355+
expectTypeOf(store.getState().counter1).toBeNumber()
356+
357+
expectTypeOf(store.getState().counter2).toBeNumber()
353358
})
354359

355360
test('nested combineReducers allows partial', () => {
@@ -371,10 +376,13 @@ describe('type tests', () => {
371376
},
372377
})
373378

374-
const group1counter1: number = store.getState().group1.counter1
375-
const group1counter2: number = store.getState().group1.counter2
376-
const group2counter1: number = store.getState().group2.counter1
377-
const group2counter2: number = store.getState().group2.counter2
379+
expectTypeOf(store.getState().group1.counter1).toBeNumber()
380+
381+
expectTypeOf(store.getState().group1.counter2).toBeNumber()
382+
383+
expectTypeOf(store.getState().group2.counter1).toBeNumber()
384+
385+
expectTypeOf(store.getState().group2.counter2).toBeNumber()
378386
})
379387

380388
test('non-nested combineReducers does not allow partial', () => {
@@ -401,10 +409,13 @@ describe('type tests', () => {
401409
},
402410
})
403411

404-
const group1counter1: number = store.getState().group1.counter1
405-
const group1counter2: number = store.getState().group1.counter2
406-
const group2counter1: number = store.getState().group2.counter1
407-
const group2counter2: number = store.getState().group2.counter2
412+
expectTypeOf(store.getState().group1.counter1).toBeNumber()
413+
414+
expectTypeOf(store.getState().group1.counter2).toBeNumber()
415+
416+
expectTypeOf(store.getState().group2.counter1).toBeNumber()
417+
418+
expectTypeOf(store.getState().group2.counter2).toBeNumber()
408419
})
409420
})
410421

@@ -416,7 +427,7 @@ describe('type tests', () => {
416427
}
417428

418429
type StateB = string
419-
function thunkB() {
430+
const thunkB = () => {
420431
return (dispatch: Dispatch, getState: () => StateB) => {}
421432
}
422433

@@ -518,9 +529,10 @@ describe('type tests', () => {
518529
middleware: () =>
519530
new Tuple(0 as unknown as Middleware<(a: StateA) => boolean, StateA>),
520531
})
521-
const result: boolean = store.dispatch(5)
522-
// @ts-expect-error
523-
const result2: string = store.dispatch(5)
532+
533+
expectTypeOf(store.dispatch(5)).toBeBoolean()
534+
535+
expectTypeOf(store.dispatch(5)).not.toBeString()
524536
})
525537

526538
test('multiple custom middleware', () => {
@@ -531,14 +543,17 @@ describe('type tests', () => {
531543
ThunkMiddleware<StateA>,
532544
]
533545
>
546+
534547
const store = configureStore({
535548
reducer: reducerA,
536549
middleware: () => middleware,
537550
})
538551

539-
const result: 'A' = store.dispatch('a')
540-
const result2: 'B' = store.dispatch('b')
541-
const result3: Promise<'A'> = store.dispatch(thunkA())
552+
expectTypeOf(store.dispatch('a')).toEqualTypeOf<'A'>()
553+
554+
expectTypeOf(store.dispatch('b')).toEqualTypeOf<'B'>()
555+
556+
expectTypeOf(store.dispatch(thunkA())).toEqualTypeOf<Promise<'A'>>()
542557
})
543558

544559
test('Accepts thunk with `unknown`, `undefined` or `null` ThunkAction extraArgument per default', () => {
@@ -587,10 +602,11 @@ describe('type tests', () => {
587602
>),
588603
})
589604

590-
const result1: 'A' = store.dispatch('a')
591-
const result2: Promise<'A'> = store.dispatch(thunkA())
592-
// @ts-expect-error
593-
store.dispatch(thunkB())
605+
expectTypeOf(store.dispatch('a')).toEqualTypeOf<'A'>()
606+
607+
expectTypeOf(store.dispatch(thunkA())).toEqualTypeOf<Promise<'A'>>()
608+
609+
expectTypeOf(store.dispatch).parameter(0).not.toMatchTypeOf(thunkB())
594610
})
595611

596612
test('custom middleware and getDefaultMiddleware, using prepend', () => {
@@ -611,10 +627,12 @@ describe('type tests', () => {
611627
return concatenated
612628
},
613629
})
614-
const result1: 'A' = store.dispatch('a')
615-
const result2: Promise<'A'> = store.dispatch(thunkA())
616-
// @ts-expect-error
617-
store.dispatch(thunkB())
630+
631+
expectTypeOf(store.dispatch('a')).toEqualTypeOf<'A'>()
632+
633+
expectTypeOf(store.dispatch(thunkA())).toEqualTypeOf<Promise<'A'>>()
634+
635+
expectTypeOf(store.dispatch).parameter(0).not.toMatchTypeOf(thunkB())
618636
})
619637

620638
test('custom middleware and getDefaultMiddleware, using concat', () => {
@@ -635,10 +653,12 @@ describe('type tests', () => {
635653
return concatenated
636654
},
637655
})
638-
const result1: 'A' = store.dispatch('a')
639-
const result2: Promise<'A'> = store.dispatch(thunkA())
640-
// @ts-expect-error
641-
store.dispatch(thunkB())
656+
657+
expectTypeOf(store.dispatch('a')).toEqualTypeOf<'A'>()
658+
659+
expectTypeOf(store.dispatch(thunkA())).toEqualTypeOf<Promise<'A'>>()
660+
661+
expectTypeOf(store.dispatch).parameter(0).not.toMatchTypeOf(thunkB())
642662
})
643663

644664
test('middlewareBuilder notation, getDefaultMiddleware (unconfigured)', () => {
@@ -650,29 +670,36 @@ describe('type tests', () => {
650670
StateA
651671
>),
652672
})
653-
const result1: 'A' = store.dispatch('a')
654-
const result2: Promise<'A'> = store.dispatch(thunkA())
655-
// @ts-expect-error
656-
store.dispatch(thunkB())
673+
674+
expectTypeOf(store.dispatch('a')).toEqualTypeOf<'A'>()
675+
676+
expectTypeOf(store.dispatch(thunkA())).toEqualTypeOf<Promise<'A'>>()
677+
678+
expectTypeOf(store.dispatch).parameter(0).not.toMatchTypeOf(thunkB())
657679
})
658680

659681
test('middlewareBuilder notation, getDefaultMiddleware, concat & prepend', () => {
660682
const otherMiddleware: Middleware<(a: 'a') => 'A', StateA> =
661683
_anyMiddleware
684+
662685
const otherMiddleware2: Middleware<(a: 'b') => 'B', StateA> =
663686
_anyMiddleware
687+
664688
const store = configureStore({
665689
reducer: reducerA,
666690
middleware: (getDefaultMiddleware) =>
667691
getDefaultMiddleware()
668692
.concat(otherMiddleware)
669693
.prepend(otherMiddleware2),
670694
})
671-
const result1: 'A' = store.dispatch('a')
672-
const result2: Promise<'A'> = store.dispatch(thunkA())
673-
const result3: 'B' = store.dispatch('b')
674-
// @ts-expect-error
675-
store.dispatch(thunkB())
695+
696+
expectTypeOf(store.dispatch('a')).toEqualTypeOf<'A'>()
697+
698+
expectTypeOf(store.dispatch(thunkA())).toEqualTypeOf<Promise<'A'>>()
699+
700+
expectTypeOf(store.dispatch('b')).toEqualTypeOf<'B'>()
701+
702+
expectTypeOf(store.dispatch).parameter(0).not.toMatchTypeOf(thunkB())
676703
})
677704

678705
test('middlewareBuilder notation, getDefaultMiddleware (thunk: false)', () => {
@@ -683,9 +710,10 @@ describe('type tests', () => {
683710
(() => {}) as any as Middleware<(a: 'a') => 'A', StateA>,
684711
),
685712
})
686-
const result1: 'A' = store.dispatch('a')
687-
// @ts-expect-error
688-
store.dispatch(thunkA())
713+
714+
expectTypeOf(store.dispatch('a')).toEqualTypeOf<'A'>()
715+
716+
expectTypeOf(store.dispatch).parameter(0).not.toMatchTypeOf(thunkA())
689717
})
690718

691719
test("badly typed middleware won't make `dispatch` `any`", () => {

0 commit comments

Comments
 (0)