Skip to content

Commit cc8f304

Browse files
committed
Add tests for duration calculation
This adds tests for the duration calculation and moves it down a component to make this easier. Adding tests for the `VariantAnalysisHeader` would require constructing a complete variant analysis object, while this is now just a simple unit test.
1 parent 1ca623f commit cc8f304

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

extensions/ql-vscode/src/view/variant-analysis/VariantAnalysisHeader.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,6 @@ export const VariantAnalysisHeader = ({
5959
return getSkippedRepoCount(variantAnalysis.skippedRepos) > 0;
6060
}, [variantAnalysis.skippedRepos]);
6161

62-
const duration = useMemo(() => {
63-
if (!variantAnalysis?.completedAt) {
64-
return undefined;
65-
}
66-
67-
const createdAt = parseDate(variantAnalysis.createdAt);
68-
const completedAt = parseDate(variantAnalysis.completedAt);
69-
return completedAt.getTime() - createdAt.getTime();
70-
}, [variantAnalysis?.completedAt, variantAnalysis?.createdAt]);
71-
7262
return (
7363
<Container>
7464
<Row>
@@ -91,7 +81,7 @@ export const VariantAnalysisHeader = ({
9181
completedRepositoryCount={completedRepositoryCount}
9282
resultCount={resultCount}
9383
hasWarnings={hasSkippedRepos}
94-
duration={duration}
84+
createdAt={parseDate(variantAnalysis.createdAt)}
9585
completedAt={parseDate(variantAnalysis.completedAt)}
9686
onViewLogsClick={onViewLogsClick}
9787
/>

extensions/ql-vscode/src/view/variant-analysis/VariantAnalysisStats.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export type VariantAnalysisStatsProps = {
1717
hasWarnings?: boolean;
1818

1919
resultCount?: number | undefined;
20-
duration?: number | undefined;
20+
createdAt: Date;
2121
completedAt?: Date | undefined;
2222

2323
onViewLogsClick: () => void;
@@ -35,7 +35,7 @@ export const VariantAnalysisStats = ({
3535
completedRepositoryCount = 0,
3636
hasWarnings,
3737
resultCount,
38-
duration,
38+
createdAt,
3939
completedAt,
4040
onViewLogsClick,
4141
}: VariantAnalysisStatsProps) => {
@@ -59,6 +59,14 @@ export const VariantAnalysisStats = ({
5959
return 'Succeeded';
6060
}, [variantAnalysisStatus, hasWarnings]);
6161

62+
const duration = useMemo(() => {
63+
if (!completedAt) {
64+
return undefined;
65+
}
66+
67+
return completedAt.getTime() - createdAt.getTime();
68+
}, [completedAt, createdAt]);
69+
6270
return (
6371
<Row>
6472
<StatItem title="Results">

extensions/ql-vscode/src/view/variant-analysis/__tests__/VariantAnalysisStats.spec.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe(VariantAnalysisStats.name, () => {
1717
variantAnalysisStatus={VariantAnalysisStatus.InProgress}
1818
totalRepositoryCount={10}
1919
onViewLogsClick={onViewLogsClick}
20+
createdAt={new Date()}
2021
{...props}
2122
/>
2223
);
@@ -100,4 +101,22 @@ describe(VariantAnalysisStats.name, () => {
100101
expect(screen.getByText('Succeeded')).toBeInTheDocument();
101102
expect(screen.queryByText('Succeeded warnings')).not.toBeInTheDocument();
102103
});
104+
105+
it('renders the duration when it is less than a second', () => {
106+
render({ createdAt: new Date('2021-05-01T00:00:00Z'), completedAt: new Date('2021-05-01T00:00:00Z') });
107+
108+
expect(screen.getByText('Less than a second')).toBeInTheDocument();
109+
});
110+
111+
it('renders the duration when it is less than a minute', () => {
112+
render({ createdAt: new Date('2021-05-01T00:00:00Z'), completedAt: new Date('2021-05-01T00:00:34Z') });
113+
114+
expect(screen.getByText('34 seconds')).toBeInTheDocument();
115+
});
116+
117+
it('renders the duration when it is more than a minute', () => {
118+
render({ createdAt: new Date('2021-05-01T00:00:00Z'), completedAt: new Date('2021-05-01T00:10:22Z') });
119+
120+
expect(screen.getByText('10 minutes')).toBeInTheDocument();
121+
});
103122
});

0 commit comments

Comments
 (0)