Skip to content

Commit

Permalink
Merge pull request #197 from almightyrush/Unit-test-for-CICSProgramTree
Browse files Browse the repository at this point in the history
Unit test for cics program tree
  • Loading branch information
davenice authored Jan 9, 2025
2 parents 10fcac9 + 002f20d commit 18afd7c
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 2 deletions.
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"jest-environment-node-debug": "^2.0.0",
"jest-html-reporter": "^3.7.1",
"jest-junit": "^16.0.0",
"jest-mock-vscode": "^4.0.4",
"jest-stare": "^2.5.0",
"madge": "^6.0.0",
"npm-lockfile": "^4.0.0",
Expand Down
12 changes: 12 additions & 0 deletions packages/vsce/__tests__/__mocks__/vscode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/

module.exports = require("jest-mock-vscode").createVSCodeMock(jest);
160 changes: 160 additions & 0 deletions packages/vsce/__tests__/__unit__/trees/CICSProgramTree.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/**
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/

const getIconOpenMock = jest.fn();

import { imperative } from "@zowe/zowe-explorer-api";
import { CICSRegionTree } from "../../../src/trees/CICSRegionTree";
import { ICMCIApiResponse } from "@zowe/cics-for-zowe-sdk";
import { CICSProgramTreeItem } from "../../../src/trees/treeItems/CICSProgramTreeItem";
import * as filterUtils from "../../../src/utils/filterUtils";
import { CICSProgramTree } from "../../../src/trees/CICSProgramTree";
import CustomError from "../../__utils__/CustomError";

jest.mock("@zowe/cics-for-zowe-sdk");
const zoweSdk = require("@zowe/cics-for-zowe-sdk");

jest.mock("../../../src/utils/profileUtils", () => {
return { getIconOpen: getIconOpenMock };
});
jest.mock("../../../src/trees/treeItems/CICSProgramTreeItem");

const imperativeSession = new imperative.Session({
user: "user",
password: "pwd",
hostname: "hostname",
protocol: "https",
type: "basic",
rejectUnauthorized: false,
});
const CICSSessionTreeMock = {
session: imperativeSession,
};

const cicsRegionTreeMock = {
parentSession: CICSSessionTreeMock,
getRegionName: () => "IYK2ZXXX",
parentPlex: {
getPlexName: () => "PLEXX",
},
};
const CICSProgramTreeItemMock = {};
const getResourceMock = jest.spyOn(zoweSdk, "getResource");
const iconPath = "/icon/path";
const resourceName = "testResource";
const cicsprogram = "cicsprogram";
const value = "NOT (PROGRAM=CEE* OR PROGRAM=DFH* OR PROGRAM=CJ* OR PROGRAM=EYU* OR PROGRAM=CSQ* OR PROGRAM=CEL* OR PROGRAM=IGZ*)";
const ICMCIApiResponseMock: ICMCIApiResponse = {
response: {
resultsummary: { api_response1: "1024", api_response2: "0", recordcount: "0", displayed_recordcount: "0" },
records: {},
},
};

describe("Test suite for CICSProgramTree", () => {
let sut: CICSProgramTree;

beforeEach(() => {
getIconOpenMock.mockReturnValue(iconPath);
sut = new CICSProgramTree(cicsRegionTreeMock as any as CICSRegionTree);
expect(getIconOpenMock).toHaveBeenCalledWith(false);
});

afterEach(() => {
jest.resetAllMocks();
});

describe("Test suite for addProgram()", () => {
it("Should add CICSProgramTreeItem into program", () => {
sut.addProgram(CICSProgramTreeItemMock as any as CICSProgramTreeItem);
expect(sut.children.length).toBeGreaterThanOrEqual(1);
});
});

describe("Test suite for loadContents()", () => {
let getDefaultProgramFilter: jest.SpyInstance;

beforeEach(() => {
getDefaultProgramFilter = jest.spyOn(filterUtils, "getDefaultProgramFilter").mockResolvedValueOnce(value);
getResourceMock.mockImplementation(async () => ICMCIApiResponseMock);
});
afterEach(() => {
getResourceMock.mockClear();
jest.resetAllMocks();
});

it("Should add newProgramItem into the addProgram() and activeFilter is undefined", async () => {
ICMCIApiResponseMock.response.records[resourceName.toLowerCase()] = [{ prop: "test1" }, { prop: "test2" }];

await sut.loadContents();
expect(getDefaultProgramFilter).toHaveBeenCalled();
expect(sut.activeFilter).toBeUndefined();
expect(sut.children.length).toBeGreaterThanOrEqual(1);
expect(getIconOpenMock).toHaveBeenCalledWith(true);
});

it("Should add newProgramItem into the addProgram() and invoke toEscapedCriteriaString when activeFilter is defined", async () => {
sut.activeFilter = "Active";
ICMCIApiResponseMock.response.records[cicsprogram.toLowerCase()] = [{ prop: "test1" }, { prop: "test2" }];
const toEscapedCriteriaString = jest.spyOn(filterUtils, "toEscapedCriteriaString").mockReturnValueOnce("PROGRAM");

await sut.loadContents();
expect(toEscapedCriteriaString).toHaveBeenCalled();
expect(sut.activeFilter).toBeDefined();
expect(sut.children.length).toBeGreaterThanOrEqual(1);
expect(getIconOpenMock).toHaveBeenCalledWith(true);
});

it("Should throw exception when error.mMessage includes {exceeded a resource limit}", async () => {
getResourceMock.mockRejectedValue(new CustomError("Error in the method exceeded a resource limit"));
await sut.loadContents();
expect(getResourceMock).toHaveBeenCalled();
});

it("Should throw exception when error.mMessage include {exceeded a resource limit}", async () => {
getResourceMock.mockRejectedValue(new CustomError("Error in the method"));

await sut.loadContents();
expect(getResourceMock).toHaveBeenCalled();
expect(sut.label).toEqual("Programs [0]");
});
});

describe("Test suite for clearFilter", () => {
it("Should clear active filter to undefined and set contextValue to unfiltered", () => {
sut.activeFilter = "Active";

sut.clearFilter();
expect(sut.activeFilter).toBeUndefined();
expect(sut.contextValue).toEqual("cicstreeprogram.unfiltered.programs");
});
});

describe("Test suite for setFilter", () => {
it("Should set active filter and set contextValue to filtered", () => {
sut.setFilter("ActiveFilter");
expect(sut.activeFilter).toEqual("ActiveFilter");
expect(sut.contextValue).toEqual("cicstreeprogram.filtered.programs");
});
});

describe("Test suite for getFilter", () => {
it("Should return activeFilter object", () => {
expect(sut.getFilter()).toBe(sut.activeFilter);
});
});

describe("Test suite for getParent", () => {
it("Should return parentRegion object", () => {
expect(sut.getParent()).toBe(sut.parentRegion);
});
});
});
18 changes: 18 additions & 0 deletions packages/vsce/__tests__/__utils__/CustomError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/

export default class CustomError extends Error {
mMessage: String | undefined;
constructor(message: String) {
super();
this.mMessage = message;
}
}
2 changes: 1 addition & 1 deletion packages/vsce/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@
"clean": "rimraf lib && rimraf dist",
"watch": "tsup --watch",
"test:system": "echo 'Please run the system tests via VSCode UI' && exit 0",
"test:unit": "jest --config unit.jest_config.ts --no-coverage",
"test:unit": "jest --config unit.jest_config.ts",
"test": "npm run test:unit",
"tsupDep": "node ./scripts/tsup-deps.js",
"getPrebuilds": "node ./scripts/getSecretsPrebuilds.js",
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"sourceMap": true,
"newLine": "lf",
"strictNullChecks": false,
"noUnusedLocals": false
"noUnusedLocals": false,
"skipLibCheck": true
},
"exclude": [
"lib",
Expand Down

0 comments on commit 18afd7c

Please sign in to comment.