|
| 1 | +/* |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + */ |
| 5 | + |
| 6 | +/* eslint-disable @typescript-eslint/no-non-null-assertion */ |
| 7 | +import { expect } from "chai"; |
| 8 | +import * as sinon from "sinon"; |
| 9 | +import * as vscode from "vscode"; |
| 10 | +import { ActionsHubTreeDataProvider } from "../../../../power-pages/actions-hub/ActionsHubTreeDataProvider"; |
| 11 | +import { oneDSLoggerWrapper } from "../../../../../common/OneDSLoggerTelemetry/oneDSLoggerWrapper"; |
| 12 | +import { Constants } from "../../../../power-pages/actions-hub/Constants"; |
| 13 | +import { EnvironmentGroupTreeItem } from "../../../../power-pages/actions-hub/tree-items/EnvironmentGroupTreeItem"; |
| 14 | +import { OtherSitesGroupTreeItem } from "../../../../power-pages/actions-hub/tree-items/OtherSitesGroupTreeItem"; |
| 15 | +import { ActionsHubTreeItem } from "../../../../power-pages/actions-hub/tree-items/ActionsHubTreeItem"; |
| 16 | +import { PacTerminal } from "../../../../lib/PacTerminal"; |
| 17 | +import { authManager } from "../../../../pac/PacAuthManager"; |
| 18 | + |
| 19 | +describe("ActionsHubTreeDataProvider", () => { |
| 20 | + let context: vscode.ExtensionContext; |
| 21 | + let pacTerminal: PacTerminal; |
| 22 | + let actionsHubTreeDataProvider: ActionsHubTreeDataProvider; |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + context = {} as vscode.ExtensionContext; |
| 26 | + pacTerminal = {} as PacTerminal; |
| 27 | + actionsHubTreeDataProvider = ActionsHubTreeDataProvider.initialize(context, pacTerminal); |
| 28 | + }); |
| 29 | + |
| 30 | + afterEach(() => { |
| 31 | + sinon.restore(); |
| 32 | + }); |
| 33 | + |
| 34 | + it("should initialize and log initialization event", () => { |
| 35 | + const traceInfoStub = sinon.stub(oneDSLoggerWrapper.getLogger(), "traceInfo"); |
| 36 | + ActionsHubTreeDataProvider.initialize(context, pacTerminal); |
| 37 | + expect(traceInfoStub.calledWith(Constants.EventNames.ACTIONS_HUB_INITIALIZED)).to.be.true; |
| 38 | + }); |
| 39 | + |
| 40 | + it("should return the element in getTreeItem", () => { |
| 41 | + const element = {} as ActionsHubTreeItem; |
| 42 | + const result = actionsHubTreeDataProvider.getTreeItem(element); |
| 43 | + expect(result).to.equal(element); |
| 44 | + }); |
| 45 | + |
| 46 | + it("should return environment and other sites group tree items in getChildren when no element is passed", async () => { |
| 47 | + const authInfoStub = sinon.stub(authManager, "getAuthInfo").returns({ |
| 48 | + organizationFriendlyName: "TestOrg", |
| 49 | + userType: "", |
| 50 | + cloud: "", |
| 51 | + tenantId: "", |
| 52 | + tenantCountry: "", |
| 53 | + user: "", |
| 54 | + entraIdObjectId: "", |
| 55 | + puid: "", |
| 56 | + userCountryRegion: "", |
| 57 | + tokenExpires: "", |
| 58 | + authority: "", |
| 59 | + environmentGeo: "", |
| 60 | + environmentId: "", |
| 61 | + environmentType: "", |
| 62 | + organizationId: "", |
| 63 | + organizationUniqueName: "" |
| 64 | + }); |
| 65 | + const result = await actionsHubTreeDataProvider.getChildren(); |
| 66 | + |
| 67 | + expect(result).to.not.be.null; |
| 68 | + expect(result).to.not.be.undefined; |
| 69 | + expect(result).to.have.lengthOf(2); |
| 70 | + expect(result![0]).to.be.instanceOf(EnvironmentGroupTreeItem); |
| 71 | + expect(result![1]).to.be.instanceOf(OtherSitesGroupTreeItem); |
| 72 | + |
| 73 | + authInfoStub.restore(); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should return environment group tree item with default name when no auth info is available", async () => { |
| 77 | + const authInfoStub = sinon.stub(authManager, "getAuthInfo").returns(null); |
| 78 | + const result = await actionsHubTreeDataProvider.getChildren(); |
| 79 | + |
| 80 | + expect(result).to.not.be.null; |
| 81 | + expect(result).to.not.be.undefined; |
| 82 | + expect(result).to.have.lengthOf(2); |
| 83 | + expect(result![0]).to.be.instanceOf(EnvironmentGroupTreeItem); |
| 84 | + expect((result![0] as EnvironmentGroupTreeItem).environmentInfo.currentEnvironmentName).to.equal(Constants.Strings.NO_ENVIRONMENTS_FOUND); |
| 85 | + expect(result![1]).to.be.instanceOf(OtherSitesGroupTreeItem); |
| 86 | + |
| 87 | + authInfoStub.restore(); |
| 88 | + }); |
| 89 | + |
| 90 | + it("should return null in getChildren when an error occurs", async () => { |
| 91 | + const authInfoStub = sinon.stub(authManager, "getAuthInfo").throws(new Error("Test Error")); |
| 92 | + const traceErrorStub = sinon.stub(oneDSLoggerWrapper.getLogger(), "traceError"); |
| 93 | + |
| 94 | + const result = await actionsHubTreeDataProvider.getChildren(); |
| 95 | + expect(result).to.be.null; |
| 96 | + expect(traceErrorStub.calledWith(Constants.EventNames.ACTIONS_HUB_CURRENT_ENV_FETCH_FAILED)).to.be.true; |
| 97 | + |
| 98 | + authInfoStub.restore(); |
| 99 | + traceErrorStub.restore(); |
| 100 | + }); |
| 101 | + |
| 102 | + it("should return an empty array in getChildren when an element is passed", async () => { |
| 103 | + const element = {} as ActionsHubTreeItem; |
| 104 | + const result = await actionsHubTreeDataProvider.getChildren(element); |
| 105 | + expect(result).to.be.an("array").that.is.empty; |
| 106 | + }); |
| 107 | + |
| 108 | + it("should dispose all disposables", () => { |
| 109 | + const disposable1 = { dispose: sinon.spy() }; |
| 110 | + const disposable2 = { dispose: sinon.spy() }; |
| 111 | + actionsHubTreeDataProvider["_disposables"].push(disposable1 as vscode.Disposable, disposable2 as vscode.Disposable); |
| 112 | + |
| 113 | + actionsHubTreeDataProvider.dispose(); |
| 114 | + expect(disposable1.dispose.calledOnce).to.be.true; |
| 115 | + expect(disposable2.dispose.calledOnce).to.be.true; |
| 116 | + }); |
| 117 | +}); |
0 commit comments