Skip to content

Commit d4d8342

Browse files
authored
chore: Migrate useComponentComparison to TS Query V5 (#3539)
1 parent 19444c6 commit d4d8342

File tree

6 files changed

+258
-189
lines changed

6 files changed

+258
-189
lines changed

src/pages/PullRequestPage/PullCoverage/routes/ComponentsTab/ComponentsTable.test.jsx

Lines changed: 65 additions & 48 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, HttpResponse } from 'msw'
48
import { setupServer } from 'msw/node'
@@ -10,31 +14,23 @@ vi.mock('./ComponentsNotConfigured', () => ({
1014
default: () => 'ComponentsNotConfigured',
1115
}))
1216

13-
const queryClient = new QueryClient()
14-
const server = setupServer()
15-
16-
const wrapper =
17-
(initialEntries = '/gh/codecov/gazebo/pull/123/components') =>
18-
({ children }) => (
19-
<QueryClientProvider client={queryClient}>
20-
<MemoryRouter initialEntries={[initialEntries]}>
21-
<Route path="/:provider/:owner/:repo/pull/:pullId/components">
22-
{children}
23-
</Route>
24-
</MemoryRouter>
25-
</QueryClientProvider>
26-
)
27-
28-
beforeAll(() => {
29-
server.listen()
30-
})
31-
afterEach(() => {
32-
queryClient.clear()
33-
server.resetHandlers()
34-
})
35-
afterAll(() => {
36-
server.close()
37-
})
17+
const mockPullComponentsResponse = {
18+
owner: {
19+
repository: {
20+
__typename: 'Repository',
21+
pull: {
22+
compareWithBase: {
23+
__typename: 'Comparison',
24+
componentComparisons: [
25+
{ name: 'component-1' },
26+
{ name: 'component-2' },
27+
{ name: 'component-3' },
28+
],
29+
},
30+
},
31+
},
32+
},
33+
}
3834

3935
const mockPull = {
4036
owner: {
@@ -46,26 +42,52 @@ const mockPull = {
4642
componentComparisons: [
4743
{
4844
name: 'secondTest',
49-
headTotals: {
50-
percentCovered: 82.71,
51-
},
52-
baseTotals: {
53-
percentCovered: 80.0,
54-
},
55-
patchTotals: {
56-
percentCovered: 59.0,
57-
},
45+
headTotals: { percentCovered: 82.71 },
46+
baseTotals: { percentCovered: 80.0 },
47+
patchTotals: { percentCovered: 59.0 },
5848
},
5949
],
6050
},
61-
head: {
62-
branchName: 'abc',
63-
},
51+
head: { branchName: 'abc' },
6452
},
6553
},
6654
},
6755
}
6856

