Skip to content

Commit ba78b25

Browse files
feat: Hide BA trend chart if Timescale is disabled (#3708)
1 parent 521e544 commit ba78b25

File tree

6 files changed

+132
-156
lines changed

6 files changed

+132
-156
lines changed

src/pages/RepoPage/BundlesTab/BundleContent/BundleContent.test.tsx

+55-25
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,24 @@ vi.mock('./BundleSelection', () => ({
1818
default: () => <div>BundleSelection</div>,
1919
}))
2020

21-
const mockRepoOverview = {
21+
const mockRepoOverview = (hasDefaultBranch: boolean) => ({
2222
owner: {
2323
isCurrentUserActivated: true,
2424
repository: {
2525
__typename: 'Repository',
2626
private: false,
27-
defaultBranch: 'main',
27+
defaultBranch: hasDefaultBranch ? 'main' : null,
2828
oldestCommitAt: '2022-10-10T11:59:59',
2929
coverageEnabled: true,
3030
bundleAnalysisEnabled: true,
3131
languages: ['javascript'],
3232
testAnalyticsEnabled: true,
3333
},
3434
},
35-
}
35+
})
3636

37-
const mockBranchBundles = {
37+
const mockBranchBundles = (isTimescaleEnabled: boolean) => ({
38+
config: { isTimescaleEnabled },
3839
owner: {
3940
repository: {
4041
__typename: 'Repository',
@@ -63,9 +64,10 @@ const mockBranchBundles = {
6364
},
6465
},
6566
},
66-
}
67+
})
6768

6869
const mockBranchBundlesError = {
70+
config: { isTimescaleEnabled: false },
6971
owner: {
7072
repository: {
7173
__typename: 'Repository',
@@ -85,6 +87,7 @@ const mockBranchBundlesError = {
8587
}
8688

8789
const mockEmptyBundleSelection = {
90+
config: { isTimescaleEnabled: false },
8891
owner: {
8992
repository: {
9093
__typename: 'Repository',
@@ -113,14 +116,8 @@ const mockAssets = {
113116
routes: ['/'],
114117
extension: 'js',
115118
bundleData: {
116-
loadTime: {
117-
threeG: 2000,
118-
highSpeed: 2000,
119-
},
120-
size: {
121-
uncompress: 3000,
122-
gzip: 4000,
123-
},
119+
loadTime: { threeG: 2000, highSpeed: 2000 },
120+
size: { uncompress: 3000, gzip: 4000 },
124121
},
125122
measurements: {
126123
change: { size: { uncompress: 5 } },
@@ -217,14 +214,8 @@ const mockBundleSummary = {
217214
name: 'bundle1',
218215
moduleCount: 10,
219216
bundleData: {
220-
loadTime: {
221-
threeG: 1000,
222-
highSpeed: 500,
223-
},
224-
size: {
225-
gzip: 1000,
226-
uncompress: 2000,
227-
},
217+
loadTime: { threeG: 1000, highSpeed: 500 },
218+
size: { gzip: 1000, uncompress: 2000 },
228219
},
229220
},
230221
},
@@ -282,12 +273,16 @@ afterAll(() => {
282273
interface SetupArgs {
283274
isBundleError?: boolean
284275
isEmptyBundleSelection?: boolean
276+
isTimescaleEnabled?: boolean
277+
hasDefaultBranch?: boolean
285278
}
286279

287280
describe('BundleContent', () => {
288281
function setup({
289282
isBundleError = false,
290283
isEmptyBundleSelection = false,
284+
isTimescaleEnabled = true,
285+
hasDefaultBranch = true,
291286
}: SetupArgs) {
292287
server.use(
293288
graphql.query('BranchBundleSummaryData', () => {
@@ -296,10 +291,12 @@ describe('BundleContent', () => {
296291
} else if (isEmptyBundleSelection) {
297292
return HttpResponse.json({ data: mockEmptyBundleSelection })
298293
}
299-
return HttpResponse.json({ data: mockBranchBundles })
294+
return HttpResponse.json({
295+
data: mockBranchBundles(isTimescaleEnabled),
296+
})
300297
}),
301298
graphql.query('GetRepoOverview', () => {
302-
return HttpResponse.json({ data: mockRepoOverview })
299+
return HttpResponse.json({ data: mockRepoOverview(hasDefaultBranch) })
303300
}),
304301
graphql.query('BundleAssets', () => {
305302
if (isBundleError) {
@@ -398,6 +395,39 @@ describe('BundleContent', () => {
398395
expect(moduleCount).toBeInTheDocument()
399396
})
400397
})
398+
399+
describe('rendering the trend chart', () => {
400+
describe('when timescale is enabled', () => {
401+
it('renders the trend chart', async () => {
402+
setup({ isTimescaleEnabled: true })
403+
render(<BundleContent />, {
404+
wrapper: wrapper(
405+
'/gh/codecov/test-repo/bundles/main/test-bundle'
406+
),
407+
})
408+
409+
const chart = await screen.findByText('Hide chart')
410+
expect(chart).toBeInTheDocument()
411+
})
412+
})
413+
414+
describe('when timescale is disabled', () => {
415+
it('renders the trend chart', async () => {
416+
setup({ isTimescaleEnabled: false })
417+
render(<BundleContent />, {
418+
wrapper: wrapper(
419+
'/gh/codecov/test-repo/bundles/main/test-bundle'
420+
),
421+
})
422+
423+
const bundleName = await screen.findByText('asset-1')
424+
expect(bundleName).toBeInTheDocument()
425+
426+
const chart = screen.queryByText('Hide chart')
427+
expect(chart).not.toBeInTheDocument()
428+
})
429+
})
430+
})
401431
})
402432

403433
describe('when only the branch is set', () => {
@@ -418,7 +448,7 @@ describe('BundleContent', () => {
418448

419449
describe('when bundle and branch are not set', () => {
420450
it('renders no branch selected banner and empty table', async () => {
421-
setup({})
451+
setup({ hasDefaultBranch: false })
422452
render(<BundleContent />, {
423453
wrapper: wrapper('/gh/codecov/test-repo/bundles'),
424454
})
@@ -463,7 +493,7 @@ describe('BundleContent', () => {
463493
describe('when the bundle type is not BundleAnalysisReport', () => {
464494
describe('there is no branch data and no branch set', () => {
465495
it('renders the info banner', async () => {
466-
setup({ isEmptyBundleSelection: true })
496+
setup({ isEmptyBundleSelection: true, hasDefaultBranch: false })
467497
render(<BundleContent />, {
468498
wrapper: wrapper('/gh/codecov/test-repo/bundles'),
469499
})

src/pages/RepoPage/BundlesTab/BundleContent/BundleContent.tsx

+36-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { useSuspenseQuery as useSuspenseQueryV5 } from '@tanstack/react-queryV5'
12
import { lazy, Suspense } from 'react'
23
import { Switch, useParams } from 'react-router-dom'
34

45
import { SentryRoute } from 'sentry'
56

6-
import { useBranchBundleSummary } from 'services/bundleAnalysis/useBranchBundleSummary'
7+
import { BranchBundleSummaryQueryOpts } from 'services/bundleAnalysis/BranchBundleSummaryQueryOpts'
8+
import { useRepoOverview } from 'services/repo'
79
import Spinner from 'ui/Spinner'
810
import { ToggleElement } from 'ui/ToggleElement'
911

@@ -32,9 +34,30 @@ const Loader = () => (
3234
)
3335

3436
const BundleContent: React.FC = () => {
35-
const { provider, owner, repo, branch, bundle } = useParams<URLParams>()
37+
const {
38+
provider,
39+
owner,
40+
repo,
41+
branch: branchParam,
42+
bundle,
43+
} = useParams<URLParams>()
3644

37-
const { data } = useBranchBundleSummary({ provider, owner, repo, branch })
45+
const { data: repoOverview } = useRepoOverview({
46+
provider,
47+
repo,
48+
owner,
49+
})
50+
51+
const branch = branchParam ?? repoOverview?.defaultBranch ?? null
52+
53+
const { data } = useSuspenseQueryV5(
54+
BranchBundleSummaryQueryOpts({
55+
provider,
56+
owner,
57+
repo,
58+
branch,
59+
})
60+
)
3861

3962
const bundleType =
4063
data?.branch?.head?.bundleAnalysis?.bundleAnalysisReport?.__typename
@@ -49,14 +72,16 @@ const BundleContent: React.FC = () => {
4972
{bundleType === 'BundleAnalysisReport' ? (
5073
<Switch>
5174
<SentryRoute path="/:provider/:owner/:repo/bundles/:branch/:bundle">
52-
<ToggleElement
53-
showButtonContent="Show chart"
54-
hideButtonContent="Hide chart"
55-
localStorageKey="is-bundle-chart-hidden"
56-
toggleRowElement={<TrendDropdown />}
57-
>
58-
<BundleChart />
59-
</ToggleElement>
75+
{data.config.isTimescaleEnabled ? (
76+
<ToggleElement
77+
showButtonContent="Show chart"
78+
hideButtonContent="Hide chart"
79+
localStorageKey="is-bundle-chart-hidden"
80+
toggleRowElement={<TrendDropdown />}
81+
>
82+
<BundleChart />
83+
</ToggleElement>
84+
) : null}
6085
<Suspense fallback={<Loader />}>
6186
<AssetsTable />
6287
</Suspense>

src/pages/RepoPage/BundlesTab/BundleContent/InfoBanner/InfoBanner.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('InfoBanner', () => {
2929

3030
describe('branch and bundle are not defined', () => {
3131
it('renders NoSelectedBranchContent', () => {
32-
render(<InfoBanner />)
32+
render(<InfoBanner branch={null} />)
3333

3434
const header = screen.getByText('No Branch Selected')
3535
expect(header).toBeInTheDocument()

src/pages/RepoPage/BundlesTab/BundleContent/InfoBanner/InfoBanner.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const NoSelectedBundleContent: React.FC = () => {
2323
}
2424

2525
interface InfoBannerProps {
26-
branch?: string
26+
branch: string | null
2727
bundle?: string
2828
}
2929

0 commit comments

Comments
 (0)