Skip to content

Commit 19444c6

Browse files
authored
chore: Migrate usePullHeadDataTeam to TS Query V5 (#3544)
1 parent 8e6e6b2 commit 19444c6

File tree

5 files changed

+79
-68
lines changed

5 files changed

+79
-68
lines changed

src/pages/PullRequestPage/Header/HeaderTeam/HeaderTeam.test.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
1+
import {
2+
QueryClientProvider as QueryClientProviderV5,
3+
QueryClient as QueryClientV5,
4+
} from '@tanstack/react-queryV5'
25
import { render, screen, waitFor } from '@testing-library/react'
36
import { graphql, HttpResponse } from 'msw'
47
import { setupServer } from 'msw/node'
@@ -39,24 +42,24 @@ const mockPullData = ({
3942
},
4043
})
4144

42-
const queryClient = new QueryClient({
45+
const queryClientV5 = new QueryClientV5({
4346
defaultOptions: { queries: { retry: false } },
4447
})
4548
const server = setupServer()
4649
const wrapper: React.FC<React.PropsWithChildren> = ({ children }) => (
47-
<QueryClientProvider client={queryClient}>
50+
<QueryClientProviderV5 client={queryClientV5}>
4851
<MemoryRouter initialEntries={['/gh/test-org/test-repo/pull/12']}>
4952
<Route path="/:provider/:owner/:repo/pull/:pullId">{children}</Route>
5053
</MemoryRouter>
51-
</QueryClientProvider>
54+
</QueryClientProviderV5>
5255
)
5356

5457
beforeAll(() => {
5558
server.listen()
5659
})
5760

5861
afterEach(() => {
59-
queryClient.clear()
62+
queryClientV5.clear()
6063
server.resetHandlers()
6164
})
6265

@@ -138,8 +141,8 @@ describe('Header', () => {
138141
setup({ nullPull: true })
139142
render(<Header />, { wrapper })
140143

141-
await waitFor(() => queryClient.isFetching)
142-
await waitFor(() => !queryClient.isFetching)
144+
await waitFor(() => queryClientV5.isFetching)
145+
await waitFor(() => !queryClientV5.isFetching)
143146

144147
const open = screen.queryByText(/open/i)
145148
expect(open).not.toBeInTheDocument()

src/pages/PullRequestPage/Header/HeaderTeam/HeaderTeam.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import cs from 'classnames'
1+
import { useSuspenseQuery as useSuspenseQueryV5 } from '@tanstack/react-queryV5'
22
import capitalize from 'lodash/capitalize'
33
import { useParams } from 'react-router-dom'
44

55
import { Provider } from 'shared/api/helpers'
6+
import { cn } from 'shared/utils/cn'
67
import { formatTimeToNow } from 'shared/utils/dates'
78
import { getProviderPullURL } from 'shared/utils/provider'
89
import A from 'ui/A'
910
import CIStatusLabel from 'ui/CIStatus'
1011
import Icon from 'ui/Icon'
1112
import TotalsNumber from 'ui/TotalsNumber'
1213

13-
import { usePullHeadDataTeam } from './hooks'
14+
import { PullHeadDataTeamQueryOpts } from './queries/PullHeadDataTeamQueryOpts'
1415

1516
import { pullStateToColor } from '../constants'
1617

@@ -23,7 +24,9 @@ interface URLParams {
2324

2425
function HeaderTeam() {
2526
const { provider, owner, repo, pullId } = useParams<URLParams>()
26-
const { data } = usePullHeadDataTeam({ provider, owner, repo, pullId })
27+
const { data } = useSuspenseQueryV5(
28+
PullHeadDataTeamQueryOpts({ provider, owner, repo, pullId })
29+
)
2730

2831
const pull = data?.pull
2932
let percentCovered = null
@@ -39,8 +42,8 @@ function HeaderTeam() {
3942
{pull?.title}
4043
{pull?.state ? (
4144
<span
42-
className={cs(
43-
'text-white font-bold px-3 py-0.5 text-xs rounded',
45+
className={cn(
46+
'rounded px-3 py-0.5 text-xs font-bold text-white',
4447
pullStateToColor[pull?.state]
4548
)}
4649
>

src/pages/PullRequestPage/Header/HeaderTeam/hooks/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/pages/PullRequestPage/Header/HeaderTeam/hooks/usePullHeadDataTeam.test.tsx renamed to src/pages/PullRequestPage/Header/HeaderTeam/queries/PullHeadDataTeamQueryOpts.test.tsx

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
1+
import {
2+
QueryClientProvider as QueryClientProviderV5,
3+
QueryClient as QueryClientV5,
4+
useQuery as useQueryV5,
5+
} from '@tanstack/react-queryV5'
26
import { renderHook, waitFor } from '@testing-library/react'
37
import { graphql, HttpResponse } from 'msw'
48
import { setupServer } from 'msw/node'
59

6-
import { usePullHeadDataTeam } from './usePullHeadDataTeam'
10+
import { PullHeadDataTeamQueryOpts } from './PullHeadDataTeamQueryOpts'
711

812
const mockPullData = {
913
owner: {
@@ -56,21 +60,23 @@ const mockNullOwner = {
5660

5761
const mockUnsuccessfulParseError = {}
5862

59-
const queryClient = new QueryClient({
63+
const queryClientV5 = new QueryClientV5({
6064
defaultOptions: { queries: { retry: false } },
6165
})
6266
const server = setupServer()
6367

6468
const wrapper: React.FC<React.PropsWithChildren> = ({ children }) => (
65-
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
69+
<QueryClientProviderV5 client={queryClientV5}>
70+
{children}
71+
</QueryClientProviderV5>
6672
)
6773

6874
beforeAll(() => {
6975
server.listen()
7076
})
7177

7278
afterEach(() => {
73-
queryClient.clear()
79+
queryClientV5.clear()
7480
server.resetHandlers()
7581
})
7682

@@ -116,15 +122,15 @@ describe('usePullHeadDataTeam', () => {
116122
setup({})
117123
const { result } = renderHook(
118124
() =>
119-
usePullHeadDataTeam({
120-
provider: 'gh',
121-
owner: 'codecov',
122-
repo: 'cool-repo',
123-
pullId: '1',
124-
}),
125-
{
126-
wrapper,
127-
}
125+
useQueryV5(
126+
PullHeadDataTeamQueryOpts({
127+
provider: 'gh',
128+
owner: 'codecov',
129+
repo: 'cool-repo',
130+
pullId: '1',
131+
})
132+
),
133+
{ wrapper }
128134
)
129135

130136
await waitFor(() => result.current.isLoading)
@@ -161,15 +167,15 @@ describe('usePullHeadDataTeam', () => {
161167
setup({ isNullOwner: true })
162168
const { result } = renderHook(
163169
() =>
164-
usePullHeadDataTeam({
165-
provider: 'gh',
166-
owner: 'codecov',
167-
repo: 'cool-repo',
168-
pullId: '1',
169-
}),
170-
{
171-
wrapper,
172-
}
170+
useQueryV5(
171+
PullHeadDataTeamQueryOpts({
172+
provider: 'gh',
173+
owner: 'codecov',
174+
repo: 'cool-repo',
175+
pullId: '1',
176+
})
177+
),
178+
{ wrapper }
173179
)
174180

175181
await waitFor(() => result.current.isLoading)
@@ -200,15 +206,15 @@ describe('usePullHeadDataTeam', () => {
200206

201207
const { result } = renderHook(
202208
() =>
203-
usePullHeadDataTeam({
204-
provider: 'gh',
205-
owner: 'codecov',
206-
repo: 'cool-repo',
207-
pullId: '1',
208-
}),
209-
{
210-
wrapper,
211-
}
209+
useQueryV5(
210+
PullHeadDataTeamQueryOpts({
211+
provider: 'gh',
212+
owner: 'codecov',
213+
repo: 'cool-repo',
214+
pullId: '1',
215+
})
216+
),
217+
{ wrapper }
212218
)
213219

214220
await waitFor(() => expect(result.current.isError).toBeTruthy())
@@ -237,15 +243,15 @@ describe('usePullHeadDataTeam', () => {
237243
setup({ isOwnerNotActivatedError: true })
238244
const { result } = renderHook(
239245
() =>
240-
usePullHeadDataTeam({
241-
provider: 'gh',
242-
owner: 'codecov',
243-
repo: 'cool-repo',
244-
pullId: '1',
245-
}),
246-
{
247-
wrapper,
248-
}
246+
useQueryV5(
247+
PullHeadDataTeamQueryOpts({
248+
provider: 'gh',
249+
owner: 'codecov',
250+
repo: 'cool-repo',
251+
pullId: '1',
252+
})
253+
),
254+
{ wrapper }
249255
)
250256

251257
await waitFor(() => expect(result.current.isError).toBeTruthy())
@@ -274,15 +280,15 @@ describe('usePullHeadDataTeam', () => {
274280
setup({ isUnsuccessfulParseError: true })
275281
const { result } = renderHook(
276282
() =>
277-
usePullHeadDataTeam({
278-
provider: 'gh',
279-
owner: 'codecov',
280-
repo: 'cool-repo',
281-
pullId: '1',
282-
}),
283-
{
284-
wrapper,
285-
}
283+
useQueryV5(
284+
PullHeadDataTeamQueryOpts({
285+
provider: 'gh',
286+
owner: 'codecov',
287+
repo: 'cool-repo',
288+
pullId: '1',
289+
})
290+
),
291+
{ wrapper }
286292
)
287293

288294
await waitFor(() => expect(result.current.isError).toBeTruthy())

src/pages/PullRequestPage/Header/HeaderTeam/hooks/usePullHeadDataTeam.tsx renamed to src/pages/PullRequestPage/Header/HeaderTeam/queries/PullHeadDataTeamQueryOpts.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useQuery } from '@tanstack/react-query'
1+
import { queryOptions as queryOptionsV5 } from '@tanstack/react-queryV5'
22
import { z } from 'zod'
33

44
import {
@@ -134,20 +134,20 @@ const query = `
134134
}
135135
`
136136

137-
interface UsePullHeadDataTeamArgs {
137+
interface PullHeadDataTeamQueryArgs {
138138
provider: string
139139
owner: string
140140
repo: string
141141
pullId: string
142142
}
143143

144-
export const usePullHeadDataTeam = ({
144+
export const PullHeadDataTeamQueryOpts = ({
145145
provider,
146146
owner,
147147
repo,
148148
pullId,
149-
}: UsePullHeadDataTeamArgs) =>
150-
useQuery({
149+
}: PullHeadDataTeamQueryArgs) =>
150+
queryOptionsV5({
151151
queryKey: ['PullHeaderTeam', provider, owner, repo, pullId, query],
152152
queryFn: ({ signal }) =>
153153
Api.graphql({

0 commit comments

Comments
 (0)