Skip to content

Commit 3c6169f

Browse files
committed
Add unit tests for tryOpenExternalFile
1 parent 4bc17ed commit 3c6169f

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

extensions/ql-vscode/src/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export async function showBinaryChoiceDialog(message: string): Promise<boolean>
118118
const yesItem = { title: 'Yes', isCloseAffordance: false };
119119
const noItem = { title: 'No', isCloseAffordance: true };
120120
const chosenItem = await Window.showInformationMessage(message, { modal: true }, yesItem, noItem);
121-
return chosenItem === yesItem;
121+
return chosenItem?.title === 'Yes';
122122
}
123123

124124
/**
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import * as chai from "chai";
2+
import "mocha";
3+
import * as vscode from "vscode";
4+
import * as sinon from "sinon";
5+
// import * as sinonChai from 'sinon-chai';
6+
import * as chaiAsPromised from "chai-as-promised";
7+
import { logger } from "../../logging";
8+
import { QueryHistoryManager } from "../../query-history";
9+
10+
chai.use(chaiAsPromised);
11+
const expect = chai.expect;
12+
13+
14+
15+
describe('query-history', () => {
16+
17+
describe("tryOpenExternalFile", () => {
18+
let showTextDocumentSpy: sinon.SinonStub;
19+
let showInformationMessageSpy: sinon.SinonStub;
20+
let executeCommandSpy: sinon.SinonStub;
21+
let logSpy: sinon.SinonStub;
22+
23+
let tryOpenExternalFile: Function;
24+
25+
beforeEach(() => {
26+
showTextDocumentSpy = sinon.stub(vscode.window, "showTextDocument");
27+
showInformationMessageSpy = sinon.stub(
28+
vscode.window,
29+
"showInformationMessage"
30+
);
31+
executeCommandSpy = sinon.stub(vscode.commands, "executeCommand");
32+
logSpy = sinon.stub(logger, "log");
33+
tryOpenExternalFile = (QueryHistoryManager.prototype as any).tryOpenExternalFile;
34+
logSpy;
35+
executeCommandSpy;
36+
});
37+
38+
afterEach(() => {
39+
(vscode.window.showTextDocument as sinon.SinonStub).restore();
40+
(vscode.commands.executeCommand as sinon.SinonStub).restore();
41+
(logger.log as sinon.SinonStub).restore();
42+
(vscode.window.showInformationMessage as sinon.SinonStub).restore();
43+
});
44+
45+
it("should open an external file", async () => {
46+
await tryOpenExternalFile('xxx');
47+
expect(showTextDocumentSpy).to.have.been.calledOnceWith(
48+
vscode.Uri.file('xxx')
49+
);
50+
expect(executeCommandSpy).not.to.have.been.called;
51+
});
52+
53+
[
54+
"too large to open",
55+
"Files above 50MB cannot be synchronized with extensions",
56+
].forEach(msg => {
57+
it(`should fail to open a file because "${msg}" and open externally`, async () => {
58+
showTextDocumentSpy.throws(new Error(msg));
59+
showInformationMessageSpy.returns({ title: "Yes" });
60+
61+
await tryOpenExternalFile("xxx");
62+
const uri = vscode.Uri.file("xxx");
63+
expect(showTextDocumentSpy).to.have.been.calledOnceWith(
64+
uri
65+
);
66+
expect(executeCommandSpy).to.have.been.calledOnceWith(
67+
"revealFileInOS",
68+
uri
69+
);
70+
});
71+
72+
it(`should fail to open a file because "${msg}" and NOT open externally`, async () => {
73+
showTextDocumentSpy.throws(new Error(msg));
74+
showInformationMessageSpy.returns({ title: "No" });
75+
76+
await tryOpenExternalFile("xxx");
77+
const uri = vscode.Uri.file("xxx");
78+
expect(showTextDocumentSpy).to.have.been.calledOnceWith(uri);
79+
expect(showInformationMessageSpy).to.have.been.called;
80+
expect(executeCommandSpy).not.to.have.been.called;
81+
});
82+
});
83+
});
84+
});

0 commit comments

Comments
 (0)