-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ea665c
commit 50ed086
Showing
11 changed files
with
351 additions
and
94 deletions.
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
src/pages/RepoPage/BundlesTab/BundleContent/BundleChart/BundleChart.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query' | ||
import { render, screen } from '@testing-library/react' | ||
import { graphql } from 'msw' | ||
import { setupServer } from 'msw/node' | ||
import { MemoryRouter, Route } from 'react-router-dom' | ||
|
||
import { BundleChart } from './BundleChart' | ||
|
||
const mockRepoOverview = { | ||
owner: { | ||
repository: { | ||
__typename: 'Repository', | ||
private: false, | ||
defaultBranch: 'main', | ||
oldestCommitAt: '2022-10-10T11:59:59', | ||
coverageEnabled: true, | ||
bundleAnalysisEnabled: true, | ||
languages: ['typescript'], | ||
testAnalyticsEnabled: false, | ||
}, | ||
}, | ||
} | ||
|
||
const mockBundleTrendData = { | ||
owner: { | ||
repository: { | ||
__typename: 'Repository', | ||
branch: { | ||
head: { | ||
bundleAnalysisReport: { | ||
__typename: 'BundleAnalysisReport', | ||
bundle: { | ||
measurements: [ | ||
{ | ||
assetType: 'REPORT_SIZE', | ||
measurements: [ | ||
{ | ||
timestamp: '2024-06-15T00:00:00+00:00', | ||
avg: null, | ||
}, | ||
{ | ||
timestamp: '2024-06-16T00:00:00+00:00', | ||
avg: null, | ||
}, | ||
{ | ||
timestamp: '2024-06-17T00:00:00+00:00', | ||
avg: 6834699.8, | ||
}, | ||
{ | ||
timestamp: '2024-06-18T00:00:00+00:00', | ||
avg: 6822037.27273, | ||
}, | ||
{ | ||
timestamp: '2024-06-19T00:00:00+00:00', | ||
avg: 6824833.33333, | ||
}, | ||
{ | ||
timestamp: '2024-06-20T00:00:00+00:00', | ||
avg: 6812341, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
const queryClient = new QueryClient() | ||
const wrapper: React.FC<React.PropsWithChildren> = ({ children }) => ( | ||
<QueryClientProvider client={queryClient}> | ||
<MemoryRouter | ||
initialEntries={['/gh/codecov/test-repo/bundles/main/test-bundle']} | ||
> | ||
<Route path="/:provider/:owner/:repo/bundles/:branch/:bundle"> | ||
{children} | ||
</Route> | ||
</MemoryRouter> | ||
</QueryClientProvider> | ||
) | ||
|
||
const server = setupServer() | ||
|
||
beforeAll(() => { | ||
server.listen() | ||
}) | ||
|
||
afterEach(() => { | ||
queryClient.clear() | ||
server.resetHandlers() | ||
}) | ||
|
||
afterAll(() => { | ||
server.close() | ||
}) | ||
|
||
describe('BundleChart', () => { | ||
function setup() { | ||
server.use( | ||
graphql.query('GetBundleTrend', (req, res, ctx) => { | ||
return res(ctx.status(200), ctx.data(mockBundleTrendData)) | ||
}), | ||
graphql.query('GetRepoOverview', (req, res, ctx) => { | ||
return res(ctx.status(200), ctx.data(mockRepoOverview)) | ||
}) | ||
) | ||
} | ||
|
||
it('renders placeholder while loading', () => { | ||
setup() | ||
render(<BundleChart />, { wrapper }) | ||
|
||
const placeholder = screen.getByTestId('bundle-chart-placeholder') | ||
expect(placeholder).toBeInTheDocument() | ||
}) | ||
|
||
it('renders the chart after loading', async () => { | ||
setup() | ||
render(<BundleChart />, { wrapper }) | ||
|
||
const bundleChart = await screen.findByTestId('bundle-trend-chart') | ||
expect(bundleChart).toBeInTheDocument() | ||
}) | ||
}) |
51 changes: 51 additions & 0 deletions
51
src/pages/RepoPage/BundlesTab/BundleContent/BundleChart/BundleChart.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { useParams } from 'react-router-dom' | ||
|
||
import { BundleTrendChart } from 'ui/BundleTrendChart' | ||
|
||
import { TrendDropdown } from './TrendDropdown' | ||
import { useBundleChartData } from './useBundleChartData' | ||
|
||
const Placeholder = () => ( | ||
<div | ||
data-testid="bundle-chart-placeholder" | ||
className="h-[23rem] animate-pulse rounded bg-ds-gray-tertiary" | ||
/> | ||
) | ||
|
||
interface URLParams { | ||
provider: string | ||
owner: string | ||
repo: string | ||
branch: string | ||
bundle: string | ||
} | ||
|
||
export function BundleChart() { | ||
const { provider, owner, repo, branch, bundle } = useParams<URLParams>() | ||
const { data, maxY, multiplier, isLoading } = useBundleChartData({ | ||
provider, | ||
owner, | ||
repo, | ||
branch, | ||
bundle, | ||
}) | ||
|
||
return ( | ||
<div className="pb-4 pt-6"> | ||
<TrendDropdown /> | ||
{isLoading ? ( | ||
<Placeholder /> | ||
) : ( | ||
<BundleTrendChart | ||
title="Bundle Size" | ||
desc="The size of the bundle over time" | ||
data={{ | ||
measurements: data, | ||
maxY, | ||
multiplier, | ||
}} | ||
/> | ||
)} | ||
</div> | ||
) | ||
} |
67 changes: 67 additions & 0 deletions
67
src/pages/RepoPage/BundlesTab/BundleContent/BundleChart/TrendDropdown.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { render, screen } from '@testing-library/react' | ||
import { userEvent } from '@testing-library/user-event' | ||
import { MemoryRouter, Route, useLocation } from 'react-router-dom' | ||
|
||
import qs from 'querystring' | ||
|
||
import { TrendDropdown } from './TrendDropdown' | ||
|
||
let testLocation: ReturnType<typeof useLocation> | ||
const wrapper: React.FC<React.PropsWithChildren> = ({ children }) => ( | ||
<MemoryRouter initialEntries={['/']}> | ||
<Route path="*"> | ||
{({ location }) => { | ||
testLocation = location | ||
return children | ||
}} | ||
</Route> | ||
</MemoryRouter> | ||
) | ||
|
||
describe('TrendDropdown', () => { | ||
function setup() { | ||
const user = userEvent.setup() | ||
return { user } | ||
} | ||
|
||
describe('when the trend is not set', () => { | ||
it('renders the default trend', () => { | ||
render(<TrendDropdown />, { wrapper }) | ||
|
||
const trend = screen.getByText(/3 months/) | ||
expect(trend).toBeInTheDocument() | ||
}) | ||
}) | ||
|
||
describe('when the trend is set', () => { | ||
it('renders the selected trend', async () => { | ||
const { user } = setup() | ||
render(<TrendDropdown />, { wrapper }) | ||
|
||
const trendDropdown = screen.getByText(/3 months/) | ||
await user.click(trendDropdown) | ||
|
||
const option = screen.getByRole('option', { name: /30 days/ }) | ||
await user.click(option) | ||
|
||
const trend = screen.getByRole('button', { | ||
name: /bundle-chart-trend-dropdown/, | ||
}) | ||
expect(trend).toBeInTheDocument() | ||
expect(trend).toHaveTextContent(/30 days/) | ||
}) | ||
|
||
it('updates the URL params', async () => { | ||
const { user } = setup() | ||
render(<TrendDropdown />, { wrapper }) | ||
|
||
const trendDropdown = screen.getByText(/3 months/) | ||
await user.click(trendDropdown) | ||
|
||
const option = screen.getByRole('option', { name: /30 days/ }) | ||
await user.click(option) | ||
|
||
expect(testLocation.search).toBe(`?${qs.stringify({ trend: '30 days' })}`) | ||
}) | ||
}) | ||
}) |
29 changes: 29 additions & 0 deletions
29
src/pages/RepoPage/BundlesTab/BundleContent/BundleChart/TrendDropdown.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useLocationParams } from 'services/navigation' | ||
import { Trend } from 'shared/utils/timeseriesCharts' | ||
import Select from 'ui/Select' | ||
|
||
const defaultQueryParams = { | ||
trend: null, | ||
} | ||
|
||
export function TrendDropdown() { | ||
const { params, updateParams } = useLocationParams(defaultQueryParams) | ||
const items = Object.values(Trend) | ||
|
||
return ( | ||
<h3 className="flex min-w-40 items-center text-sm font-semibold text-ds-gray-octonary"> | ||
<Select | ||
// @ts-expect-error Select needs to be typed | ||
ariaName="bundle-chart-trend-dropdown" | ||
dataMarketing="Bundle chart trend dropdown" | ||
variant="text" | ||
items={items} | ||
onChange={(selected: string) => updateParams({ trend: selected })} | ||
// @ts-expect-error useLocationParams needs to be typed | ||
value={params?.trend || Trend.THREE_MONTHS} | ||
renderItem={(item: string) => <p className="capitalize">{item}</p>} | ||
/> | ||
<span className="text-ds-gray-senary">trend</span> | ||
</h3> | ||
) | ||
} |
1 change: 1 addition & 0 deletions
1
src/pages/RepoPage/BundlesTab/BundleContent/BundleChart/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { BundleChart } from './BundleChart' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.