Skip to content

Commit 3b62f0f

Browse files
committed
Add tests for sort order and selection
1 parent ec168c7 commit 3b62f0f

File tree

2 files changed

+161
-2
lines changed

2 files changed

+161
-2
lines changed

extensions/ql-vscode/src/query-history.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const FAILED_QUERY_HISTORY_ITEM_ICON = 'media/red-x.svg';
7070
*/
7171
const LOCAL_SUCCESS_QUERY_HISTORY_ITEM_ICON = 'media/drive.svg';
7272

73-
enum SortOrder {
73+
export enum SortOrder {
7474
NameAsc = 'NameAsc',
7575
NameDesc = 'NameDesc',
7676
DateAsc = 'DateAsc',
@@ -820,6 +820,9 @@ the file in the file explorer and dragging it into the workspace.`
820820

821821
/**
822822
* If no items are selected, attempt to grab the selection from the treeview.
823+
* However, often the treeview itself does not have any selection. In this case,
824+
* grab the selection from the `treeDataProvider` current item.
825+
*
823826
* We need to use this method because when clicking on commands from the view title
824827
* bar, the selections are not passed in.
825828
*
@@ -845,6 +848,13 @@ the file in the file explorer and dragging it into the workspace.`
845848
};
846849
}
847850
}
851+
852+
// ensure we do not return undefined
853+
if (singleItem && !multiSelect?.[0]) {
854+
multiSelect = [singleItem];
855+
} else if (!singleItem && multiSelect?.[0]) {
856+
singleItem = multiSelect[0];
857+
}
848858
return {
849859
finalSingleItem: singleItem,
850860
finalMultiSelect: multiSelect

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

Lines changed: 150 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as vscode from 'vscode';
55
import * as sinon from 'sinon';
66
import * as chaiAsPromised from 'chai-as-promised';
77
import { logger } from '../../logging';
8-
import { QueryHistoryManager, HistoryTreeDataProvider } from '../../query-history';
8+
import { QueryHistoryManager, HistoryTreeDataProvider, SortOrder } from '../../query-history';
99
import { QueryEvaluationInfo, QueryWithResults } from '../../run-queries';
1010
import { QueryHistoryConfigListener } from '../../config';
1111
import * as messages from '../../pure/messages';
@@ -356,6 +356,155 @@ describe('query-history', () => {
356356
});
357357
});
358358

