Skip to content

Commit 3c674b5

Browse files
authored
chore: Migrate useSyncProviders to TS Query V5 (#3573)
1 parent 7200eea commit 3c674b5

File tree

5 files changed

+74
-54
lines changed

5 files changed

+74
-54
lines changed

src/pages/SyncProviderPage/SyncProviderPage.test.tsx

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
2+
import {
3+
QueryClientProvider as QueryClientProviderV5,
4+
QueryClient as QueryClientV5,
5+
} from '@tanstack/react-queryV5'
26
import { render, screen, waitFor } from '@testing-library/react'
37
import { graphql, http, HttpResponse } from 'msw'
48
import { setupServer } from 'msw/node'
@@ -22,30 +26,35 @@ const createMockedInternalUser = (
2226
owners: owner !== undefined ? [owner] : [],
2327
})
2428

29+
const server = setupServer()
2530
const queryClient = new QueryClient({
26-
defaultOptions: { queries: { suspense: true } },
31+
defaultOptions: { queries: { retry: false, suspense: true } },
32+
})
33+
const queryClientV5 = new QueryClientV5({
34+
defaultOptions: { queries: { retry: false } },
2735
})
28-
const server = setupServer()
2936

3037
const testLocation: { pathname: string } = {
3138
pathname: '',
3239
}
3340

3441
const wrapper: React.FC<React.PropsWithChildren> = ({ children }) => (
35-
<QueryClientProvider client={queryClient}>
36-
<MemoryRouter initialEntries={['/sync']}>
37-
<Route path="/sync">
38-
<Suspense fallback={<p>Loading</p>}>{children}</Suspense>
39-
</Route>
40-
<Route
41-
path="*"
42-
render={({ location }) => {
43-
testLocation.pathname = location.pathname
44-
return null
45-
}}
46-
/>
47-
</MemoryRouter>
48-
</QueryClientProvider>
42+
<QueryClientProviderV5 client={queryClientV5}>
43+
<QueryClientProvider client={queryClient}>
44+
<MemoryRouter initialEntries={['/sync']}>
45+
<Route path="/sync">
46+
<Suspense fallback={<p>Loading</p>}>{children}</Suspense>
47+
</Route>
48+
<Route
49+
path="*"
50+
render={({ location }) => {
51+
testLocation.pathname = location.pathname
52+
return null
53+
}}
54+
/>
55+
</MemoryRouter>
56+
</QueryClientProvider>
57+
</QueryClientProviderV5>
4958
)
5059

