Skip to content

Commit

Permalink
tests: AuthHandler.updateTreeProvidersWithProfile
Browse files Browse the repository at this point in the history
Signed-off-by: Trae Yelovich <[email protected]>
  • Loading branch information
traeok committed Dec 24, 2024
1 parent d986e47 commit 9264fde
Showing 1 changed file with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
*/

import { Mutex } from "async-mutex";
import { AuthHandler, Gui } from "../../../src";
import { AuthHandler, Gui, imperative } from "../../../src";
import { FileManagement } from "../../../src/utils/FileManagement";
import { ImperativeError } from "@zowe/imperative";
import { commands } from "vscode";

const TEST_PROFILE_NAME = "lpar.zosmf";

Expand Down Expand Up @@ -71,6 +72,59 @@ describe("AuthHandler.lockProfile", () => {
});
});

describe("AuthHandler.updateTreeProvidersWithProfile", () => {
function getBlockMocks(nodeInChildren: boolean = false) {
const setProfileToChoice = jest.fn();
const dummyNode = nodeInChildren
? {
label: "lpar.zosmf",
setProfileToChoice,
}
: undefined;
const children = [dummyNode].filter(Boolean);
const getChildren = jest.fn().mockReturnValue(children);
const treeProviders = {
ds: {
getChildren,
},
uss: {
getChildren,
},
job: {
getChildren,
},
};
return {
children,
dummyNode,
executeCommand: jest.spyOn(commands, "executeCommand").mockResolvedValueOnce(treeProviders),
getChildren,
setProfileToChoice,
treeProviders,
};
}
it("calls node.setProfileToChoice on matching profile nodes in each provider", async () => {
const blockMocks = getBlockMocks(true);
const profile: imperative.IProfileLoaded = { name: "lpar.zosmf", type: "zosmf", message: "", failNotFound: true };
await (AuthHandler as any).updateTreeProvidersWithProfile(profile);
// getChildren called for each tree provider
expect(blockMocks.getChildren).toHaveBeenCalledTimes(3);
// setProfileToChoice called once for each matching profile node per provider (3 providers, 3 calls)
expect(blockMocks.setProfileToChoice).toHaveBeenCalledTimes(3);
expect(blockMocks.setProfileToChoice).toHaveBeenCalledWith(profile);
});

it("does nothing if the profile node does not exist for a provider", async () => {
const blockMocks = getBlockMocks();
const profile: imperative.IProfileLoaded = { name: "lpar.zosmf", type: "zosmf", message: "", failNotFound: true };
await (AuthHandler as any).updateTreeProvidersWithProfile(profile);
// getChildren called for each tree provider
expect(blockMocks.getChildren).toHaveBeenCalledTimes(3);
// if no matching nodes were found, setProfileToChoice should never be called
expect(blockMocks.setProfileToChoice).not.toHaveBeenCalled();
});
});

describe("AuthHandler.promptForAuthentication", () => {
it("handles a token-based authentication error - login successful, profile is string", async () => {
const tokenNotValidMsg = "Token is not valid or expired.";
Expand Down

0 comments on commit 9264fde

Please sign in to comment.