Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 7 additions & 4 deletions packages/toolkit/src/dynamicMiddleware/tests/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type { Action, Middleware, UnknownAction } from 'redux'
import type { ThunkDispatch } from 'redux-thunk'
import { configureStore } from '../../configureStore'
import { createDynamicMiddleware } from '../index'
import type {
Action,
Middleware,
ThunkDispatch,
UnknownAction,
} from '@reduxjs/toolkit'
import { configureStore, createDynamicMiddleware } from '@reduxjs/toolkit'

const untypedInstance = createDynamicMiddleware()

Expand Down
10 changes: 7 additions & 3 deletions packages/toolkit/src/dynamicMiddleware/tests/react.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import type {
Action,
Middleware,
ThunkDispatch,
UnknownAction,
} from '@reduxjs/toolkit'
import { createDynamicMiddleware } from '@reduxjs/toolkit/react'
import type { Context } from 'react'
import type { ReactReduxContextValue } from 'react-redux'
import type { Action, Middleware, UnknownAction } from 'redux'
import type { ThunkDispatch } from 'redux-thunk'
import { createDynamicMiddleware } from '../react'

interface AppDispatch extends ThunkDispatch<number, undefined, UnknownAction> {
(n: 1): 1
Expand Down
26 changes: 9 additions & 17 deletions packages/toolkit/src/entities/tests/state_selectors.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { createDraftSafeSelectorCreator } from '../../createDraftSafeSelector'
import type { EntityAdapter, EntityState } from '../index'
import { createEntityAdapter } from '../index'
import type { EntitySelectors } from '../models'
import type {
EntityAdapter,
EntitySelectors,
EntityState,
} from '@reduxjs/toolkit'
import {
createDraftSafeSelectorCreator,
createEntityAdapter,
} from '@reduxjs/toolkit'
import type { BookModel } from './fixtures/book'
import { AClockworkOrange, AnimalFarm, TheGreatGatsby } from './fixtures/book'
import type { Selector } from 'reselect'
import { createSelector, weakMapMemoize } from 'reselect'
import { vi } from 'vitest'

describe('Entity State Selectors', () => {
describe('Composed Selectors', () => {
Expand Down Expand Up @@ -103,12 +105,6 @@ describe('Entity State Selectors', () => {
expect(entities).toEqual(state.entities)
})

it('should type single entity from Dictionary as entity type or undefined', () => {
expectType<
Selector<EntityState<BookModel, string>, BookModel | undefined>
>(createSelector(selectors.selectEntities, (entities) => entities[0]))
})

it('should create a selector for selecting the list of models', () => {
const models = selectors.selectAll(state)

Expand Down Expand Up @@ -148,7 +144,3 @@ describe('Entity State Selectors', () => {
})
})
})

function expectType<T>(t: T) {
return t
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('type tests', () => {
effect: (action, listenerApi) => {
foundExtra = listenerApi.extra

expectTypeOf(listenerApi.extra).toMatchTypeOf(originalExtra)
expectTypeOf(listenerApi.extra).toExtend<typeof originalExtra>()
},
})

Expand Down Expand Up @@ -121,7 +121,7 @@ describe('type tests', () => {
takeResult = await listenerApi.take(increment.match, timeout)
expect(takeResult).toBeNull()

expectTypeOf(takeResult).toMatchTypeOf<ExpectedTakeResultType>()
expectTypeOf(takeResult).toExtend<ExpectedTakeResultType>()

done = true
},
Expand Down Expand Up @@ -242,14 +242,14 @@ describe('type tests', () => {
startListening({
actionCreator: incrementByAmount,
effect: (action, listenerApi) => {
expectTypeOf(action).toMatchTypeOf<PayloadAction<number>>()
expectTypeOf(action).toExtend<PayloadAction<number>>()
},
})

startListening({
matcher: incrementByAmount.match,
effect: (action, listenerApi) => {
expectTypeOf(action).toMatchTypeOf<PayloadAction<number>>()
expectTypeOf(action).toExtend<PayloadAction<number>>()
},
})

Expand Down Expand Up @@ -292,7 +292,7 @@ describe('type tests', () => {
addListener({
actionCreator: incrementByAmount,
effect: (action, listenerApi) => {
expectTypeOf(action).toMatchTypeOf<PayloadAction<number>>()
expectTypeOf(action).toExtend<PayloadAction<number>>()
},
}),
)
Expand All @@ -301,7 +301,7 @@ describe('type tests', () => {
addListener({
matcher: incrementByAmount.match,
effect: (action, listenerApi) => {
expectTypeOf(action).toMatchTypeOf<PayloadAction<number>>()
expectTypeOf(action).toExtend<PayloadAction<number>>()
},
}),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ describe('listenerMiddleware.withTypes<RootState, AppDispatch>()', () => {
})

test('addListener.withTypes', () => {
const addAppListener = addListener.withTypes<RootState, AppDispatch, ExtraArgument>()
const addAppListener = addListener.withTypes<
RootState,
AppDispatch,
ExtraArgument
>()

expectTypeOf(addAppListener).toEqualTypeOf<
TypedAddListener<RootState, AppDispatch, ExtraArgument>
Expand All @@ -138,7 +142,11 @@ describe('listenerMiddleware.withTypes<RootState, AppDispatch>()', () => {
})

test('removeListener.withTypes', () => {
const removeAppListener = removeListener.withTypes<RootState, AppDispatch, ExtraArgument>()
const removeAppListener = removeListener.withTypes<
RootState,
AppDispatch,
ExtraArgument
>()

expectTypeOf(removeAppListener).toEqualTypeOf<
TypedRemoveListener<RootState, AppDispatch, ExtraArgument>
Expand Down
24 changes: 15 additions & 9 deletions packages/toolkit/src/query/core/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
ThunkDispatch,
UnknownAction,
} from '@reduxjs/toolkit'
import { enablePatches } from '../utils/immerImports'
import type { CreateSelectorFunction } from 'reselect'
import type { Api, Module } from '../apiTypes'
import type { BaseQueryFn } from '../baseQueryTypes'
import type { InternalSerializeQueryArgs } from '../defaultSerializeQueryArgs'
Expand All @@ -30,19 +30,21 @@ import {
isQueryDefinition,
} from '../endpointDefinitions'
import { assertCast, safeAssign } from '../tsHelpers'
import { getOrInsertComputed } from '../utils'
import { enablePatches } from '../utils/immerImports'
import type {
CombinedState,
MutationKeys,
QueryKeys,
RootState,
} from './apiState'
import type {
BuildInitiateApiEndpointInfiniteQuery,
BuildInitiateApiEndpointMutation,
BuildInitiateApiEndpointQuery,
InfiniteQueryActionCreatorResult,
MutationActionCreatorResult,
QueryActionCreatorResult,
InfiniteQueryActionCreatorResult,
BuildInitiateApiEndpointInfiniteQuery,
} from './buildInitiate'
import { buildInitiate } from './buildInitiate'
import type {
Expand All @@ -51,6 +53,7 @@ import type {
ReferenceQueryLifecycle,
} from './buildMiddleware'
import { buildMiddleware } from './buildMiddleware'
import type { InternalMiddlewareState } from './buildMiddleware/types'
import type {
BuildSelectorsApiEndpointInfiniteQuery,
BuildSelectorsApiEndpointMutation,
Expand All @@ -72,9 +75,6 @@ import type {
import { buildThunks } from './buildThunks'
import { createSelector as _createSelector } from './rtkImports'
import { onFocus, onFocusLost, onOffline, onOnline } from './setupListeners'
import type { InternalMiddlewareState } from './buildMiddleware/types'
import { getOrInsertComputed } from '../utils'
import type { CreateSelectorFunction } from 'reselect'

/**
* `ifOlderThan` - (default: `false` | `number`) - _number is value in seconds_
Expand Down Expand Up @@ -431,7 +431,9 @@ export interface ApiEndpointQuery<
Definition extends QueryDefinition<any, any, any, any, any>,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Definitions extends EndpointDefinitions,
> extends BuildThunksApiEndpointQuery<Definition>,
>
extends
BuildThunksApiEndpointQuery<Definition>,
BuildInitiateApiEndpointQuery<Definition>,
BuildSelectorsApiEndpointQuery<Definition, Definitions> {
name: string
Expand All @@ -446,7 +448,9 @@ export interface ApiEndpointInfiniteQuery<
Definition extends InfiniteQueryDefinition<any, any, any, any, any>,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Definitions extends EndpointDefinitions,
> extends BuildThunksApiEndpointInfiniteQuery<Definition>,
>
extends
BuildThunksApiEndpointInfiniteQuery<Definition>,
BuildInitiateApiEndpointInfiniteQuery<Definition>,
BuildSelectorsApiEndpointInfiniteQuery<Definition, Definitions> {
name: string
Expand All @@ -462,7 +466,9 @@ export interface ApiEndpointMutation<
Definition extends MutationDefinition<any, any, any, any, any>,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Definitions extends EndpointDefinitions,
> extends BuildThunksApiEndpointMutation<Definition>,
>
extends
BuildThunksApiEndpointMutation<Definition>,
BuildInitiateApiEndpointMutation<Definition>,
BuildSelectorsApiEndpointMutation<Definition, Definitions> {
name: string
Expand Down
17 changes: 6 additions & 11 deletions packages/toolkit/src/query/tests/buildHooks.test-d.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import type {
QueryStateSelector,
UseMutation,
UseQuery,
} from '@internal/query/react/buildHooks'
import type { UseMutation, UseQuery } from '@internal/query/react/buildHooks'
import { ANY } from '@internal/tests/utils/helpers'
import type { SerializedError } from '@reduxjs/toolkit'
import type {
QueryDefinition,
SubscriptionOptions,
TypedQueryStateSelector,
} from '@reduxjs/toolkit/query/react'
Expand Down Expand Up @@ -111,15 +106,15 @@ describe('type tests', () => {
// no-op simply for clearer type assertions
res.then((result) => {
if (result.isSuccess) {
expectTypeOf(result).toMatchTypeOf<{
expectTypeOf(result).toMatchObjectType<{
data: {
name: string
}
}>()
}

if (result.isError) {
expectTypeOf(result).toMatchTypeOf<{
expectTypeOf(result).toExtend<{
error: { status: number; data: unknown } | SerializedError
}>()
}
Expand All @@ -137,7 +132,7 @@ describe('type tests', () => {
(options: SubscriptionOptions) => void
>()

expectTypeOf(res.refetch).toMatchTypeOf<() => void>()
expectTypeOf(res.refetch).toExtend<() => void>()

expectTypeOf(res.unwrap()).resolves.toEqualTypeOf<{ name: string }>()
}
Expand All @@ -164,7 +159,7 @@ describe('type tests', () => {
const handleClick = async () => {
const res = updateUser({ name: 'Banana' })

expectTypeOf(res).resolves.toMatchTypeOf<
expectTypeOf(res).resolves.toExtend<
| {
error: { status: number; data: unknown } | SerializedError
}
Expand All @@ -175,7 +170,7 @@ describe('type tests', () => {
}
>()

expectTypeOf(res.arg).toMatchTypeOf<{
expectTypeOf(res.arg).toMatchObjectType<{
endpointName: string
originalArgs: { name: string }
track?: boolean
Expand Down
3 changes: 1 addition & 2 deletions packages/toolkit/src/query/tests/buildSelector.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'

import { configureStore, createSelector } from '@reduxjs/toolkit'
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'

describe('type tests', () => {
test('buildSelector type test', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/tests/cacheLifecycle.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('type tests', () => {
) {
const firstValue = await cacheDataLoaded

expectTypeOf(firstValue).toMatchTypeOf<{
expectTypeOf(firstValue).toExtend<{
data: number
meta?: FetchBaseQueryMeta
}>()
Expand Down
12 changes: 5 additions & 7 deletions packages/toolkit/src/query/tests/createApi.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe('type tests', () => {
},
})

expectTypeOf(query).toMatchTypeOf<
expectTypeOf(query).toExtend<
QueryDefinition<'Arg', any, any, 'RetVal'>
>()

Expand Down Expand Up @@ -178,7 +178,7 @@ describe('type tests', () => {
},
})

expectTypeOf(query).toMatchTypeOf<
expectTypeOf(query).toExtend<
MutationDefinition<'Arg', any, any, 'RetVal'>
>()

Expand Down Expand Up @@ -362,15 +362,13 @@ describe('type tests', () => {
enhancedApi.endpoints.query1.initiate(),
)

expectTypeOf(queryResponse.data).toMatchTypeOf<
Transformed | undefined
>()
expectTypeOf(queryResponse.data).toExtend<Transformed | undefined>()

const mutationResponse = await storeRef.store.dispatch(
enhancedApi.endpoints.mutation1.initiate(),
)

expectTypeOf(mutationResponse).toMatchTypeOf<
expectTypeOf(mutationResponse).toExtend<
| { data: Transformed }
| { error: FetchBaseQueryError | SerializedError }
>()
Expand Down Expand Up @@ -508,7 +506,7 @@ describe('type tests', () => {
).toEqualTypeOf<Post>()
expectTypeOf(api.endpoints.query2.Types.RawResultType).toBeAny()

expectTypeOf(api.endpoints.query3.Types.QueryArg).toEqualTypeOf<void>()
expectTypeOf(api.endpoints.query3.Types.QueryArg).toBeVoid()
expectTypeOf(api.endpoints.query3.Types.ResultType).toEqualTypeOf<
EntityState<Post, Post['id']>
>()
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/tests/createApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
fetchBaseQuery,
NamedSchemaError,
} from '@reduxjs/toolkit/query'
import { HttpResponse, delay, http } from 'msw'
import { delay, http, HttpResponse } from 'msw'
import nodeFetch from 'node-fetch'
import * as v from 'valibot'
import type { SchemaFailureHandler } from '../endpointDefinitions'
Expand Down
4 changes: 1 addition & 3 deletions packages/toolkit/src/query/tests/fetchBaseQuery.test.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { createSlice } from '@reduxjs/toolkit'
import type { FetchArgs } from '@reduxjs/toolkit/query'
import type { BaseQueryApi, FetchArgs } from '@reduxjs/toolkit/query'
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'
import { headersToObject } from 'headers-polyfill'
import { HttpResponse, delay, http } from 'msw'
import nodeFetch from 'node-fetch'
import queryString from 'query-string'
import { vi } from 'vitest'
import { setupApiStore } from '../../tests/utils/helpers'
import type { BaseQueryApi } from '../baseQueryTypes'
import { server } from './mocks/server'

const defaultHeaders: Record<string, string> = {
Expand Down Expand Up @@ -854,7 +853,6 @@ describe('fetchBaseQuery', () => {
const prepare = vitest.fn()
const baseQuery = fetchBaseQuery({
prepareHeaders(headers, api) {
expectTypeOf(api.extraOptions).toEqualTypeOf<unknown>()
prepare.apply(undefined, arguments as unknown as any[])
},
})
Expand Down
Loading
Loading