5160
beforeAll(() => {
@@ -54,6 +63,7 @@ beforeAll(() => {
5463

5564
afterEach(() => {
5665
queryClient.clear()
66+
queryClientV5.clear()
5767
server.resetHandlers()
5868
})
5969

src/pages/SyncProviderPage/SyncProviderPage.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1+
import { useSuspenseQuery as useSuspenseQueryV5 } from '@tanstack/react-queryV5'
12
import gt from 'lodash/gt'
23
import isEmpty from 'lodash/isEmpty'
34
import { Redirect } from 'react-router-dom'
45

5-
import { useSyncProviders } from 'services/config'
6+
import { SyncProvidersQueryOpts } from 'services/config/SyncProvidersQueryOpts'
67
import { useInternalUser } from 'services/user/useInternalUser'
78
import { loginProviderToShortName } from 'shared/utils/loginProviders'
89
import A from 'ui/A'
910

1011
import SyncButton from './SyncButton'
1112

12-
interface SyncButtonProps {
13-
hasSynced: boolean
14-
}
15-
16-
const SyncButtons: React.FC<SyncButtonProps> = ({ hasSynced }) => {
17-
const { data: syncProviders } = useSyncProviders({ enabled: !hasSynced })
13+
const SyncButtons: React.FC = () => {
14+
const { data: syncProviders } = useSuspenseQueryV5(SyncProvidersQueryOpts())
1815

1916
if (isEmpty(syncProviders)) {
2017
return (
@@ -80,7 +77,7 @@ const SyncProviderPage: React.FC = () => {
8077
</p>
8178
</div>
8279
<div className="mx-auto mt-2 w-96 space-y-4 border-t border-ds-gray-secondary pt-4">
83-
<SyncButtons hasSynced={hasSynced} />
80+
<SyncButtons />
8481
</div>
8582
</div>
8683
)

src/services/config/useSyncProviders.test.tsx renamed to src/services/config/SyncProvidersQueryOpts.test.tsx

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,45 @@
11
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
2+
import {
3+
QueryClientProvider as QueryClientProviderV5,
4+
QueryClient as QueryClientV5,
5+
useQuery as useQueryV5,
6+
} from '@tanstack/react-queryV5'
27
import { renderHook, waitFor } from '@testing-library/react'
38
import { graphql, HttpResponse } from 'msw'
49
import { setupServer } from 'msw/node'
510
import { MemoryRouter, Route } from 'react-router-dom'
611

7-
import { EnterpriseSyncProviders, useSyncProviders } from './useSyncProviders'
12+
import {
13+
EnterpriseSyncProviders,
14+
SyncProvidersQueryOpts,
15+
} from './SyncProvidersQueryOpts'
816

917
const server = setupServer()
1018
const queryClient = new QueryClient({
1119
defaultOptions: { queries: { retry: false } },
1220
})
21+
const queryClientV5 = new QueryClientV5({
22+
defaultOptions: { queries: { retry: false } },
23+
})
1324

1425
const wrapper: React.FC<React.PropsWithChildren> = ({ children }) => (
15-
<QueryClientProvider client={queryClient}>
16-
<MemoryRouter initialEntries={['/']}>
17-
<Route path="/">{children}</Route>
18-
</MemoryRouter>
19-
</QueryClientProvider>
26+
<QueryClientProviderV5 client={queryClientV5}>
27+
<QueryClientProvider client={queryClient}>
28+
<MemoryRouter initialEntries={['/']}>
29+
<Route path="/">{children}</Route>
30+
</MemoryRouter>
31+
</QueryClientProvider>
32+
</QueryClientProviderV5>
2033
)
2134

2235
beforeAll(() => {
2336
server.listen()
2437
})
2538

2639
beforeEach(() => {
27-
server.resetHandlers()
2840
queryClient.clear()
41+
queryClientV5.clear()
42+
server.resetHandlers()
2943
})
3044

3145
afterAll(() => {
@@ -54,13 +68,13 @@ describe('useSyncProviders', () => {
5468

5569
describe('third party services are configured providers', () => {
5670
it('returns data', async () => {
57-
setup({
58-
syncProviders: ['GITHUB', 'GITLAB', 'BITBUCKET'],
59-
})
60-
const { result } = renderHook(() => useSyncProviders({}), { wrapper })
71+
setup({ syncProviders: ['GITHUB', 'GITLAB', 'BITBUCKET'] })
72+
const { result } = renderHook(
73+
() => useQueryV5(SyncProvidersQueryOpts()),
74+
{ wrapper }
75+
)
6176

6277
await waitFor(() => result.current.isSuccess)
63-
6478
await waitFor(() =>
6579
expect(result.current.data).toStrictEqual(['gh', 'gl', 'bb'])
6680
)
@@ -76,10 +90,12 @@ describe('useSyncProviders', () => {
7690
'BITBUCKET_SERVER',
7791
],
7892
})
79-
const { result } = renderHook(() => useSyncProviders({}), { wrapper })
93+
const { result } = renderHook(
94+
() => useQueryV5(SyncProvidersQueryOpts()),
95+
{ wrapper }
96+
)
8097

8198
await waitFor(() => result.current.isSuccess)
82-
8399
await waitFor(() =>
84100
expect(result.current.data).toStrictEqual(['ghe', 'gle', 'bbs'])
85101
)
@@ -96,14 +112,13 @@ describe('useSyncProviders', () => {
96112
})
97113

98114
it('throws an error', async () => {
99-
setup({
100-
hasParsingError: true,
101-
})
102-
103-
const { result } = renderHook(() => useSyncProviders({}), { wrapper })
115+
setup({ hasParsingError: true })
116+
const { result } = renderHook(
117+
() => useQueryV5(SyncProvidersQueryOpts()),
118+
{ wrapper }
119+
)
104120

105121
await waitFor(() => expect(result.current.isError).toBeTruthy())
106-
107122
expect(result.current.error).toEqual(
108123
expect.objectContaining({ status: 404 })
109124
)

src/services/config/useSyncProviders.ts renamed to src/services/config/SyncProvidersQueryOpts.ts

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

44
import Api from 'shared/api'
5+
import { rejectNetworkError } from 'shared/api/helpers'
56

67
const EnterpriseSyncProvidersUnionSchema = z.union([
78
z.literal('GITHUB'),
@@ -29,12 +30,8 @@ query GetSyncProviders {
2930
}
3031
}`
3132

32-
interface UseSyncProvidersArgs {
33-
enabled?: boolean
34-
}
35-
36-
export const useSyncProviders = ({ enabled = true }: UseSyncProvidersArgs) => {
37-
return useQuery({
33+
export const SyncProvidersQueryOpts = () => {
34+
return queryOptionsV5({
3835
queryKey: ['GetSyncProviders'],
3936
queryFn: ({ signal }) => {
4037
return Api.graphql({
@@ -45,8 +42,11 @@ export const useSyncProviders = ({ enabled = true }: UseSyncProvidersArgs) => {
4542
const parsedRes = GetSyncProvidersSchema.safeParse(res?.data)
4643

4744
if (!parsedRes.success) {
48-
return Promise.reject({
45+
return rejectNetworkError({
4946
status: 404,
47+
data: {},
48+
dev: `SyncProvidersQueryOpts - 404 Failed to parse`,
49+
error: parsedRes.error,
5050
})
5151
}
5252

@@ -81,6 +81,5 @@ export const useSyncProviders = ({ enabled = true }: UseSyncProvidersArgs) => {
8181
return syncProviders
8282
})
8383
},
84-
enabled,
8584
})
8685
}

src/services/config/index.ts

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

0 commit comments

Comments
 (0)