57+
const server = setupServer()
58+
const queryClient = new QueryClient({
59+
defaultOptions: { queries: { retry: false } },
60+
})
61+
const queryClientV5 = new QueryClientV5({
62+
defaultOptions: { queries: { retry: false } },
63+
})
64+
65+
const wrapper =
66+
(initialEntries = '/gh/codecov/gazebo/pull/123/components') =>
67+
({ children }) => (
68+
<QueryClientProviderV5 client={queryClientV5}>
69+
<QueryClientProvider client={queryClient}>
70+
<MemoryRouter initialEntries={[initialEntries]}>
71+
<Route path="/:provider/:owner/:repo/pull/:pullId/components">
72+
{children}
73+
</Route>
74+
</MemoryRouter>
75+
</QueryClientProvider>
76+
</QueryClientProviderV5>
77+
)
78+
79+
beforeAll(() => {
80+
server.listen()
81+
})
82+
afterEach(() => {
83+
queryClient.clear()
84+
queryClientV5.clear()
85+
server.resetHandlers()
86+
})
87+
afterAll(() => {
88+
server.close()
89+
})
90+
6991
describe('ComponentsTable', () => {
7092
function setup(overrideData) {
7193
const componentsMock = vi.fn()
@@ -79,22 +101,19 @@ describe('ComponentsTable', () => {
79101
if (overrideData) {
80102
return HttpResponse.json({ data: overrideData })
81103
}
82-
83104
return HttpResponse.json({ data: mockPull })
105+
}),
106+
graphql.query('PullComponentsSelector', () => {
107+
return HttpResponse.json({ data: mockPullComponentsResponse })
84108
})
85109
)
86110

87111
return { componentsMock }
88112
}
89113

90114
describe('when there are no components in the new tab', () => {
91-
beforeEach(() => {
92-
setup({
93-
owner: null,
94-
})
95-
})
96-
97115
it('will render card with no dismiss button', async () => {
116+
setup({ owner: null })
98117
render(<ComponentsTable />, { wrapper: wrapper() })
99118

100119
const componentNotConfigured = await screen.findByText(
@@ -105,11 +124,8 @@ describe('ComponentsTable', () => {
105124
})
106125

107126
describe('when rendered with populated data in the new tab', () => {
108-
beforeEach(() => {
109-
setup()
110-
})
111-
112127
it('shows title and body', async () => {
128+
setup()
113129
render(<ComponentsTable />, { wrapper: wrapper() })
114130

115131
const nameTableField = await screen.findByText(`Name`)
@@ -158,6 +174,7 @@ describe('ComponentsTable', () => {
158174

159175
describe('when loading', () => {
160176
it('renders spinner', () => {
177+
setup()
161178
render(<ComponentsTable />, { wrapper: wrapper() })
162179

163180
const spinner = screen.getByTestId('spinner')

src/pages/PullRequestPage/PullCoverage/routes/ComponentsTab/ComponentsTable.tsx

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useQuery as useQueryV5 } from '@tanstack/react-queryV5'
12
import {
23
createColumnHelper,
34
flexRender,
@@ -8,14 +9,17 @@ import cs from 'classnames'
89
import isArray from 'lodash/isArray'
910
import qs, { type ParsedQs } from 'qs'
1011
import { useMemo } from 'react'
11-
import { useLocation } from 'react-router-dom'
12+
import { useLocation, useParams } from 'react-router-dom'
1213

1314
import A from 'ui/A'
1415
import Spinner from 'ui/Spinner'
1516
import TotalsNumber from 'ui/TotalsNumber'
1617

1718
import ComponentsNotConfigured from './ComponentsNotConfigured'
18-
import { ComponentsComparison, useComponentComparison } from './hooks'
19+
import {
20+
ComponentComparisonQueryOpts,
21+
ComponentsComparison,
22+
} from './queries/ComponentComparisonQueryOpts'
1923

2024
import ComponentsSelector from '../ComponentsSelector'
2125

@@ -91,7 +95,15 @@ function getFilters({ components }: { components?: ParsedQs[] | string[] }) {
9195
}
9296
}
9397

98+
interface URLParams {
99+
provider: string
100+
owner: string
101+
repo: string
102+
pullId: string
103+
}
104+
94105
export default function ComponentsTable() {
106+
const { provider, owner, repo, pullId } = useParams<URLParams>()
95107
const location = useLocation()
96108
const queryParams = qs.parse(location.search, {
97109
ignoreQueryPrefix: true,
@@ -104,9 +116,15 @@ export default function ComponentsTable() {
104116
}
105117

106118
const filters = getFilters({ components })
107-
const { data, isLoading } = useComponentComparison({
108-
filters,
109-
})
119+
const { data, isLoading } = useQueryV5(
120+
ComponentComparisonQueryOpts({
121+
provider,
122+
owner,
123+
repo,
124+
pullId,
125+
filters,
126+
})
127+
)
110128

111129
const tableData = useMemo(() => {
112130
if (

src/pages/PullRequestPage/PullCoverage/routes/ComponentsTab/hooks/index.ts

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

src/pages/PullRequestPage/PullCoverage/routes/ComponentsTab/hooks/query.ts

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)