Skip to content

Commit

Permalink
Add unit tests for query-history
Browse files Browse the repository at this point in the history
  • Loading branch information
aeisenberg committed Dec 16, 2020
1 parent 4462cf0 commit 1ece1bb
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
8 changes: 4 additions & 4 deletions extensions/ql-vscode/src/query-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const FAILED_QUERY_HISTORY_ITEM_ICON = 'media/red-x.svg';
/**
* Tree data provider for the query history view.
*/
class HistoryTreeDataProvider extends DisposableObject {
export class HistoryTreeDataProvider extends DisposableObject {
private _onDidChangeTreeData = super.push(new vscode.EventEmitter<CompletedQuery | undefined>());

readonly onDidChangeTreeData: vscode.Event<CompletedQuery | undefined> = this
Expand Down Expand Up @@ -144,8 +144,8 @@ class HistoryTreeDataProvider extends DisposableObject {
return this.history;
}

refresh(CompletedQuery?: CompletedQuery) {
this._onDidChangeTreeData.fire(CompletedQuery);
refresh(completedQuery?: CompletedQuery) {
this._onDidChangeTreeData.fire(completedQuery);
}

find(queryId: number): CompletedQuery | undefined {
Expand Down Expand Up @@ -356,7 +356,7 @@ export class QueryHistoryManager extends DisposableObject {
if (response !== undefined) {
// Interpret empty string response as 'go back to using default'
singleItem.options.label = response === '' ? undefined : response;
this.treeDataProvider.refresh();
this.treeDataProvider.refresh(singleItem);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import * as vscode from 'vscode';
import * as sinon from 'sinon';
import * as chaiAsPromised from 'chai-as-promised';
import { logger } from '../../logging';
import { QueryHistoryManager } from '../../query-history';
import { QueryHistoryManager, HistoryTreeDataProvider } from '../../query-history';
import { CompletedQuery } from '../../query-results';
import { QueryInfo } from '../../run-queries';

chai.use(chaiAsPromised);
const expect = chai.expect;
Expand Down Expand Up @@ -43,7 +45,6 @@ describe('query-history', () => {
});

describe('tryOpenExternalFile', () => {

it('should open an external file', async () => {
await tryOpenExternalFile('xxx');
expect(showTextDocumentSpy).to.have.been.calledOnceWith(
Expand Down Expand Up @@ -204,6 +205,67 @@ describe('query-history', () => {
expect(queryHistory.compareWithItem).to.be.eq('a');
});
});

describe('HistoryTreeDataProvider', () => {
let historyTreeDataProvider: HistoryTreeDataProvider;
beforeEach(() => {
historyTreeDataProvider = new HistoryTreeDataProvider(vscode.Uri.file('/a/b/c').fsPath);
});

it('should get a tree item with raw results', async () => {
const mockQuery = {
query: {
hasInterpretedResults: () => Promise.resolve(false)
} as QueryInfo,
didRunSuccessfully: true,
toString: () => 'mock label'
} as CompletedQuery;
const treeItem = await historyTreeDataProvider.getTreeItem(mockQuery);
expect(treeItem.command).to.deep.eq({
title: 'Query History Item',
command: 'codeQLQueryHistory.itemClicked',
arguments: [mockQuery],
});
expect(treeItem.label).to.eq('mock label');
expect(treeItem.contextValue).to.eq('rawResultsItem');
expect(treeItem.iconPath).to.be.undefined;
});

it('should get a tree item with interpreted results', async () => {
const mockQuery = {
query: {
// as above, except for this line
hasInterpretedResults: () => Promise.resolve(true)
} as QueryInfo,
didRunSuccessfully: true,
toString: () => 'mock label'
} as CompletedQuery;
const treeItem = await historyTreeDataProvider.getTreeItem(mockQuery);
expect(treeItem.contextValue).to.eq('interpretedResultsItem');
});

it('should get a tree item that did not complete successfully', async () => {
const mockQuery = {
query: {
hasInterpretedResults: () => Promise.resolve(true)
} as QueryInfo,
// as above, except for this line
didRunSuccessfully: false,
toString: () => 'mock label'
} as CompletedQuery;
const treeItem = await historyTreeDataProvider.getTreeItem(mockQuery);
expect(treeItem.iconPath).to.eq(vscode.Uri.file('/a/b/c/media/red-x.svg').fsPath);
});

it('should get children', () => {
const mockQuery = {
databaseName: 'abc'
} as CompletedQuery;
historyTreeDataProvider.allHistory.push(mockQuery);
expect(historyTreeDataProvider.getChildren()).to.deep.eq([mockQuery]);
expect(historyTreeDataProvider.getChildren(mockQuery)).to.deep.eq([]);
});
});
});

function createMockQueryHistory(allHistory: {}[]) {
Expand Down

0 comments on commit 1ece1bb

Please sign in to comment.