Skip to content

Commit 1d02c19

Browse files
committed
Add tests for RepoRow expansion
1 parent 3167cee commit 1d02c19

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export const RepoRow = ({
9191
return;
9292
}
9393

94-
if (resultsLoaded) {
94+
if (resultsLoaded || status !== VariantAnalysisRepoStatus.Succeeded) {
9595
setExpanded(oldIsExpanded => !oldIsExpanded);
9696
return;
9797
}
@@ -102,15 +102,17 @@ export const RepoRow = ({
102102
});
103103

104104
setResultsLoading(true);
105-
}, [resultsLoading, resultsLoaded, repository.fullName]);
105+
}, [resultsLoading, resultsLoaded, repository.fullName, status]);
106106

107107
useEffect(() => {
108-
if (resultsLoaded) {
108+
if (resultsLoaded && resultsLoading) {
109109
setResultsLoading(false);
110+
setExpanded(true);
110111
}
111-
}, [resultsLoaded]);
112+
}, [resultsLoaded, resultsLoading]);
112113

113114
const disabled = !status || !isCompletedAnalysisRepoStatus(status);
115+
const expandableContentLoaded = status && (status !== VariantAnalysisRepoStatus.Succeeded || resultsLoaded);
114116

115117
return (
116118
<div>
@@ -130,7 +132,7 @@ export const RepoRow = ({
130132
</span>
131133
{downloadStatus === VariantAnalysisScannedRepositoryDownloadStatus.InProgress && <LoadingIcon label="Downloading" />}
132134
</TitleContainer>
133-
{isExpanded && resultsLoaded && status &&
135+
{isExpanded && expandableContentLoaded &&
134136
<AnalyzedRepoItemContent status={status} interpretedResults={interpretedResults} rawResults={rawResults} />}
135137
</div>
136138
);

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,56 @@ describe(RepoRow.name, () => {
164164
screen.getByText('Error: Timed out');
165165
});
166166

167+
it('can expand the repo item when succeeded and loaded', async () => {
168+
render({
169+
status: VariantAnalysisRepoStatus.Succeeded,
170+
interpretedResults: [],
171+
});
172+
173+
await userEvent.click(screen.getByRole('button', {
174+
expanded: false
175+
}));
176+
177+
expect(screen.getByRole('button', {
178+
expanded: true,
179+
})).toBeInTheDocument();
180+
});
181+
182+
it('can expand the repo item when succeeded and not loaded', async () => {
183+
const { rerender } = render({
184+
status: VariantAnalysisRepoStatus.Succeeded,
185+
});
186+
187+
await userEvent.click(screen.getByRole('button', {
188+
expanded: false
189+
}));
190+
191+
expect((window as any).vsCodeApi.postMessage).toHaveBeenCalledWith({
192+
t: 'requestRepositoryResults',
193+
repositoryFullName: 'octodemo/hello-world-1',
194+
});
195+
196+
expect(screen.getByRole('button', {
197+
expanded: false,
198+
})).toBeInTheDocument();
199+
200+
rerender(
201+
<RepoRow
202+
repository={{
203+
id: 1,
204+
fullName: 'octodemo/hello-world-1',
205+
private: false,
206+
}}
207+
status={VariantAnalysisRepoStatus.Succeeded}
208+
interpretedResults={[]}
209+
/>
210+
);
211+
212+
expect(screen.getByRole('button', {
213+
expanded: true,
214+
})).toBeInTheDocument();
215+
});
216+
167217
it('does not allow expanding the repo item when status is undefined', async () => {
168218
render({
169219
status: undefined,

extensions/ql-vscode/test/jest.setup.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@ Object.defineProperty(window, 'matchMedia', {
1414
dispatchEvent: jest.fn(),
1515
})),
1616
});
17+
18+
// Store this on the window so we can mock it
19+
(window as any).vsCodeApi = {
20+
postMessage: jest.fn(),
21+
setState: jest.fn(),
22+
};
23+
24+
(window as any).acquireVsCodeApi = () => (window as any).vsCodeApi;

0 commit comments

Comments
 (0)