|
| 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