Add tests for RepoRow expansion

This commit is contained in:
Koen Vlaswinkel
2022-10-07 14:00:46 +02:00
parent 3167ceec91
commit 1d02c19854
3 changed files with 65 additions and 5 deletions

View File

@@ -91,7 +91,7 @@ export const RepoRow = ({
return;
}
if (resultsLoaded) {
if (resultsLoaded || status !== VariantAnalysisRepoStatus.Succeeded) {
setExpanded(oldIsExpanded => !oldIsExpanded);
return;
}
@@ -102,15 +102,17 @@ export const RepoRow = ({
});
setResultsLoading(true);
}, [resultsLoading, resultsLoaded, repository.fullName]);
}, [resultsLoading, resultsLoaded, repository.fullName, status]);
useEffect(() => {
if (resultsLoaded) {
if (resultsLoaded && resultsLoading) {
setResultsLoading(false);
setExpanded(true);
}
}, [resultsLoaded]);
}, [resultsLoaded, resultsLoading]);
const disabled = !status || !isCompletedAnalysisRepoStatus(status);
const expandableContentLoaded = status && (status !== VariantAnalysisRepoStatus.Succeeded || resultsLoaded);
return (
<div>
@@ -130,7 +132,7 @@ export const RepoRow = ({
</span>
{downloadStatus === VariantAnalysisScannedRepositoryDownloadStatus.InProgress && <LoadingIcon label="Downloading" />}
</TitleContainer>
{isExpanded && resultsLoaded && status &&
{isExpanded && expandableContentLoaded &&
<AnalyzedRepoItemContent status={status} interpretedResults={interpretedResults} rawResults={rawResults} />}
</div>
);

View File

@@ -164,6 +164,56 @@ describe(RepoRow.name, () => {
screen.getByText('Error: Timed out');
});
it('can expand the repo item when succeeded and loaded', async () => {
render({
status: VariantAnalysisRepoStatus.Succeeded,
interpretedResults: [],
});
await userEvent.click(screen.getByRole('button', {
expanded: false
}));
expect(screen.getByRole('button', {
expanded: true,
})).toBeInTheDocument();
});
it('can expand the repo item when succeeded and not loaded', async () => {
const { rerender } = render({
status: VariantAnalysisRepoStatus.Succeeded,
});
await userEvent.click(screen.getByRole('button', {
expanded: false
}));
expect((window as any).vsCodeApi.postMessage).toHaveBeenCalledWith({
t: 'requestRepositoryResults',
repositoryFullName: 'octodemo/hello-world-1',
});
expect(screen.getByRole('button', {
expanded: false,
})).toBeInTheDocument();
rerender(
<RepoRow
repository={{
id: 1,
fullName: 'octodemo/hello-world-1',
private: false,
}}
status={VariantAnalysisRepoStatus.Succeeded}
interpretedResults={[]}
/>
);
expect(screen.getByRole('button', {
expanded: true,
})).toBeInTheDocument();
});
it('does not allow expanding the repo item when status is undefined', async () => {
render({
status: undefined,

View File

@@ -14,3 +14,11 @@ Object.defineProperty(window, 'matchMedia', {
dispatchEvent: jest.fn(),
})),
});
// Store this on the window so we can mock it
(window as any).vsCodeApi = {
postMessage: jest.fn(),
setState: jest.fn(),
};
(window as any).acquireVsCodeApi = () => (window as any).vsCodeApi;