From b4f10b17cead3de5278f9b29d90be18751b8d48a Mon Sep 17 00:00:00 2001 From: Frank Schmid Date: Wed, 27 Dec 2017 13:47:36 +0100 Subject: [PATCH] Added CommandHandler tests --- test/lib/CommandHandler.test.ts | 72 +++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 test/lib/CommandHandler.test.ts diff --git a/test/lib/CommandHandler.test.ts b/test/lib/CommandHandler.test.ts new file mode 100644 index 0000000..dccfd5a --- /dev/null +++ b/test/lib/CommandHandler.test.ts @@ -0,0 +1,72 @@ +import * as chai from "chai"; +import * as sinon from "sinon"; +import * as sinon_chai from "sinon-chai"; +import { commands, ExtensionContext, Memento, window } from "vscode"; +import { ICommand } from "../../src/lib/CommandBase"; +import { CommandHandler } from "../../src/lib/CommandHandler"; +import { NodeKind, ServerlessNode } from "../../src/lib/ServerlessNode"; + +// tslint:disable:max-classes-per-file +// tslint:disable:no-unused-expression + +chai.use(sinon_chai); +const expect = chai.expect; + +class TestContext implements ExtensionContext { + public subscriptions: Array<{ dispose(): any; }> = []; + public workspaceState: Memento; + public globalState: Memento; + public extensionPath: string = "myExtensionPath"; + public asAbsolutePath: sinon.SinonStub = sinon.stub(); + public storagePath: string = "myStoragePath"; +} + +class TestCommand implements ICommand { + constructor(public context: ExtensionContext) { + this.invoke = sinon.stub(); + } + + public invoke(node: ServerlessNode): Thenable { + throw new Error("Method not implemented."); + } +} + +describe("CommandHandler", () => { + let sandbox: sinon.SinonSandbox; + let windowShowInputBoxStub: sinon.SinonStub; + + before(() => { + sandbox = sinon.createSandbox(); + }); + + beforeEach(() => { + windowShowInputBoxStub = sandbox.stub(window, "showInputBox"); + }); + + afterEach(() => { + sandbox.restore(); + }); + + describe("registerCommand", () => { + let testContext: ExtensionContext; + let commandsRegisterCommandSpy: sinon.SinonSpy; + + beforeEach(() => { + testContext = new TestContext(); + commandsRegisterCommandSpy = sandbox.spy(commands, "registerCommand"); + }); + + it("should register command and keep subscription", async () => { + CommandHandler.registerCommand( + TestCommand, + "serverless.test", + testContext, + ); + + expect(testContext.subscriptions).to.have.length(1); + expect(commandsRegisterCommandSpy).to.have.been.calledOnce; + const registeredCommands = await commands.getCommands(); + expect(registeredCommands).to.include("serverless.test"); + }); + }); +});