Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew W. Harn <[email protected]>
  • Loading branch information
awharn committed Dec 12, 2024
1 parent b0512af commit 3350f16
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 = {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 3350f16

Please sign in to comment.