Skip to content

Commit

Permalink
Added CommandHandler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Schmid committed Dec 27, 2017
1 parent a47acc5 commit b4f10b1
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions test/lib/CommandHandler.test.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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");
});
});
});

0 comments on commit b4f10b1

Please sign in to comment.