Skip to content

Commit 6ec0d4e

Browse files
committed
Fix remaining import issues in test files
1 parent 6b0eca6 commit 6ec0d4e

File tree

7 files changed

+71
-63
lines changed

7 files changed

+71
-63
lines changed

packages/toolkit/src/query/tests/buildCreateApi.test.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
createStoreHook,
1616
} from 'react-redux'
1717
import { setupApiStore, useRenderCounter } from '../../tests/utils/helpers'
18-
import { delay } from '../../utils'
1918

2019
const MyContext = React.createContext<ReactReduxContextValue>(null as any)
2120

packages/toolkit/src/query/tests/buildHooks.test.tsx

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,43 @@
1-
import type { SerializedError, UnknownAction } from '@reduxjs/toolkit';
1+
import type { SerializedError, UnknownAction } from '@reduxjs/toolkit'
22
import {
33
configureStore,
44
createListenerMiddleware,
55
createSlice,
6-
} from '@reduxjs/toolkit';
7-
import type { SubscriptionOptions } from '@reduxjs/toolkit/dist/query/core/apiState';
6+
} from '@reduxjs/toolkit'
7+
import type { SubscriptionOptions } from '@reduxjs/toolkit/dist/query/core/apiState'
88
import type {
99
UseMutation,
1010
UseQuery,
11-
} from '@reduxjs/toolkit/dist/query/react/buildHooks';
11+
} from '@reduxjs/toolkit/dist/query/react/buildHooks'
1212
import {
1313
QueryStatus,
1414
createApi,
1515
fetchBaseQuery,
1616
skipToken,
17-
} from '@reduxjs/toolkit/query/react';
17+
} from '@reduxjs/toolkit/query/react'
1818
import {
1919
act,
2020
fireEvent,
2121
render,
2222
renderHook,
2323
screen,
2424
waitFor,
25-
renderHook,
2625
} from '@testing-library/react'
2726
import userEvent from '@testing-library/user-event'
28-
import { http, HttpResponse } from 'msw'
27+
import { HttpResponse, http } from 'msw'
28+
import { useEffect, useState } from 'react'
29+
import type { MockInstance } from 'vitest'
2930
import {
3031
actionsReducer,
3132
setupApiStore,
3233
useRenderCounter,
3334
waitMs,
3435
withProvider,
35-
} from '../../tests/utils/helpers';
36-
import { expectExactType, expectType } from '../../tests/utils/typeTestHelpers';
37-
import type { SubscriptionSelectors } from '../core/buildMiddleware/types';
38-
import { countObjectKeys } from '../utils/countObjectKeys';
39-
import { server } from './mocks/server';
36+
} from '../../tests/utils/helpers'
37+
import { expectExactType, expectType } from '../../tests/utils/typeTestHelpers'
38+
import type { SubscriptionSelectors } from '../core/buildMiddleware/types'
39+
import { countObjectKeys } from '../utils/countObjectKeys'
40+
import { server } from './mocks/server'
4041