359+
describe('determineSelection', () => {
360+
const singleItem = 'a';
361+
const multipleItems = ['b', 'c', 'd'];
362+
363+
it('should get the selection from parameters', async () => {
364+
queryHistoryManager = await createMockQueryHistory(allHistory);
365+
const selection = (queryHistoryManager as any).determineSelection(singleItem, multipleItems);
366+
expect(selection).to.deep.eq({
367+
finalSingleItem: singleItem,
368+
finalMultiSelect: multipleItems
369+
});
370+
});
371+
372+
it('should get the selection when single selection is empty', async () => {
373+
queryHistoryManager = await createMockQueryHistory(allHistory);
374+
const selection = (queryHistoryManager as any).determineSelection(undefined, multipleItems);
375+
expect(selection).to.deep.eq({
376+
finalSingleItem: multipleItems[0],
377+
finalMultiSelect: multipleItems
378+
});
379+
});
380+
381+
it('should get the selection when multi-selection is empty', async () => {
382+
queryHistoryManager = await createMockQueryHistory(allHistory);
383+
const selection = (queryHistoryManager as any).determineSelection(singleItem, undefined);
384+
expect(selection).to.deep.eq({
385+
finalSingleItem: singleItem,
386+
finalMultiSelect: [singleItem]
387+
});
388+
});
389+
390+
it('should get the selection from the treeView when both selections are empty', async () => {
391+
queryHistoryManager = await createMockQueryHistory(allHistory);
392+
await (queryHistoryManager as any).treeView.reveal(allHistory[1], { select: true });
393+
const selection = (queryHistoryManager as any).determineSelection(undefined, undefined);
394+
expect(selection).to.deep.eq({
395+
finalSingleItem: allHistory[1],
396+
finalMultiSelect: [allHistory[1]]
397+
});
398+
});
399+
400+
it('should get the selection from the treeDataProvider when both selections and the treeView are empty', async () => {
401+
queryHistoryManager = await createMockQueryHistory(allHistory);
402+
(queryHistoryManager as any).treeDataProvider.current = allHistory[1];
403+
const selection = (queryHistoryManager as any).determineSelection(undefined, undefined);
404+
expect(selection).to.deep.eq({
405+
finalSingleItem: allHistory[1],
406+
finalMultiSelect: [allHistory[1]]
407+
});
408+
});
409+
});
410+
411+
describe('getChildren', () => {
412+
const history = [
413+
item('a', 10, 20),
414+
item('b', 5, 30),
415+
item('c', 1, 25),
416+
];
417+
let treeDataProvider: HistoryTreeDataProvider;
418+
419+
beforeEach(async () => {
420+
queryHistoryManager = await createMockQueryHistory(allHistory);
421+
(queryHistoryManager.treeDataProvider as any).history = [...history];
422+
treeDataProvider = queryHistoryManager.treeDataProvider;
423+
});
424+
425+
it('should get children for name ascending', async () => {
426+
const expected = [...history];
427+
treeDataProvider.sortOrder = SortOrder.NameAsc;
428+
429+
const children = await treeDataProvider.getChildren();
430+
expect(children).to.deep.eq(expected);
431+
});
432+
433+
it('should get children for name descending', async () => {
434+
const expected = [...history].reverse();
435+
treeDataProvider.sortOrder = SortOrder.NameDesc;
436+
437+
const children = await treeDataProvider.getChildren();
438+
expect(children).to.deep.eq(expected);
439+
});
440+
441+
it('should get children for date ascending', async () => {
442+
const expected = [history[2], history[1], history[0]];
443+
treeDataProvider.sortOrder = SortOrder.DateAsc;
444+
445+
const children = await treeDataProvider.getChildren();
446+
expect(children).to.deep.eq(expected);
447+
});
448+
449+
it('should get children for date descending', async () => {
450+
const expected = [history[0], history[1], history[2]];
451+
treeDataProvider.sortOrder = SortOrder.DateDesc;
452+
453+
const children = await treeDataProvider.getChildren();
454+
expect(children).to.deep.eq(expected);
455+
});
456+
457+
it('should get children for result count ascending', async () => {
458+
const expected = [history[0], history[2], history[1]];
459+
treeDataProvider.sortOrder = SortOrder.CountAsc;
460+
461+
const children = await treeDataProvider.getChildren();
462+
expect(children).to.deep.eq(expected);
463+
});
464+
465+
it('should get children for result count descending', async () => {
466+
const expected = [history[1], history[2], history[0]];
467+
treeDataProvider.sortOrder = SortOrder.CountDesc;
468+
469+
const children = await treeDataProvider.getChildren();
470+
expect(children).to.deep.eq(expected);
471+
});
472+
473+
it('should get children for result count ascending when there are no results', async () => {
474+
// fall back to name
475+
const thisHistory = [item('a', 10), item('b', 50), item('c', 1)];
476+
(queryHistoryManager!.treeDataProvider as any).history = [...thisHistory];
477+
const expected = [...thisHistory];
478+
treeDataProvider.sortOrder = SortOrder.CountAsc;
479+
480+
const children = await treeDataProvider.getChildren();
481+
expect(children).to.deep.eq(expected);
482+
});
483+
484+
it('should get children for result count descending when there are no results', async () => {
485+
// fall back to name
486+
const thisHistory = [item('a', 10), item('b', 50), item('c', 1)];
487+
(queryHistoryManager!.treeDataProvider as any).history = [...thisHistory];
488+
const expected = [...thisHistory].reverse();
489+
treeDataProvider.sortOrder = SortOrder.CountDesc;
490+
491+
const children = await treeDataProvider.getChildren();
492+
expect(children).to.deep.eq(expected);
493+
});
494+
495+
function item(label: string, start: number, resultCount?: number) {
496+
return {
497+
label,
498+
initialInfo: {
499+
start: new Date(start),
500+
},
501+
completedQuery: {
502+
resultCount,
503+
}
504+
};
505+
}
506+
});
507+
359508
function createMockFullQueryInfo(dbName = 'a', queryWitbResults?: QueryWithResults, isFail = false): FullQueryInfo {
360509
const fqi = new FullQueryInfo(
361510
{

0 commit comments

Comments
 (0)