Skip to content

Commit

Permalink
test: add ut
Browse files Browse the repository at this point in the history
  • Loading branch information
yiqing-zhao committed Jun 21, 2024
1 parent 338158d commit d9840bd
Showing 1 changed file with 150 additions and 51 deletions.
201 changes: 150 additions & 51 deletions packages/vscode-extension/test/handlers/accountHandlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,162 @@ import * as vscode from "vscode";
import * as sinon from "sinon";
import * as chai from "chai";
import M365TokenInstance from "../../src/commonlib/m365Login";
import { ok } from "@microsoft/teamsfx-api";
import { err, ok } from "@microsoft/teamsfx-api";
import { AzureAccountManager } from "../../src/commonlib/azureLogin";
import * as vsc_ui from "../../src/qm/vsc_ui";
import { cmpAccountsHandler } from "../../src/handlers/accountHandlers";
import { cmpAccountsHandler, createAccountHandler } from "../../src/handlers/accountHandlers";
import { ExtTelemetry } from "../../src/telemetry/extTelemetry";
import * as localizeUtils from "../../src/utils/localizeUtils";

describe("AccountHandlers", () => {
const sandbox = sinon.createSandbox();
describe("createAccountHandler", () => {
const sandbox = sinon.createSandbox();

afterEach(() => {
sandbox.restore();
afterEach(() => {
sandbox.restore();
});

beforeEach(() => {
sandbox.stub(localizeUtils, "localize").returns("test");
sandbox.stub(vsc_ui, "VS_CODE_UI").value(new vsc_ui.VsCodeUI(<vscode.ExtensionContext>{}));
});

it("create M365 account", async () => {
const selectOptionStub = sandbox
.stub(vsc_ui.VS_CODE_UI, "selectOption")
.resolves(ok({ result: "createAccountM365" } as any));
const openUrlStub = sandbox.stub(vsc_ui.VS_CODE_UI, "openUrl");
const sendTelemetryEventStub = sandbox.stub(ExtTelemetry, "sendTelemetryEvent");

await createAccountHandler([]);

chai.expect(selectOptionStub.calledOnce).to.be.true;
chai.expect(
openUrlStub.calledOnceWith("https://developer.microsoft.com/microsoft-365/dev-program")
).to.be.true;
chai.expect(sendTelemetryEventStub.args[1][1]).to.deep.equal({
"account-type": "m365",
"trigger-from": "CommandPalette",
});
});

it("create Azure account", async () => {
const selectOptionStub = sandbox
.stub(vsc_ui.VS_CODE_UI, "selectOption")
.resolves(ok({ result: "createAccountAzure" } as any));
const openUrlStub = sandbox.stub(vsc_ui.VS_CODE_UI, "openUrl");
const sendTelemetryEventStub = sandbox.stub(ExtTelemetry, "sendTelemetryEvent");

await createAccountHandler([]);

chai.expect(selectOptionStub.calledOnce).to.be.true;
chai.expect(openUrlStub.calledOnceWith("https://azure.microsoft.com/en-us/free/")).to.be.true;
chai.expect(sendTelemetryEventStub.args[1][1]).to.deep.equal({
"account-type": "azure",
"trigger-from": "CommandPalette",
});
});

it("create account error", async () => {
const selectOptionStub = sandbox
.stub(vsc_ui.VS_CODE_UI, "selectOption")
.resolves(err("error") as any);
const sendTelemetryErrorEventStub = sandbox.stub(ExtTelemetry, "sendTelemetryErrorEvent");

await createAccountHandler([]);

chai.expect(selectOptionStub.calledOnce).to.be.true;
chai.expect(sendTelemetryErrorEventStub.calledOnce).to.be.true;
});
});

it("cmpAccountsHandler", async () => {
const showMessageStub = sandbox
.stub(vscode.window, "showInformationMessage")
.resolves(undefined);
const M365SignOutStub = sandbox.stub(M365TokenInstance, "signout");
sandbox
.stub(M365TokenInstance, "getStatus")
.resolves(ok({ status: "SignedIn", accountInfo: { upn: "test.email.com" } }));
sandbox
.stub(AzureAccountManager.prototype, "getStatus")
.resolves({ status: "SignedIn", accountInfo: { upn: "test.email.com" } });
let changeSelectionCallback: (e: readonly vscode.QuickPickItem[]) => any = () => {};
const stubQuickPick = {
items: [],
onDidChangeSelection: (
_changeSelectionCallback: (e: readonly vscode.QuickPickItem[]) => any
) => {
changeSelectionCallback = _changeSelectionCallback;
return {
dispose: () => {},
};
},
onDidHide: () => {
return {
dispose: () => {},
};
},
show: () => {},
hide: () => {},
onDidAccept: () => {},
};
const hideStub = sandbox.stub(stubQuickPick, "hide");
sandbox.stub(vscode.window, "createQuickPick").returns(stubQuickPick as any);
sandbox.stub(vsc_ui, "VS_CODE_UI").value(new vsc_ui.VsCodeUI(<vscode.ExtensionContext>{}));
sandbox.stub(vsc_ui.VS_CODE_UI, "selectOption").resolves(ok({ result: "unknown" } as any));

await cmpAccountsHandler([]);
changeSelectionCallback([stubQuickPick.items[1]]);

for (const i of stubQuickPick.items) {
await (i as any).function();
}

chai.assert.isTrue(showMessageStub.calledTwice);
chai.assert.isTrue(M365SignOutStub.calledOnce);
chai.assert.isTrue(hideStub.calledOnce);
describe("cmpAccountsHandler", () => {
const sandbox = sinon.createSandbox();
let changeSelectionCallback: (e: readonly vscode.QuickPickItem[]) => any;
let stubQuickPick: any;

afterEach(() => {
sandbox.restore();
});

beforeEach(() => {
changeSelectionCallback = () => {};
stubQuickPick = {
items: [],
onDidChangeSelection: (
_changeSelectionCallback: (e: readonly vscode.QuickPickItem[]) => any
) => {
changeSelectionCallback = _changeSelectionCallback;
return {
dispose: () => {},
};
},
onDidHide: () => {
return {
dispose: () => {},
};
},
show: () => {},
hide: () => {},
onDidAccept: () => {},
};
sandbox.stub(ExtTelemetry, "sendTelemetryEvent");
sandbox.stub(vscode.window, "createQuickPick").returns(stubQuickPick as any);
sandbox.stub(vsc_ui, "VS_CODE_UI").value(new vsc_ui.VsCodeUI(<vscode.ExtensionContext>{}));
sandbox.stub(vsc_ui.VS_CODE_UI, "selectOption").resolves(ok({ result: "unknown" } as any));
});

it("Sign out happy path", async () => {
const showMessageStub = sandbox
.stub(vscode.window, "showInformationMessage")
.resolves(undefined);
const M365SignOutStub = sandbox.stub(M365TokenInstance, "signout");
sandbox
.stub(M365TokenInstance, "getStatus")
.resolves(ok({ status: "SignedIn", accountInfo: { upn: "test.email.com" } }));
sandbox
.stub(AzureAccountManager.prototype, "getStatus")
.resolves({ status: "SignedIn", accountInfo: { upn: "test.email.com" } });
const hideStub = sandbox.stub(stubQuickPick, "hide");

await cmpAccountsHandler([]);
changeSelectionCallback([stubQuickPick.items[1]]);

for (const i of stubQuickPick.items) {
await (i as any).function();
}

chai.assert.isTrue(showMessageStub.calledTwice);
chai.assert.isTrue(M365SignOutStub.calledOnce);
chai.assert.isTrue(hideStub.calledOnce);
});

it("Sign in happy path", async () => {
const showMessageStub = sandbox
.stub(vscode.window, "showInformationMessage")
.resolves(undefined);
const executeCommandStub = sandbox.stub(vscode.commands, "executeCommand");
sandbox
.stub(M365TokenInstance, "getStatus")
.resolves(ok({ status: "SignedOut", accountInfo: { upn: "test.email.com" } }));
sandbox
.stub(AzureAccountManager.prototype, "getStatus")
.resolves({ status: "SignedOut", accountInfo: { upn: "test.email.com" } });
const hideStub = sandbox.stub(stubQuickPick, "hide");

await cmpAccountsHandler([]);
changeSelectionCallback([stubQuickPick.items[1]]);

for (const i of stubQuickPick.items) {
await (i as any).function();
}

chai.assert.isTrue(showMessageStub.notCalled);
chai.assert.isTrue(executeCommandStub.calledThrice);
chai.expect(executeCommandStub.args[0][0]).to.be.equal("fx-extension.signinAzure");
chai.expect(executeCommandStub.args[1][0]).to.be.equal("fx-extension.signinM365");
chai.expect(executeCommandStub.args[2][0]).to.be.equal("fx-extension.signinAzure");
chai.assert.isTrue(hideStub.calledOnce);
});
});
});

0 comments on commit d9840bd

Please sign in to comment.