4142
// Just setup a temporary in-memory counter for tests that `getIncrementedAmount`.
4243
// This can be used to test how many renders happen due to data changes or
@@ -188,7 +189,7 @@ describe('hooks tests', () => {
188189

189190
test('useQuery hook sets isFetching=true whenever a request is in flight', async () => {
190191
function User() {
191-
const [value, setValue] = React.useState(0)
192+
const [value, setValue] = useState(0)
192193

193194
const { isFetching } = api.endpoints.getUser.useQuery(1, {
194195
skip: value < 1,
@@ -229,7 +230,7 @@ describe('hooks tests', () => {
229230
test('useQuery hook sets isLoading=true only on initial request', async () => {
230231
let refetch: any, isLoading: boolean, isFetching: boolean
231232
function User() {
232-
const [value, setValue] = React.useState(0)
233+
const [value, setValue] = useState(0)
233234

234235
;({ isLoading, isFetching, refetch } = api.endpoints.getUser.useQuery(
235236
2,
@@ -278,7 +279,7 @@ describe('hooks tests', () => {
278279
test('useQuery hook sets isLoading and isFetching to the correct states', async () => {
279280
let refetchMe: () => void = () => {}
280281
function User() {
281-
const [value, setValue] = React.useState(0)
282+
const [value, setValue] = useState(0)
282283
getRenderCount = useRenderCounter()
283284

284285
const { isLoading, isFetching, refetch } =
@@ -344,10 +345,10 @@ describe('hooks tests', () => {
344345
const { isLoading, isFetching, status } =
345346
api.endpoints.getUser.useQuery(id)
346347

347-
React.useEffect(() => {
348+
useEffect(() => {
348349
loadingHist.push(isLoading)
349350
}, [isLoading])
350-
React.useEffect(() => {
351+
useEffect(() => {
351352
fetchingHist.push(isFetching)
352353
}, [isFetching])
353354
return (
@@ -509,7 +510,7 @@ describe('hooks tests', () => {
509510
test('refetchOnMountOrArgChange works as expected when changing skip from false->true', async () => {
510511
let data, isLoading, isFetching
511512
function User() {
512-
const [skip, setSkip] = React.useState(true)
513+
const [skip, setSkip] = useState(true)
513514
;({ data, isLoading, isFetching } =
514515
api.endpoints.getIncrementedAmount.useQuery(undefined, {
515516
refetchOnMountOrArgChange: 0.5,
@@ -555,7 +556,7 @@ describe('hooks tests', () => {
555556

556557
let data, isLoading, isFetching
557558
function User() {
558-
const [skip, setSkip] = React.useState(true)
559+
const [skip, setSkip] = useState(true)
559560
;({ data, isLoading, isFetching } =
560561
api.endpoints.getIncrementedAmount.useQuery(undefined, {
561562
skip,
@@ -633,7 +634,7 @@ describe('hooks tests', () => {
633634

634635
test(`useQuery refetches when query args object changes even if serialized args don't change`, async () => {
635636
function ItemList() {
636-
const [pageNumber, setPageNumber] = React.useState(0)
637+
const [pageNumber, setPageNumber] = useState(0)
637638
const { data = [] } = api.useListItemsQuery({ pageNumber })
638639

639640
const renderedItems = data.map((item) => (
@@ -908,7 +909,7 @@ describe('hooks tests', () => {
908909
test('useLazyQuery accepts updated subscription options and only dispatches updateSubscriptionOptions when values are updated', async () => {
909910
let interval = 1000
910911
function User() {
911-
const [options, setOptions] = React.useState<SubscriptionOptions>()
912+
const [options, setOptions] = useState<SubscriptionOptions>()
912913
const [fetchUser, { data: hookData, isFetching, isUninitialized }] =
913914
api.endpoints.getUser.useLazyQuery(options)
914915
getRenderCount = useRenderCounter()
@@ -1066,7 +1067,7 @@ describe('hooks tests', () => {
10661067
test('useLazyQuery hook callback returns various properties to handle the result', async () => {
10671068
function User() {
10681069
const [getUser] = api.endpoints.getUser.useLazyQuery()
1069-
const [{ successMsg, errMsg, isAborted }, setValues] = React.useState({
1070+
const [{ successMsg, errMsg, isAborted }, setValues] = useState({
10701071
successMsg: '',
10711072
errMsg: '',
10721073
isAborted: false,
@@ -1160,7 +1161,7 @@ describe('hooks tests', () => {
11601161
const [getUser, { data, error }] =
11611162
api.endpoints.getUserAndForceError.useLazyQuery()
11621163

1163-
const [unwrappedError, setUnwrappedError] = React.useState<any>()
1164+
const [unwrappedError, setUnwrappedError] = useState<any>()
11641165

11651166
const handleClick = async () => {
11661167
const res = getUser(1)
@@ -1213,7 +1214,7 @@ describe('hooks tests', () => {
12131214
function User() {
12141215
const [getUser, { data, error }] = api.endpoints.getUser.useLazyQuery()
12151216

1216-
const [unwrappedResult, setUnwrappedResult] = React.useState<
1217+
const [unwrappedResult, setUnwrappedResult] = useState<
12171218
undefined | { name: string }
12181219
>()
12191220

@@ -1320,9 +1321,9 @@ describe('hooks tests', () => {
13201321
test('useMutation hook callback returns various properties to handle the result', async () => {
13211322
function User() {
13221323
const [updateUser] = api.endpoints.updateUser.useMutation()
1323-
const [successMsg, setSuccessMsg] = React.useState('')
1324-
const [errMsg, setErrMsg] = React.useState('')
1325-
const [isAborted, setIsAborted] = React.useState(false)
1324+
const [successMsg, setSuccessMsg] = useState('')
1325+
const [errMsg, setErrMsg] = useState('')
1326+
const [isAborted, setIsAborted] = useState(false)
13261327

13271328
const handleClick = async () => {
13281329
const res = updateUser({ name: 'Banana' })
@@ -1750,14 +1751,18 @@ describe('hooks tests', () => {
17501751
test('initially failed useQueries that provide an tag will refetch after a mutation invalidates it', async () => {
17511752
const checkSessionData = { name: 'matt' }
17521753
server.use(
1753-
http.get('https://example.com/me', () => {
1754+
http.get(
1755+
'https://example.com/me',
1756+
() => {
17541757
return HttpResponse.json(null, { status: 500 })
1755-
}, { once: true }),
1758+
},
1759+
{ once: true }
1760+
),
17561761
http.get('https://example.com/me', () => {
1757-
return HttpResponse.json(checkSessionData)
1762+
return HttpResponse.json(checkSessionData)
17581763
}),
17591764
http.post('https://example.com/login', () => {
1760-
return HttpResponse.json(null, { status: 200 })
1765+
return HttpResponse.json(null, { status: 200 })
17611766
})
17621767
)
17631768
let data, isLoading, isError
@@ -1976,12 +1981,12 @@ describe('hooks with createApi defaults set', () => {
19761981

19771982
const handlers = [
19781983
http.get('https://example.com/posts', () => {
1979-
return HttpResponse.json(posts)
1984+
return HttpResponse.json(posts)
19801985
}),
19811986
http.put<{ id: string }, Partial<Post>>(
19821987
'https://example.com/post/:id',
19831988
async ({ request, params }) => {
1984-
const body = await request.json();
1989+
const body = await request.json()
19851990
const id = Number(params.id)
19861991
const idx = posts.findIndex((post) => post.id === id)
19871992

@@ -1997,20 +2002,23 @@ describe('hooks with createApi defaults set', () => {
19972002
)
19982003
posts = [...newPosts]
19992004

2000-
return HttpResponse.json(posts)
2005+
return HttpResponse.json(posts)
20012006
}
20022007
),
2003-
http.post<any, Omit<Post, 'id'>>('https://example.com/post', async ({ request }) => {
2004-
const body = await request.json();
2005-
let post = body
2006-
startingId += 1
2007-
posts.concat({
2008-
...post,
2009-
fetched_at: new Date().toISOString(),
2010-
id: startingId,
2011-
})
2008+
http.post<any, Omit<Post, 'id'>>(
2009+
'https://example.com/post',
2010+
async ({ request }) => {
2011+
const body = await request.json()
2012+
let post = body
2013+
startingId += 1
2014+
posts.concat({
2015+
...post,
2016+
fetched_at: new Date().toISOString(),
2017+
id: startingId,
2018+
})
20122019
return HttpResponse.json(posts)
2013-
}),
2020+
}
2021+
),
20142022
]
20152023

20162024
server.use(...handlers)
@@ -2292,7 +2300,7 @@ describe('hooks with createApi defaults set', () => {
22922300
})
22932301
getRenderCount = useRenderCounter()
22942302

2295-
React.useEffect(() => {
2303+
useEffect(() => {
22962304
expectablePost = post
22972305
}, [post])
22982306

@@ -2379,7 +2387,6 @@ describe('hooks with createApi defaults set', () => {
23792387

23802388
test('useQuery with selectFromResult option has a type error if the result is not an object', async () => {
23812389
function SelectedPost() {
2382-
23832390
const res2 = api.endpoints.getPosts.useQuery(undefined, {
23842391
// selectFromResult must always return an object
23852392
selectFromResult: ({ data }) => ({ size: data?.length ?? 0 }),

packages/toolkit/src/query/tests/createApi.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ import type {
2020
} from '@reduxjs/toolkit/dist/query/endpointDefinitions'
2121
import { HttpResponse, delay, http } from 'msw'
2222
import nodeFetch from 'node-fetch'
23+
import {
24+
ANY,
25+
getSerializedHeaders,
26+
setupApiStore,
27+
} from '../../tests/utils/helpers'
28+
import { expectExactType, expectType } from '../../tests/utils/typeTestHelpers'
2329
import { server } from './mocks/server'
2430

2531
beforeAll(() => {

packages/toolkit/src/query/tests/fetchBaseQuery.test.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
import { createSlice } from '@reduxjs/toolkit'
22
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'
3-
import { vi } from 'vitest'
4-
import { setupApiStore, waitMs } from '../../tests/utils/helpers'
5-
import { server } from './mocks/server'
6-
// @ts-ignore
7-
import nodeFetch from 'node-fetch'
8-
import { setupApiStore } from './helpers'
9-
import { server } from './mocks/server'
10-
113
import { headersToObject } from 'headers-polyfill'
124
import { HttpResponse, delay, http } from 'msw'
5+
import nodeFetch from 'node-fetch'
136
import queryString from 'query-string'
7+
import { vi } from 'vitest'
8+
import { setupApiStore } from '../../tests/utils/helpers'
149
import type { BaseQueryApi } from '../baseQueryTypes'
10+
import { server } from './mocks/server'
1511

1612
const defaultHeaders: Record<string, string> = {
1713
fake: 'header',
@@ -28,7 +24,7 @@ const baseQuery = fetchBaseQuery({
2824
baseUrl,
2925
fetchFn: fetchFn as any,
3026
prepareHeaders: (headers, { getState }) => {
31-
const token = (getState() as RootState).auth.token
27+
const { token } = (getState() as RootState).auth
3228

3329
// If we have a token set in state, let's assume that we should be passing it.
3430
if (token) {

packages/toolkit/src/query/tests/polling.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ describe('polling tests', () => {
6565

6666
const getSubs = createSubscriptionGetter(queryCacheKey)
6767

68-
await waitMs(1)
68+
await delay(1)
6969
expect(Object.keys(getSubs())).toHaveLength(1)
7070
expect(getSubs()[requestId].pollingInterval).toBe(10)
7171

7272
subscription.updateSubscriptionOptions({ pollingInterval: 20 })
7373

74-
await waitMs(1)
74+
await delay(1)
7575
expect(Object.keys(getSubs())).toHaveLength(1)
7676
expect(getSubs()[requestId].pollingInterval).toBe(20)
7777
})
@@ -91,15 +91,15 @@ describe('polling tests', () => {
9191
})
9292
)
9393

94-
await waitMs(10)
94+
await delay(10)
9595

9696
const getSubs = createSubscriptionGetter(subscriptionOne.queryCacheKey)
9797

9898
expect(Object.keys(getSubs())).toHaveLength(2)
9999

100100
subscriptionOne.unsubscribe()
101101

102-
await waitMs(1)
102+
await delay(1)
103103
expect(Object.keys(getSubs())).toHaveLength(1)
104104
})
105105

packages/toolkit/src/query/tests/refetchingBehaviors.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const defaultApi = createApi({
3636

3737
const storeRef = setupApiStore(defaultApi)
3838

39-
let getIncrementedAmountState = () =>
39+
const getIncrementedAmountState = () =>
4040
storeRef.store.getState().api.queries['getIncrementedAmount(undefined)']
4141

4242
afterEach(() => {
@@ -217,7 +217,7 @@ describe('refetchOnFocus tests', () => {
217217
fireEvent.focus(window)
218218
})
219219

220-
await waitMs(1)
220+
await delay(1)
221221
expect(getIncrementedAmountState()).toBeUndefined()
222222
})
223223
})

packages/toolkit/src/tests/utils/helpers.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ expect.extend({
154154
) {
155155
const restore = mockConsole(createConsole())
156156
await fn()
157-
const log = getLog().log
157+
const { log } = getLog()
158158
restore()
159159

160160
if (normalize(log) === normalize(expectedOutput))

0 commit comments

Comments
 (0)