Skip to content

Commit a1e07ef

Browse files
authored
ref: Migrate useReposTeam to TS Query V5 (#3615)
1 parent 56cde4f commit a1e07ef

File tree

8 files changed

+140
-96
lines changed

8 files changed

+140
-96
lines changed

src/services/repos/useReposTeam.test.tsx renamed to src/services/repos/ReposTeamQueryOpts.test.tsx

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
1+
import {
2+
QueryClientProvider as QueryClientProviderV5,
3+
QueryClient as QueryClientV5,
4+
useInfiniteQuery as useInfiniteQueryV5,
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
import { MemoryRouter, Route } from 'react-router-dom'
610
import { MockInstance } from 'vitest'
711

8-
import { useReposTeam } from './useReposTeam'
12+
import { ReposTeamQueryOpts } from './ReposTeamQueryOpts'
913

10-
const queryClient = new QueryClient({
14+
const queryClientV5 = new QueryClientV5({
1115
defaultOptions: { queries: { retry: false } },
1216
})
1317
const wrapper: React.FC<React.PropsWithChildren> = ({ children }) => (
14-
<QueryClientProvider client={queryClient}>
18+
<QueryClientProviderV5 client={queryClientV5}>
1519
<MemoryRouter initialEntries={['/gh/some-owner']}>
1620
<Route path="/:provider/:owner">{children}</Route>
1721
</MemoryRouter>
18-
</QueryClientProvider>
22+
</QueryClientProviderV5>
1923
)
2024

2125
const repo1 = {
@@ -88,8 +92,8 @@ beforeAll(() => {
8892
})
8993

9094
beforeEach(() => {
95+
queryClientV5.clear()
9196
server.resetHandlers()
92-
queryClient.clear()
9397
})
9498

9599
afterAll(() => {
@@ -129,7 +133,14 @@ describe('useReposTeam', () => {
129133
it('returns repositories', async () => {
130134
setup()
131135
const { result } = renderHook(
132-
() => useReposTeam({ activated: true, owner: 'codecov' }),
136+
() =>
137+
useInfiniteQueryV5(
138+
ReposTeamQueryOpts({
139+
provider: 'gh',
140+
activated: true,
141+
owner: 'codecov',
142+
})
143+
),
133144
{ wrapper }
134145
)
135146

@@ -145,7 +156,7 @@ describe('useReposTeam', () => {
145156
},
146157
},
147158
],
148-
pageParams: [undefined],
159+
pageParams: [''],
149160
})
150161
)
151162
})
@@ -155,7 +166,15 @@ describe('useReposTeam', () => {
155166
it('returns repositories of the user', async () => {
156167
setup()
157168
const { result } = renderHook(
158-
() => useReposTeam({ owner: 'codecov', activated: true, first: 2 }),
169+
() =>
170+
useInfiniteQueryV5(
171+
ReposTeamQueryOpts({
172+
provider: 'gh',
173+
activated: true,
174+
owner: 'codecov',
175+
first: 2,
176+
})
177+
),
159178
{ wrapper }
160179
)
161180

@@ -178,13 +197,10 @@ describe('useReposTeam', () => {
178197
{
179198
repos: [repo3, repo4],
180199
isCurrentUserPartOfOrg: true,
181-
pageInfo: {
182-
hasNextPage: false,
183-
endCursor: 'aa',
184-
},
200+
pageInfo: { hasNextPage: false, endCursor: 'aa' },
185201
},
186202
],
187-
pageParams: [undefined, 'MjAyMC0wOC0xMSAxNzozMDowMiswMDowMHwxMDA='],
203+
pageParams: ['', 'MjAyMC0wOC0xMSAxNzozMDowMiswMDowMHwxMDA='],
188204
})
189205
)
190206
})
@@ -204,13 +220,24 @@ describe('useReposTeam', () => {
204220
it('throws an error', async () => {
205221
setup({ invalidResponse: true })
206222
const { result } = renderHook(
207-
() => useReposTeam({ owner: 'codecov', activated: true, first: 2 }),
223+
() =>
224+
useInfiniteQueryV5(
225+
ReposTeamQueryOpts({
226+
provider: 'gh',
227+
activated: true,
228+
owner: 'codecov',
229+
first: 2,
230+
})
231+
),
208232
{ wrapper }
209233
)
210234

211235
await waitFor(() =>
212236
expect(result.current.error).toEqual(
213-
expect.objectContaining({ status: 404 })
237+
expect.objectContaining({
238+
status: 404,
239+
dev: 'ReposTeamQueryOpts - 404 Failed to parse schema',
240+
})
214241
)
215242
)
216243
})

src/services/repos/useReposTeam.tsx renamed to src/services/repos/ReposTeamQueryOpts.tsx

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
import { useInfiniteQuery } from '@tanstack/react-query'
2-
import { useParams } from 'react-router-dom'
1+
import { infiniteQueryOptions as infiniteQueryOptionsV5 } from '@tanstack/react-queryV5'
32
import { z } from 'zod'
43

54
import Api from 'shared/api'
5+
import { rejectNetworkError } from 'shared/api/helpers'
66
import { mapEdges } from 'shared/utils/graphql'
77

8-
import { orderingOptions } from './config'
8+
import {
9+
nonActiveOrderingOptions,
10+
OrderingDirection,
11+
orderingOptions,
12+
TeamOrdering,
13+
} from './orderingOptions'
914

1015
const RepositorySchema = z
1116
.object({
@@ -27,7 +32,7 @@ const RepositorySchema = z
2732
})
2833
.nullable()
2934

30-
export type Repository = z.infer<typeof RepositorySchema>
35+
type Repository = z.infer<typeof RepositorySchema>
3136

3237
const RequestSchema = z.object({
3338
owner: z
@@ -92,7 +97,8 @@ const query = `query GetReposTeam(
9297
}
9398
}`
9499

95-
interface UseReposTeamArgs {
100+
interface ReposTeamQueryArgs {
101+
provider: string
96102
activated?: boolean
97103
term?: string
98104
owner: string
@@ -104,24 +110,23 @@ interface UseReposTeamArgs {
104110
repoNames?: string[]
105111
}
106112

107-
export function useReposTeam({
113+
function ReposTeamQueryOpts({
114+
provider,
108115
activated,
109116
term,
110117
owner,
111118
sortItem = orderingOptions[0],
112119
first = 20,
113120
repoNames,
114-
...options
115-
}: UseReposTeamArgs) {
116-
const { provider } = useParams<{ provider: string }>()
121+
}: ReposTeamQueryArgs) {
117122
const variables = {
118123
filters: { activated, term, repoNames },
119124
ordering: sortItem?.ordering,
120125
direction: sortItem?.direction,
121126
first,
122127
}
123128

124-
return useInfiniteQuery({
129+
return infiniteQueryOptionsV5({
125130
queryKey: ['GetReposTeam', provider, variables, owner],
126131
queryFn: ({ pageParam, signal }) => {
127132
return Api.graphql({
@@ -137,9 +142,11 @@ export function useReposTeam({
137142
const parsedRes = RequestSchema.safeParse(res?.data)
138143

139144
if (!parsedRes.success) {
140-
return Promise.reject({
145+
return rejectNetworkError({
141146
status: 404,
142-
data: null,
147+
data: {},
148+
dev: 'ReposTeamQueryOpts - 404 Failed to parse schema',
149+
error: parsedRes.error,
143150
})
144151
}
145152

@@ -152,8 +159,21 @@ export function useReposTeam({
152159
}
153160
})
154161
},
155-
getNextPageParam: (data) =>
156-
data?.pageInfo?.hasNextPage ? data.pageInfo.endCursor : undefined,
157-
...options,
162+
initialPageParam: '',
163+
getNextPageParam: (data) => {
164+
if (data?.pageInfo?.hasNextPage) {
165+
return data.pageInfo.endCursor
166+
}
167+
return null
168+
},
158169
})
159170
}
171+
172+
export {
173+
type Repository,
174+
orderingOptions,
175+
nonActiveOrderingOptions,
176+
OrderingDirection,
177+
ReposTeamQueryOpts,
178+
TeamOrdering,
179+
}

src/services/repos/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
export * from './useRepos'
2-
export * from './config'
3-
export * from './useReposTeam'
2+
export * from './orderingOptions'
File renamed without changes.

src/services/repos/useRepos.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { RepositoryConfigSchema } from 'services/repo/useRepoConfig'
55
import Api from 'shared/api'
66
import { mapEdges } from 'shared/utils/graphql'
77

8-
import { orderingOptions } from './config'
8+
import { orderingOptions } from './orderingOptions'
99

1010
const RepositorySchema = z
1111
.object({

0 commit comments

Comments
 (0)