From 3350f16984fefc6d6be2c36fb42b893b785eed6a Mon Sep 17 00:00:00 2001 From: "Andrew W. Harn" Date: Thu, 12 Dec 2024 16:28:20 -0500 Subject: [PATCH] Add more tests Signed-off-by: Andrew W. Harn --- .../__unit__/trees/job/JobTree.unit.test.ts | 9 +- .../trees/job/ZoweJobNode.unit.test.ts | 152 +++++++++++++++++- 2 files changed, 152 insertions(+), 9 deletions(-) diff --git a/packages/zowe-explorer/__tests__/__unit__/trees/job/JobTree.unit.test.ts b/packages/zowe-explorer/__tests__/__unit__/trees/job/JobTree.unit.test.ts index 018e5ca1ad..caf65e80bd 100644 --- a/packages/zowe-explorer/__tests__/__unit__/trees/job/JobTree.unit.test.ts +++ b/packages/zowe-explorer/__tests__/__unit__/trees/job/JobTree.unit.test.ts @@ -12,14 +12,7 @@ import * as vscode from "vscode"; import * as zosjobs from "@zowe/zos-jobs-for-zowe-sdk"; import { Gui, imperative, IZoweJobTreeNode, ProfilesCache, Validation, Poller, ZosEncoding } from "@zowe/zowe-explorer-api"; -import { - createIJobFile, - createIJobObject, - createJobFavoritesNode, - createJobNode, - createJobSessionNode, - MockJobDetail, -} from "../../../__mocks__/mockCreators/jobs"; +import { createIJobFile, createIJobObject, createJobFavoritesNode, createJobSessionNode, MockJobDetail } from "../../../__mocks__/mockCreators/jobs"; import { createIProfile, createISession, diff --git a/packages/zowe-explorer/__tests__/__unit__/trees/job/ZoweJobNode.unit.test.ts b/packages/zowe-explorer/__tests__/__unit__/trees/job/ZoweJobNode.unit.test.ts index 96fae712cb..1d70a24880 100644 --- a/packages/zowe-explorer/__tests__/__unit__/trees/job/ZoweJobNode.unit.test.ts +++ b/packages/zowe-explorer/__tests__/__unit__/trees/job/ZoweJobNode.unit.test.ts @@ -13,7 +13,7 @@ jest.mock("@zowe/zos-jobs-for-zowe-sdk"); import * as vscode from "vscode"; import * as zosjobs from "@zowe/zos-jobs-for-zowe-sdk"; import * as zosmf from "@zowe/zosmf-for-zowe-sdk"; -import { createIJobFile, createIJobObject, createJobSessionNode } from "../../../__mocks__/mockCreators/jobs"; +import { createIJobFile, createIJobObject, createJobNode, createJobSessionNode } from "../../../__mocks__/mockCreators/jobs"; import { imperative, IZoweJobTreeNode, ProfilesCache, Gui, Sorting } from "@zowe/zowe-explorer-api"; import { TreeViewUtils } from "../../../../src/utils/TreeViewUtils"; import { @@ -34,6 +34,8 @@ import { ZoweJobNode, ZoweSpoolNode } from "../../../../src/trees/job/ZoweJobNod import { SharedContext } from "../../../../src/trees/shared/SharedContext"; import { SharedTreeProviders } from "../../../../src/trees/shared/SharedTreeProviders"; import { JobInit } from "../../../../src/trees/job/JobInit"; +import { ZoweLogger } from "../../../../src/tools/ZoweLogger"; +import * as path from "path"; async function createGlobalMocks() { const globalMocks = { @@ -813,6 +815,154 @@ describe("ZoweJobNode unit tests - Function saveSearch", () => { }); }); +describe("ZoweJobNode unit tests - Function getEncodingInMap", () => { + it("should get the encoding in the map", async () => { + const globalMocks = await createGlobalMocks(); + JobFSProvider.instance.encodingMap["fakePath"] = { kind: "text" }; + const encoding = globalMocks.testJobNode.getEncodingInMap("fakePath"); + expect(encoding).toEqual({ kind: "text" }); + }); +}); + +describe("ZoweJobNode unit tests - Function updateEncodingInMap", () => { + it("should update the encoding in the map", async () => { + const globalMocks = await createGlobalMocks(); + globalMocks.testJobNode.updateEncodingInMap("fakePath", { kind: "binary" }); + expect(JobFSProvider.instance.encodingMap["fakePath"]).toEqual({ kind: "binary" }); + }); +}); + +describe("ZoweJobNode unit tests - Function getEncoding", () => { + it("should update the encoding of the node", () => { + const testNode = createJobNode(createJobSessionNode(createISession(), createIProfile()), createIProfile()); + const getEncodingForFileSpy = jest + .spyOn(JobFSProvider.instance, "getEncodingForFile") + .mockReturnValue({ kind: "other", codepage: "IBM-1147" }); + const encoding = testNode.getEncoding(); + expect(getEncodingForFileSpy).toHaveBeenCalledTimes(1); + expect(getEncodingForFileSpy).toHaveBeenCalledWith(testNode.resourceUri); + expect(encoding).toEqual({ kind: "other", codepage: "IBM-1147" }); + }); +}); + +describe("ZoweJobNode unit tests - Function setEncoding", () => { + const zoweLoggerTraceSpy = jest.spyOn(ZoweLogger, "trace").mockImplementation(); + const setEncodingForFileSpy = jest.spyOn(JobFSProvider.instance, "setEncodingForFile").mockImplementation(); + const existsSpy = jest.spyOn(JobFSProvider.instance, "exists"); + + beforeEach(() => { + jest.clearAllMocks(); + JobFSProvider.instance.encodingMap = {}; + }); + + afterAll(() => { + zoweLoggerTraceSpy.mockRestore(); + setEncodingForFileSpy.mockRestore(); + existsSpy.mockRestore(); + }); + + it("should error if set encoding is called on a non-spool node", () => { + const testNode = createJobNode(createJobSessionNode(createISession(), createIProfile()), createIProfile()); + const updateEncodingSpy = jest.spyOn(testNode, "updateEncodingInMap"); + + testNode.dirty = false; + + let e: Error; + try { + testNode.setEncoding({ kind: "text" }); + } catch (err) { + e = err; + } + + expect(e).toBeDefined(); + expect(e.message).toEqual("Cannot set encoding for node with context job"); + expect(existsSpy).not.toHaveBeenCalled(); + expect(setEncodingForFileSpy).not.toHaveBeenCalled(); + expect(updateEncodingSpy).not.toHaveBeenCalled(); + expect(testNode.dirty).toEqual(false); + }); + + it("should error if the resource does not exist", () => { + const testNode = new ZoweSpoolNode({ label: "SPOOL", collapsibleState: vscode.TreeItemCollapsibleState.None, spool: createIJobFile() }); + const updateEncodingSpy = jest.spyOn(testNode, "updateEncodingInMap"); + testNode.dirty = false; + + existsSpy.mockReturnValueOnce(false); + + let e: Error; + try { + testNode.setEncoding({ kind: "text" }); + } catch (err) { + e = err; + } + + expect(e).toBeDefined(); + expect(e.message).toEqual("Cannot set encoding for non-existent node"); + expect(existsSpy).toHaveBeenCalledWith(testNode.resourceUri); + expect(setEncodingForFileSpy).not.toHaveBeenCalled(); + expect(updateEncodingSpy).not.toHaveBeenCalled(); + expect(testNode.dirty).toEqual(false); + }); + + it("should delete a null encoding from the provider", () => { + const testParentNode = createJobNode(createJobSessionNode(createISession(), createIProfile()), createIProfile()); + const testNode = new ZoweSpoolNode({ + label: "SPOOL", + collapsibleState: vscode.TreeItemCollapsibleState.None, + spool: createIJobFile(), + parentNode: testParentNode, + }); + const updateEncodingSpy = jest.spyOn(testNode, "updateEncodingInMap").mockImplementation(); + const basename = path.posix.basename(testNode.resourceUri.path); + + testNode.dirty = false; + JobFSProvider.instance.encodingMap[basename] = { kind: "text" }; + existsSpy.mockReturnValueOnce(true); + + let e: Error; + try { + testNode.setEncoding(null); + } catch (err) { + e = err; + } + + expect(e).not.toBeDefined(); + expect(existsSpy).toHaveBeenCalledWith(testNode.resourceUri); + expect(setEncodingForFileSpy).toHaveBeenCalledWith(testNode.resourceUri, null); + expect(updateEncodingSpy).not.toHaveBeenCalled(); + expect(JobFSProvider.instance.encodingMap[basename]).not.toBeDefined(); + expect(testNode.dirty).toEqual(true); + }); + + it("should update the encoding in the provider map", () => { + const testParentNode = createJobNode(createJobSessionNode(createISession(), createIProfile()), createIProfile()); + const testNode = new ZoweSpoolNode({ + label: "SPOOL", + collapsibleState: vscode.TreeItemCollapsibleState.None, + spool: createIJobFile(), + parentNode: testParentNode, + }); + const basename = path.posix.basename(testNode.resourceUri.path); + const updateEncodingSpy = jest.spyOn(testNode, "updateEncodingInMap").mockImplementation(); + + testNode.dirty = false; + existsSpy.mockReturnValueOnce(true); + + let e: Error; + try { + testNode.setEncoding({ kind: "binary" }); + } catch (err) { + e = err; + } + + expect(e).not.toBeDefined(); + expect(existsSpy).toHaveBeenCalledWith(testNode.resourceUri); + expect(setEncodingForFileSpy).toHaveBeenCalledWith(testNode.resourceUri, { kind: "binary" }); + expect(updateEncodingSpy).toHaveBeenCalledWith(basename, { kind: "binary" }); + expect(testNode.dirty).toEqual(true); + }); +}); + describe("ZosJobsProvider - Function searchPrompt", () => { it("should exit if searchCriteria is undefined", async () => { const globalMocks = await createGlobalMocks();