Skip to content

Commit a1daa91

Browse files
committed
Break down handleItemClicked tests per history item type
We were expecting all three types to behave the same when clicked / double clicked. In fact local & remote queries only allow you to open the results view when they're complete, while variant analyses always allow you to open the results view no matter what their status. Let's break down these tests per history item type and test the expected behaviour more granularly. NB: I tried moving some of the setup in beforeEach blocks, but alas queryHistoryManager can be undefined so rather than adding `?` to every method call, I'm just gonna leave the setup inside the tests. In an ideal world, we'd stop declaring `queryHistoryManager` as `undefined`: ``` let queryHistoryManager: QueryHistoryManager | undefined; ``` Baby steps!
1 parent a21dec7 commit a1daa91

File tree

2 files changed

+105
-42
lines changed

2 files changed

+105
-42
lines changed

extensions/ql-vscode/src/vscode-tests/factories/remote-queries/remote-query-history-item.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import { QueryStatus } from '../../../query-status';
33

44
export function createMockRemoteQueryHistoryItem({
55
date = new Date('2022-01-01T00:00:00.000Z'),
6+
status = QueryStatus.InProgress,
67
failureReason = undefined,
78
resultCount = 16,
89
repositoryCount = 0,
910
userSpecifiedLabel = undefined,
1011
}: {
1112
date?: Date;
13+
status?: QueryStatus;
1214
failureReason?: string;
1315
resultCount?: number;
1416
repositoryCount?: number;
@@ -18,7 +20,7 @@ export function createMockRemoteQueryHistoryItem({
1820
t: 'remote',
1921
failureReason,
2022
resultCount,
21-
status: QueryStatus.InProgress,
23+
status,
2224
completed: false,
2325
queryId: 'queryId',
2426
remoteQuery: {

extensions/ql-vscode/src/vscode-tests/no-workspace/query-history.test.ts

Lines changed: 102 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import * as fs from 'fs-extra';
22
import * as path from 'path';
3-
import { expect, assert } from 'chai';
3+
import { assert, expect } from 'chai';
44
import * as vscode from 'vscode';
55
import * as sinon from 'sinon';
66

77
import { logger } from '../../logging';
88
import { registerQueryHistoryScrubber } from '../../query-history-scrubber';
9-
import { QueryHistoryManager, HistoryTreeDataProvider, SortOrder } from '../../query-history';
9+
import { HistoryTreeDataProvider, QueryHistoryManager, SortOrder } from '../../query-history';
1010
import { QueryHistoryConfig, QueryHistoryConfigListener } from '../../config';
1111
import { LocalQueryInfo } from '../../query-results';
1212
import { DatabaseManager } from '../../databases';
1313
import * as tmp from 'tmp-promise';
14-
import { ONE_DAY_IN_MS, ONE_HOUR_IN_MS, TWO_HOURS_IN_MS, THREE_HOURS_IN_MS } from '../../pure/time';
14+
import { ONE_DAY_IN_MS, ONE_HOUR_IN_MS, THREE_HOURS_IN_MS, TWO_HOURS_IN_MS } from '../../pure/time';
1515
import { tmpDir } from '../../helpers';
1616
import { getErrorMessage } from '../../pure/helpers-pure';
1717
import { HistoryItemLabelProvider } from '../../history-item-label-provider';
@@ -27,6 +27,7 @@ import { RemoteQueryHistoryItem } from '../../remote-queries/remote-query-histor
2727
import { shuffleHistoryItems } from '../utils/query-history-helpers';
2828
import { createMockVariantAnalysisHistoryItem } from '../factories/remote-queries/variant-analysis-history-item';
2929
import { VariantAnalysisHistoryItem } from '../../remote-queries/variant-analysis-history-item';
30+
import { QueryStatus } from '../../query-status';
3031

3132
describe('query-history', () => {
3233
const mockExtensionLocation = path.join(tmpDir.name, 'mock-extension-location');
@@ -93,16 +94,16 @@ describe('query-history', () => {
9394
createMockLocalQuery('a', createMockQueryWithResults(sandbox, true)),
9495
];
9596
remoteQueryHistory = [
96-
createMockRemoteQueryHistoryItem({}),
97-
createMockRemoteQueryHistoryItem({}),
98-
createMockRemoteQueryHistoryItem({}),
99-
createMockRemoteQueryHistoryItem({})
97+
createMockRemoteQueryHistoryItem({ status: QueryStatus.Completed }),
98+
createMockRemoteQueryHistoryItem({ status: QueryStatus.Failed }),
99+
createMockRemoteQueryHistoryItem({ status: QueryStatus.InProgress }),
100+
createMockRemoteQueryHistoryItem({ status: QueryStatus.InProgress })
100101
];
101102
variantAnalysisHistory = [
102-
createMockVariantAnalysisHistoryItem(),
103-
createMockVariantAnalysisHistoryItem(),
104-
createMockVariantAnalysisHistoryItem(),
105-
createMockVariantAnalysisHistoryItem()
103+
createMockVariantAnalysisHistoryItem(QueryStatus.Completed),
104+
createMockVariantAnalysisHistoryItem(QueryStatus.InProgress),
105+
createMockVariantAnalysisHistoryItem(QueryStatus.Failed),
106+
createMockVariantAnalysisHistoryItem(QueryStatus.InProgress)
106107
];
107108
allHistory = shuffleHistoryItems([...localQueryHistory, ...remoteQueryHistory, ...variantAnalysisHistory]);
108109

@@ -158,45 +159,105 @@ describe('query-history', () => {
158159
});
159160
});
160161

161-
describe('handleItemClicked', () => {
162-
it('should call the selectedCallback when an item is clicked', async () => {
163-
queryHistoryManager = await createMockQueryHistory(allHistory);
164-
const itemClicked = allHistory[0];
165-
await queryHistoryManager.handleItemClicked(itemClicked, [itemClicked]);
166-
167-
if (itemClicked.t == 'local') {
168-
expect(localQueriesResultsViewStub.showResults).to.have.been.calledOnceWith(itemClicked);
169-
} else if (itemClicked.t == 'remote') {
170-
expect(remoteQueriesManagerStub.openRemoteQueryResults).to.have.been.calledOnceWith(itemClicked.queryId);
171-
} else if (itemClicked.t == 'variant-analysis') {
172-
expect(variantAnalysisManagerStub.showView).to.have.been.calledOnceWith(itemClicked.variantAnalysis.id);
173-
}
162+
describe('handleItemClicked', async () => {
163+
describe('single click', async () => {
164+
describe('local query', async () => {
165+
describe('when complete', async () => {
166+
it('should show results', async () => {
167+
queryHistoryManager = await createMockQueryHistory(allHistory);
168+
const itemClicked = localQueryHistory[0];
169+
await queryHistoryManager.handleItemClicked(itemClicked, [itemClicked]);
170+
171+
expect(localQueriesResultsViewStub.showResults).to.have.been.calledOnceWith(itemClicked);
172+
expect(queryHistoryManager.treeDataProvider.getCurrent()).to.eq(itemClicked);
173+
});
174+
});
175+
176+
describe('when incomplete', async () => {
177+
it('should do nothing', async () => {
178+
queryHistoryManager = await createMockQueryHistory(allHistory);
179+
const itemClicked = localQueryHistory[2];
180+
await queryHistoryManager.handleItemClicked(itemClicked, [itemClicked]);
181+
182+
expect(localQueriesResultsViewStub.showResults).not.to.have.been.calledWith(itemClicked);
183+
});
184+
});
185+
});
186+
187+
describe('remote query', async () => {
188+
describe('when complete', async () => {
189+
it('should show results', async () => {
190+
queryHistoryManager = await createMockQueryHistory(allHistory);
191+
const itemClicked = remoteQueryHistory[0];
192+
await queryHistoryManager.handleItemClicked(itemClicked, [itemClicked]);
193+
194+
expect(remoteQueriesManagerStub.openRemoteQueryResults).to.have.been.calledOnceWith(itemClicked.queryId);
195+
expect(queryHistoryManager.treeDataProvider.getCurrent()).to.eq(itemClicked);
196+
});
197+
});
198+
199+
describe('when incomplete', async () => {
200+
it('should do nothing', async () => {
201+
queryHistoryManager = await createMockQueryHistory(allHistory);
202+
const itemClicked = remoteQueryHistory[2];
203+
await queryHistoryManager.handleItemClicked(itemClicked, [itemClicked]);
204+
205+
expect(remoteQueriesManagerStub.openRemoteQueryResults).not.to.have.been.calledWith(itemClicked.queryId);
206+
});
207+
});
208+
});
209+
210+
describe('variant analysis', async () => {
211+
describe('when complete', async () => {
212+
it('should show results', async () => {
213+
queryHistoryManager = await createMockQueryHistory(allHistory);
214+
const itemClicked = variantAnalysisHistory[0];
215+
await queryHistoryManager.handleItemClicked(itemClicked, [itemClicked]);
216+
217+
expect(variantAnalysisManagerStub.showView).to.have.been.calledOnceWith(itemClicked.variantAnalysis.id);
218+
expect(queryHistoryManager.treeDataProvider.getCurrent()).to.eq(itemClicked);
219+
});
220+
});
174221

175-
expect(queryHistoryManager.treeDataProvider.getCurrent()).to.eq(itemClicked);
222+
describe('when incomplete', async () => {
223+
it('should show results', async () => {
224+
queryHistoryManager = await createMockQueryHistory(allHistory);
225+
const itemClicked = variantAnalysisHistory[1];
226+
await queryHistoryManager.handleItemClicked(itemClicked, [itemClicked]);
227+
228+
expect(variantAnalysisManagerStub.showView).to.have.been.calledOnceWith(itemClicked.variantAnalysis.id);
229+
expect(queryHistoryManager.treeDataProvider.getCurrent()).to.eq(itemClicked);
230+
});
231+
});
232+
});
176233
});
177234

178-
it('should do nothing if there is a multi-selection', async () => {
179-
queryHistoryManager = await createMockQueryHistory(allHistory);
180-
const itemClicked = allHistory[0];
181-
const secondItemClicked = allHistory[1];
235+
describe('double click', () => {
236+
it('should do nothing', async () => {
237+
queryHistoryManager = await createMockQueryHistory(allHistory);
238+
const itemClicked = allHistory[0];
239+
const secondItemClicked = allHistory[1];
182240

183-
await queryHistoryManager.handleItemClicked(itemClicked, [itemClicked, secondItemClicked]);
241+
await queryHistoryManager.handleItemClicked(itemClicked, [itemClicked, secondItemClicked]);
184242

185-
expect(localQueriesResultsViewStub.showResults).not.to.have.been.called;
186-
expect(remoteQueriesManagerStub.openRemoteQueryResults).not.to.have.been.called;
187-
expect(variantAnalysisManagerStub.showView).not.to.have.been.called;
188-
expect(queryHistoryManager.treeDataProvider.getCurrent()).to.be.undefined;
243+
expect(localQueriesResultsViewStub.showResults).not.to.have.been.called;
244+
expect(remoteQueriesManagerStub.openRemoteQueryResults).not.to.have.been.called;
245+
expect(variantAnalysisManagerStub.showView).not.to.have.been.called;
246+
expect(queryHistoryManager.treeDataProvider.getCurrent()).to.be.undefined;
247+
});
189248
});
190249

191-
it('should do nothing if there is no selection', async () => {
192-
queryHistoryManager = await createMockQueryHistory(allHistory);
250+
describe('no selection', () => {
251+
it('should do nothing', async () => {
252+
queryHistoryManager = await createMockQueryHistory(allHistory);
193253

194-
await queryHistoryManager.handleItemClicked(undefined!, []);
254+
await queryHistoryManager.handleItemClicked(undefined!, []);
195255

196-
expect(localQueriesResultsViewStub.showResults).not.to.have.been.called;
197-
expect(remoteQueriesManagerStub.openRemoteQueryResults).not.to.have.been.called;
198-
expect(variantAnalysisManagerStub.showView).not.to.have.been.called;
199-
expect(queryHistoryManager.treeDataProvider.getCurrent()).to.be.undefined;
256+
expect(localQueriesResultsViewStub.showResults).not.to.have.been.called;
257+
expect(remoteQueriesManagerStub.openRemoteQueryResults).not.to.have.been.called;
258+
expect(variantAnalysisManagerStub.showView).not.to.have.been.called;
259+
expect(queryHistoryManager.treeDataProvider.getCurrent()).to.be.undefined;
260+
});
200261
});
201262
});
202263

0 commit comments

Comments
 (0)