Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate type tests to Vitest #4127

Merged
merged 79 commits into from
Jan 29, 2024
Merged
Changes from 1 commit
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
5994f5f
Rename `src/dynamicMiddleware/tests/index.typetest.ts` to `src/dynami…
aryaemami59 Jan 26, 2024
799b20b
Migrate type tests for `src\dynamicMiddleware\tests\index.test-d.ts` …
aryaemami59 Jan 26, 2024
acb46b5
Rename `src/dynamicMiddleware/tests/react.typetest.ts` to `src/dynami…
aryaemami59 Jan 26, 2024
70af524
Migrate type tests for `src\dynamicMiddleware\tests\react.test-d.ts` …
aryaemami59 Jan 26, 2024
ad884ed
Rename `src/query/tests/baseQueryTypes.typetest.ts` to `src/query/tes…
aryaemami59 Jan 26, 2024
b8f6f81
Migrate type tests for `src\query\tests\baseQueryTypes.test-d.ts` to …
aryaemami59 Jan 26, 2024
9928268
Rename `src/tests/combineSlices.typetest.ts` to `src/tests/combineSli…
aryaemami59 Jan 26, 2024
e3a6d20
Migrate type tests for `src/tests/combineSlices.test-d.ts` to Vitest
aryaemami59 Jan 26, 2024
505ee08
Rename `src/tests/configureStore.typetest.ts` to `src/tests/configure…
aryaemami59 Jan 26, 2024
6fbf1bd
Migrate type tests for `src/tests/configureStore.test-d.ts` to Vitest
aryaemami59 Jan 26, 2024
6d577ad
Rename `src/tests/createAsyncThunk.typetest.ts` to `src/tests/createA…
aryaemami59 Jan 26, 2024
293c669
Migrate type tests for `src/tests/createAsyncThunk.test-d.ts` to Vitest
aryaemami59 Jan 27, 2024
bf09e94
Rename `src/tests/createEntityAdapter.typetest.ts` to `src/tests/crea…
aryaemami59 Jan 27, 2024
7a7fc95
Migrate type tests for `src/tests/createEntityAdapter.test-d.ts` to V…
aryaemami59 Jan 27, 2024
1ac69c1
Rename `src/tests/createReducer.typetest.ts` to `src/tests/createRedu…
aryaemami59 Jan 27, 2024
6d309df
Migrate type tests for `src/tests/createReducer.test-d.ts` to Vitest
aryaemami59 Jan 27, 2024
c3c7dbf
Rename `src/tests/createSlice.typetest.ts` to `src/tests/createSlice.…
aryaemami59 Jan 27, 2024
cfa8466
Migrate type tests for `src/tests/createSlice.test-d.ts` to Vitest
aryaemami59 Jan 27, 2024
1cdfa79
Rename `src/tests/getDefaultEnhancers.typetest.ts` to `src/tests/getD…
aryaemami59 Jan 27, 2024
0bd705c
Migrate type tests for `src/tests/getDefaultEnhancers.test-d.ts` to V…
aryaemami59 Jan 27, 2024
10d3add
Rename `src/tests/getDefaultMiddleware.typetest.ts` to `src/tests/get…
aryaemami59 Jan 27, 2024
7d7e219
Migrate type tests for `src/tests/getDefaultMiddleware.test-d.ts` to …
aryaemami59 Jan 27, 2024
1b6923b
Rename `src/tests/mapBuilders.typetest.ts` to `src/tests/mapBuilders.…
aryaemami59 Jan 27, 2024
4ba50e2
Migrate type tests for `src/tests/mapBuilders.test-d.ts` to Vitest
aryaemami59 Jan 27, 2024
7ffb614
Rename `src/tests/matchers.typetest.ts` to `src/tests/matchers.test-d…
aryaemami59 Jan 27, 2024
efea226
Migrate type tests for `src/tests/matchers.test-d.ts` to Vitest
aryaemami59 Jan 27, 2024
bb9aecf
Rename `src/tests/Tuple.typetest.ts` to `src/tests/Tuple.test-d.ts`
aryaemami59 Jan 27, 2024
d109c3c
Add `@ts-ignore` to `createAsyncThunk.test-d.ts` TS 4.7 issues
aryaemami59 Jan 27, 2024
008230b
Migrate type tests for `src/tests/Tuple.test-d.ts` to Vitest
aryaemami59 Jan 27, 2024
1b7a714
Migrate type tests for `src/query/tests/buildHooks.test-d.tsx` to Vitest
aryaemami59 Jan 27, 2024
3f0f097
Move type tests inside `listenerMiddleware.test.ts` to `listenerMiddl…
aryaemami59 Jan 28, 2024
6b02a91
Fix type tests inside `mapBuilders.test-d.ts`
aryaemami59 Jan 28, 2024
94814b6
Merge branch 'master' of https://github.com/reduxjs/redux-toolkit int…
aryaemami59 Jan 28, 2024
59f451d
Move the type tests inside `createApi.test.ts` into `createApi.test-d…
aryaemami59 Jan 28, 2024
50df02d
Move the type tests inside `cacheLifecycle.test.ts` into `cacheLifecy…
aryaemami59 Jan 28, 2024
9f7cf97
Move the type tests inside `errorHandling.test.tsx` into `errorHandli…
aryaemami59 Jan 28, 2024
f7dbd7e
Move the type tests inside `getDefaultMiddleware.test.ts` into `getDe…
aryaemami59 Jan 28, 2024
8025f61
Move the type tests inside `createAsyncThunk.test.ts` into `createAsy…
aryaemami59 Jan 28, 2024
56c3bc5
Move the type tests inside `matchers.test.tsx` into `matchers.test-d.…
aryaemami59 Jan 28, 2024
1a98378
Move type tests inside `queryLifecycle.test.tsx` into `queryLifecycle…
aryaemami59 Jan 28, 2024
5e2a47e
Move type tests from `combineSlices.test.ts` into `combineSlices.test…
aryaemami59 Jan 28, 2024
6b533d6
Rename `src/tests/createAction.typetest.tsx` to `src/tests/createActi…
aryaemami59 Jan 28, 2024
af3511d
Migrate type tests for `createAction.test-d.tsx` to Vitest
aryaemami59 Jan 28, 2024
672cc5e
Remove `typeTestHelpers.ts`
aryaemami59 Jan 28, 2024
209d0d9
Cut excessive assertions in `Tuple.test-d.ts`
aryaemami59 Jan 28, 2024
c2abc20
Cut excessive assertions in `combineSlices.test-d.ts`
aryaemami59 Jan 28, 2024
9e0cb1a
Cut excessive assertions in `configureStore.test-d.ts`
aryaemami59 Jan 28, 2024
d5155bd
Cut excessive assertions in `mapBuilders.test-d.ts`
aryaemami59 Jan 28, 2024
4042506
Cut excessive assertions in `buildHooks.test-d.tsx`
aryaemami59 Jan 28, 2024
0724a36
Cut excessive assertions in `createSlice.test-d.ts`
aryaemami59 Jan 28, 2024
9669753
Cut excessive assertions in `createEntityAdapter.test-d.ts`
aryaemami59 Jan 28, 2024
ba94b0a
Cut excessive assertions in `createAsyncThunk.test-d.ts`
aryaemami59 Jan 28, 2024
2a70d74
Cut excessive assertions in `unionTypes.test-d.ts`
aryaemami59 Jan 28, 2024
bf6bd48
Cut excessive assertions in `configureStore.test-d.ts`
aryaemami59 Jan 28, 2024
18a6b2a
Cut excessive assertions in `createAction.test-d.tsx`
aryaemami59 Jan 28, 2024
7713856
Fix issue with importing `endpointDefinitions` inside `createApi.test…
aryaemami59 Jan 28, 2024
f6d812c
Remove nested `describe` block from `createAction.test-d.tsx`
aryaemami59 Jan 28, 2024
c76527d
Add missing type import in `retry.test-d.ts`
aryaemami59 Jan 28, 2024
2518b10
Fix remaining type test issues
aryaemami59 Jan 28, 2024
50456ba
Fix `endpointDefinitions` type imports in `createApi.test.ts`
aryaemami59 Jan 28, 2024
407bcf5
Fix issue with importing `Tuple` in `getDefaultMiddleware.test.ts`
aryaemami59 Jan 28, 2024
ebb0637
Fix type issue in `getDefaultMiddleware.test-d.ts`
aryaemami59 Jan 28, 2024
b1071f6
remove (now inaccurate) TODOs
EskiMojo14 Jan 28, 2024
819e241
Merge branch 'master' of https://github.com/reduxjs/redux-toolkit int…
aryaemami59 Jan 29, 2024
c231da4
Remove redundant `returns.resolves` assertion
aryaemami59 Jan 29, 2024
9339d5c
Remove unnecessary `.then` blocks
aryaemami59 Jan 29, 2024
50e9640
Remove unnecessary `if` blocks
aryaemami59 Jan 29, 2024
a6a7504
Fix broken assertion
aryaemami59 Jan 29, 2024
8577395
Remove unnecessary `test` block
aryaemami59 Jan 29, 2024
d98757f
Remove IIFE's
aryaemami59 Jan 29, 2024
8bba2ee
Replace `parameters.items` with `parameter(0)`
aryaemami59 Jan 29, 2024
8b331ba
Fix type annotation
aryaemami59 Jan 29, 2024
38eff96
Replace `@ts-expect-error`s with `expectTypeOf` assertions
aryaemami59 Jan 29, 2024
a5781ee
Remove redundant assertion
aryaemami59 Jan 29, 2024
3c2afe0
Convert some expressions and statements to type test assertions in `…
aryaemami59 Jan 29, 2024
5ecd984
Add `tsconfig.vitest-temp.json` to `.gitignore`
aryaemami59 Jan 29, 2024
5a43739
Merge branch 'master' of https://github.com/reduxjs/redux-toolkit int…
aryaemami59 Jan 29, 2024
3418c5a
Fix `OverrideResultType` imports in test files
aryaemami59 Jan 29, 2024
77511c3
Replace `dist` imports with `@internal` imports
aryaemami59 Jan 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Migrate type tests for src/tests/combineSlices.test-d.ts to Vitest
aryaemami59 committed Jan 26, 2024
commit e3a6d209d6e76937ca101f7f1cb494d5542f379d
329 changes: 163 additions & 166 deletions packages/toolkit/src/tests/combineSlices.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* eslint-disable no-lone-blocks */
import type { Reducer, Slice, WithSlice } from '@reduxjs/toolkit'
import { combineSlices } from '@reduxjs/toolkit'
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'
import { expectExactType, expectType } from './utils/typeTestHelpers'

declare const stringSlice: Slice<string, {}, 'string'>

@@ -21,70 +19,6 @@ const exampleApi = createApi({

type ExampleApiState = ReturnType<typeof exampleApi.reducer>

/**
* Test: combineSlices correctly combines static state
*/
{
const rootReducer = combineSlices(stringSlice, numberSlice, exampleApi, {
boolean: booleanReducer,
})
expectType<{
string: string
number: number
boolean: boolean
api: ExampleApiState
}>(rootReducer(undefined, { type: '' }))
}

/**
* Test: withLazyLoadedSlices adds partial to state
*/
{
const rootReducer = combineSlices(stringSlice).withLazyLoadedSlices<
WithSlice<typeof numberSlice> & WithSlice<typeof exampleApi>
>()
expectExactType<number | undefined>(0)(
rootReducer(undefined, { type: '' }).number
)
expectExactType<ExampleApiState | undefined>(undefined)(
rootReducer(undefined, { type: '' }).api
)
}

/**
* Test: inject marks injected keys as required
*/
{
const rootReducer = combineSlices(stringSlice).withLazyLoadedSlices<
WithSlice<typeof numberSlice> &
WithSlice<typeof exampleApi> & { boolean: boolean }
>()

expectExactType<number | undefined>(0)(
rootReducer(undefined, { type: '' }).number
)
expectExactType<boolean | undefined>(true)(
rootReducer(undefined, { type: '' }).boolean
)
expectExactType<ExampleApiState | undefined>(undefined)(
rootReducer(undefined, { type: '' }).api
)

const withNumber = rootReducer.inject(numberSlice)
expectExactType<number>(0)(withNumber(undefined, { type: '' }).number)

const withBool = rootReducer.inject({
reducerPath: 'boolean' as const,
reducer: booleanReducer,
})
expectExactType<boolean>(true)(withBool(undefined, { type: '' }).boolean)

const withApi = rootReducer.inject(exampleApi)
expectExactType<ExampleApiState>({} as ExampleApiState)(
withApi(undefined, { type: '' }).api
)
}

declare const wrongNumberSlice: Slice<string, {}, 'number'>

declare const wrongBooleanReducer: Reducer<number>
@@ -98,113 +32,176 @@ const wrongApi = createApi({
}),
})

/**
* Test: selector() allows defining selectors with injected reducers defined
*/
{
const rootReducer = combineSlices(stringSlice).withLazyLoadedSlices<
WithSlice<typeof numberSlice> & { boolean: boolean }
>()

type RootState = ReturnType<typeof rootReducer>

const withoutInjection = rootReducer.selector(
(state: RootState) => state.number
)

expectExactType<number | undefined>(0)(
withoutInjection(rootReducer(undefined, { type: '' }))
)

const withInjection = rootReducer
.inject(numberSlice)
.selector((state) => state.number)

expectExactType<number>(0)(
withInjection(rootReducer(undefined, { type: '' }))
)
}

/**
* Test: selector() passes arguments through
*/
{
const rootReducer = combineSlices(stringSlice).withLazyLoadedSlices<
WithSlice<typeof numberSlice> & { boolean: boolean }
>()

const selector = rootReducer
.inject(numberSlice)
.selector((state, num: number) => state.number)

const state = rootReducer(undefined, { type: '' })
// @ts-expect-error required argument
selector(state)
// @ts-expect-error number not string
selector(state, '')
selector(state, 0)
}

/**
* Test: nested calls inferred correctly
*/
{
const innerReducer =
combineSlices(stringSlice).withLazyLoadedSlices<
WithSlice<typeof numberSlice>
describe('type tests', () => {
test('combineSlices correctly combines static state', () => {
const rootReducer = combineSlices(stringSlice, numberSlice, exampleApi, {
boolean: booleanReducer,
})

expectTypeOf(rootReducer(undefined, { type: '' })).toEqualTypeOf<{
string: string
number: number
boolean: boolean
api: ExampleApiState
}>()
})

test('withLazyLoadedSlices adds partial to state', () => {
const rootReducer = combineSlices(stringSlice).withLazyLoadedSlices<
WithSlice<typeof numberSlice> & WithSlice<typeof exampleApi>
>()

const innerSelector = innerReducer.inject(numberSlice).selector(
(state) => state.number,
(rootState: RootState) => rootState.inner
)
expectTypeOf(rootReducer(undefined, { type: '' }).number).toEqualTypeOf<
number | undefined
>()

const outerReducer = combineSlices({ inner: innerReducer })
expectTypeOf(rootReducer(undefined, { type: '' }).api).toEqualTypeOf<
ExampleApiState | undefined
>()
})

type RootState = ReturnType<typeof outerReducer>
test('inject marks injected keys as required', () => {
const rootReducer = combineSlices(stringSlice).withLazyLoadedSlices<
WithSlice<typeof numberSlice> &
WithSlice<typeof exampleApi> & { boolean: boolean }
>()

expectType<{ inner: { string: string } }>(
outerReducer(undefined, { type: '' })
)
expectTypeOf(rootReducer(undefined, { type: '' }).number).toEqualTypeOf<
number | undefined
>()

expectType<number>(innerSelector(outerReducer(undefined, { type: '' })))
}
expectTypeOf(rootReducer(undefined, { type: '' }).boolean).toEqualTypeOf<
boolean | undefined
>()

expectTypeOf(rootReducer(undefined, { type: '' }).api).toEqualTypeOf<
ExampleApiState | undefined
>()

const withNumber = rootReducer.inject(numberSlice)

expectTypeOf(
withNumber(undefined, { type: '' }).number
).toEqualTypeOf<number>()

/**
* Test: selector errors if selectorFn and selectState are mismatched
*/
const withBool = rootReducer.inject({
reducerPath: 'boolean' as const,
reducer: booleanReducer,
})

{
const combinedReducer =
combineSlices(stringSlice).withLazyLoadedSlices<
WithSlice<typeof numberSlice>
expectTypeOf(
withBool(undefined, { type: '' }).boolean
).toEqualTypeOf<boolean>()

const withApi = rootReducer.inject(exampleApi)

expectTypeOf(
withApi(undefined, { type: '' }).api
).toEqualTypeOf<ExampleApiState>()
})

test('selector() allows defining selectors with injected reducers defined', () => {
const rootReducer = combineSlices(stringSlice).withLazyLoadedSlices<
WithSlice<typeof numberSlice> & { boolean: boolean }
>()

const outerReducer = combineSlices({ inner: combinedReducer })

type RootState = ReturnType<typeof outerReducer>

combinedReducer.selector(
(state) => state.number,
// @ts-expect-error wrong state returned
(rootState: RootState) => rootState.inner.number
)
combinedReducer.selector(
(state, num: number) => state.number,
// @ts-expect-error wrong arguments
(rootState: RootState, str: string) => rootState.inner
)

combinedReducer.selector(
(state, num: number) => state.number,
(rootState: RootState) => rootState.inner
)

// TODO: see if there's a way of making this work
// probably a rare case so not the end of the world if not
combinedReducer.selector(
(state) => state.number,
// @ts-ignore
(rootState: RootState, num: number) => rootState.inner
)
}
type RootState = ReturnType<typeof rootReducer>

const withoutInjection = rootReducer.selector(
(state: RootState) => state.number
)

expectTypeOf(
withoutInjection(rootReducer(undefined, { type: '' }))
).toEqualTypeOf<number | undefined>()

const withInjection = rootReducer
.inject(numberSlice)
.selector((state) => state.number)

expectTypeOf(
withInjection(rootReducer(undefined, { type: '' }))
).toEqualTypeOf<number>()
})

test('selector() passes arguments through', () => {
const rootReducer = combineSlices(stringSlice).withLazyLoadedSlices<
WithSlice<typeof numberSlice> & { boolean: boolean }
>()

const selector = rootReducer
.inject(numberSlice)
.selector((state, num: number) => state.number)

const state = rootReducer(undefined, { type: '' })
// @ts-expect-error required argument
selector(state)
// @ts-expect-error number not string
selector(state, '')
selector(state, 0)
})

test('nested calls inferred correctly', () => {
const innerReducer =
combineSlices(stringSlice).withLazyLoadedSlices<
WithSlice<typeof numberSlice>
>()

const innerSelector = innerReducer.inject(numberSlice).selector(
(state) => state.number,
(rootState: RootState) => rootState.inner
)

const outerReducer = combineSlices({ inner: innerReducer })

type RootState = ReturnType<typeof outerReducer>

expectTypeOf(outerReducer(undefined, { type: '' })).toMatchTypeOf<{
inner: { string: string }
}>()

expectTypeOf(outerReducer(undefined, { type: '' })).not.toEqualTypeOf<{
inner: { string: string }
}>()

expectTypeOf(
innerSelector(outerReducer(undefined, { type: '' }))
).toEqualTypeOf<number>()
})

test('selector errors if selectorFn and selectState are mismatched', () => {
const combinedReducer =
combineSlices(stringSlice).withLazyLoadedSlices<
WithSlice<typeof numberSlice>
>()

const outerReducer = combineSlices({ inner: combinedReducer })

type RootState = ReturnType<typeof outerReducer>

combinedReducer.selector(
(state) => state.number,
// @ts-expect-error wrong state returned
(rootState: RootState) => rootState.inner.number
)

combinedReducer.selector(
(state, num: number) => state.number,
// @ts-expect-error wrong arguments
(rootState: RootState, str: string) => rootState.inner
)

combinedReducer.selector(
(state, num: number) => state.number,
(rootState: RootState) => rootState.inner
)

// TODO: see if there's a way of making this work
// probably a rare case so not the end of the world if not
combinedReducer.selector(
(state) => state.number,
// @ts-ignore
(rootState: RootState, num: number) => rootState.inner
)
})
})