From 176551155f0bb22ead4a8b7b92ad2ea8cff4cec9 Mon Sep 17 00:00:00 2001 From: likhithanimma1 <142219673+likhithanimma1@users.noreply.github.com> Date: Sun, 6 Oct 2024 21:53:23 +0530 Subject: [PATCH] Add more test suites Signed-off-by: likhithanimma1 <142219673+likhithanimma1@users.noreply.github.com> --- .../shared/SharedHistoryView.unit.test.ts | 30 ++++++++++++++--- .../trees/uss/USSAttributeView.unit.test.ts | 21 +++++++++++- .../utils/CertificateWizard.unit.test.ts | 33 ++++++++++++++++++- .../zosconsole/ZosConsolePanel.unit.test.ts | 26 +++++++++++---- .../src/webviews/src/edit-history/App.tsx | 14 ++++---- .../PersistentTable/PersistentDataPanel.tsx | 12 ++++--- .../src/zosconsole/ZosConsolePanel.ts | 3 -- 7 files changed, 111 insertions(+), 28 deletions(-) diff --git a/packages/zowe-explorer/__tests__/__unit__/trees/shared/SharedHistoryView.unit.test.ts b/packages/zowe-explorer/__tests__/__unit__/trees/shared/SharedHistoryView.unit.test.ts index ef4feced6..39784dc52 100644 --- a/packages/zowe-explorer/__tests__/__unit__/trees/shared/SharedHistoryView.unit.test.ts +++ b/packages/zowe-explorer/__tests__/__unit__/trees/shared/SharedHistoryView.unit.test.ts @@ -69,7 +69,10 @@ function createGlobalMocks(): any { }, configurable: true, }); - + const spyReadFile = jest.fn((path, encoding, callback) => { + callback(null, "file contents"); + }); + Object.defineProperty(fs, "readFile", { value: spyReadFile, configurable: true }); return globalMocks; } @@ -284,10 +287,6 @@ describe("HistoryView Unit Tests", () => { it("should handle the case where 'GET_LOCALIZATION' is the command sent", async () => { const globalMocks = await createGlobalMocks(); const blockMocks = createBlockMocks(globalMocks); - const spyReadFile = jest.fn((path, encoding, callback) => { - callback(null, "file contents"); - }); - Object.defineProperty(fs, "readFile", { value: spyReadFile }); const historyView = await initializeHistoryViewMock(blockMocks, globalMocks); const postMessageSpy = jest.spyOn(historyView.panel.webview, "postMessage"); await historyView["onDidReceiveMessage"]({ command: "GET_LOCALIZATION" }); @@ -296,6 +295,27 @@ describe("HistoryView Unit Tests", () => { contents: "file contents", }); }); + + it("if this.panel doesn't exist in GET_LOCALIZATION", async () => { + const globalMocks = await createGlobalMocks(); + const blockMocks = createBlockMocks(globalMocks); + const historyView = await initializeHistoryViewMock(blockMocks, globalMocks); + historyView.panel = undefined as any; + await historyView["onDidReceiveMessage"]({ command: "GET_LOCALIZATION" }); + expect(historyView.panel).toBeUndefined(); + }); + + it("if read file throwing an error in GET_LOCALIZATION", async () => { + const globalMocks = await createGlobalMocks(); + const blockMocks = createBlockMocks(globalMocks); + const spyReadFile = jest.fn((path, encoding, callback) => { + callback("error", "file contents"); + }); + Object.defineProperty(fs, "readFile", { value: spyReadFile, configurable: true }); + const historyView = await initializeHistoryViewMock(blockMocks, globalMocks); + await historyView["onDidReceiveMessage"]({ command: "GET_LOCALIZATION" }); + expect(spyReadFile).toHaveBeenCalledTimes(1); + }); }); describe("getHistoryData", () => { diff --git a/packages/zowe-explorer/__tests__/__unit__/trees/uss/USSAttributeView.unit.test.ts b/packages/zowe-explorer/__tests__/__unit__/trees/uss/USSAttributeView.unit.test.ts index 566d0af44..625449320 100644 --- a/packages/zowe-explorer/__tests__/__unit__/trees/uss/USSAttributeView.unit.test.ts +++ b/packages/zowe-explorer/__tests__/__unit__/trees/uss/USSAttributeView.unit.test.ts @@ -132,11 +132,30 @@ describe("AttributeView unit tests", () => { const spyReadFile = jest.fn((path, encoding, callback) => { callback(null, "file contents"); }); - Object.defineProperty(fs, "readFile", { value: spyReadFile }); + Object.defineProperty(fs, "readFile", { value: spyReadFile, configurable: true }); await (view as any).onDidReceiveMessage({ command: "GET_LOCALIZATION" }); expect(view.panel.webview.postMessage).toHaveBeenCalledWith({ command: "GET_LOCALIZATION", contents: "file contents", }); }); + + it("if this.panel doesn't exist in GET_LOCALIZATION", async () => { + const spyReadFile = jest.fn((path, encoding, callback) => { + callback(null, "file contents"); + }); + Object.defineProperty(fs, "readFile", { value: spyReadFile, configurable: true }); + view.panel = undefined as any; + await (view as any).onDidReceiveMessage({ command: "GET_LOCALIZATION" }); + expect(view.panel).toBeUndefined(); + }); + + it("if read file throwing an error in GET_LOCALIZATION", async () => { + const spyReadFile = jest.fn((path, encoding, callback) => { + callback("error", "file contents"); + }); + Object.defineProperty(fs, "readFile", { value: spyReadFile, configurable: true }); + await (view as any).onDidReceiveMessage({ command: "GET_LOCALIZATION" }); + expect(spyReadFile).toHaveBeenCalledTimes(1); + }); }); diff --git a/packages/zowe-explorer/__tests__/__unit__/utils/CertificateWizard.unit.test.ts b/packages/zowe-explorer/__tests__/__unit__/utils/CertificateWizard.unit.test.ts index 558edec03..960b22b68 100644 --- a/packages/zowe-explorer/__tests__/__unit__/utils/CertificateWizard.unit.test.ts +++ b/packages/zowe-explorer/__tests__/__unit__/utils/CertificateWizard.unit.test.ts @@ -109,7 +109,7 @@ describe("CertificateWizard", () => { const spyReadFile = jest.fn((path, encoding, callback) => { callback(null, "file contents"); }); - Object.defineProperty(fs, "readFile", { value: spyReadFile }); + Object.defineProperty(fs, "readFile", { value: spyReadFile, configurable: true }); const certWizard = new CertificateWizard(context, { cert: "/a/b/cert.pem", certKey: "/a/b/cert.key.pem", @@ -121,4 +121,35 @@ describe("CertificateWizard", () => { (certWizard as any).data = "file contents"; expect(postMessageMock).toHaveBeenCalledWith({ command: "GET_LOCALIZATION", contents: (certWizard as any).data }); }); + + it("if this.panel doesn't exist in GET_LOCALIZATION", async () => { + const spyReadFile = jest.fn((path, encoding, callback) => { + callback(null, "file contents"); + }); + Object.defineProperty(fs, "readFile", { value: spyReadFile, configurable: true }); + const certWizard = new CertificateWizard(context, { + cert: "/a/b/cert.pem", + certKey: "/a/b/cert.key.pem", + }); + certWizard.panel = undefined as any; + (certWizard as any).onDidReceiveMessage({ + command: "GET_LOCALIZATION", + }); + expect(certWizard.panel).toBeUndefined(); + }); + + it("if read file throwing an error in GET_LOCALIZATION", async () => { + const spyReadFile = jest.fn((path, encoding, callback) => { + callback("error", "file contents"); + }); + Object.defineProperty(fs, "readFile", { value: spyReadFile, configurable: true }); + const certWizard = new CertificateWizard(context, { + cert: "/a/b/cert.pem", + certKey: "/a/b/cert.key.pem", + }); + (certWizard as any).onDidReceiveMessage({ + command: "GET_LOCALIZATION", + }); + expect(spyReadFile).toHaveBeenCalledTimes(1); + }); }); diff --git a/packages/zowe-explorer/__tests__/__unit__/zosconsole/ZosConsolePanel.unit.test.ts b/packages/zowe-explorer/__tests__/__unit__/zosconsole/ZosConsolePanel.unit.test.ts index 860d501d7..c5a3c4870 100644 --- a/packages/zowe-explorer/__tests__/__unit__/zosconsole/ZosConsolePanel.unit.test.ts +++ b/packages/zowe-explorer/__tests__/__unit__/zosconsole/ZosConsolePanel.unit.test.ts @@ -41,7 +41,10 @@ describe("ZosConsoleViewProvider", () => { configurable: true, }); Object.defineProperty(vscode.Uri, "joinPath", { value: jest.fn(), configurable: true }); - + const spyReadFile = jest.fn((path, encoding, callback) => { + callback(null, "file contents"); + }); + Object.defineProperty(fs, "readFile", { value: spyReadFile, configurable: true }); return newMocks; } describe("resolveWebviewView", () => { @@ -51,12 +54,8 @@ describe("ZosConsoleViewProvider", () => { myconsole.resolveWebviewView(globalMocks.testWebView, {} as any, { isCancellationRequested: false } as any); expect(globalMocks.testWebView.webview.onDidReceiveMessage).toHaveBeenCalled(); }); - it("handles the get_localozation message", async () => { + it("handles the get_localization message", async () => { const globalMocks = createGlobalMocks(); - const spyReadFile = jest.fn((path, encoding, callback) => { - callback(null, "file contents"); - }); - Object.defineProperty(fs, "readFile", { value: spyReadFile }); const myconsole = new ZosConsoleViewProvider({} as any); const postMessageMock = jest.spyOn(globalMocks.testWebView.webview, "postMessage").mockImplementation(); const onDidReceiveMessageCallback = jest @@ -69,5 +68,20 @@ describe("ZosConsoleViewProvider", () => { expect(onDidReceiveMessageCallback).toHaveBeenCalled(); expect(postMessageMock).toHaveBeenCalledWith({ type: "GET_LOCALIZATION", contents: (myconsole as any).data }); }); + it("handles the get_localization message", async () => { + const globalMocks = createGlobalMocks(); + const spyReadFile = jest.fn((path, encoding, callback) => { + callback("error", "file contents"); + }); + Object.defineProperty(fs, "readFile", { value: spyReadFile, configurable: true }); + const myconsole = new ZosConsoleViewProvider({} as any); + const onDidReceiveMessageCallback = jest + .spyOn(globalMocks.testWebView.webview, "onDidReceiveMessage") + .mockImplementation((callback: any) => { + callback({ command: "GET_LOCALIZATION" }); + }); + myconsole.resolveWebviewView(globalMocks.testWebView, {} as any, { isCancellationRequested: false } as any); + expect(onDidReceiveMessageCallback).toHaveBeenCalled(); + }); }); }); diff --git a/packages/zowe-explorer/src/webviews/src/edit-history/App.tsx b/packages/zowe-explorer/src/webviews/src/edit-history/App.tsx index e04628f57..bd01b3aee 100644 --- a/packages/zowe-explorer/src/webviews/src/edit-history/App.tsx +++ b/packages/zowe-explorer/src/webviews/src/edit-history/App.tsx @@ -21,7 +21,6 @@ import * as l10n from "@vscode/l10n"; export function App(): JSXInternal.Element { const [timestamp, setTimestamp] = useState(); const [currentTab, setCurrentTab] = useState<{ [key: string]: string }>({}); - const [localization, setLocalization] = useState(""); useEffect(() => { window.addEventListener("message", (event) => { @@ -33,18 +32,19 @@ export function App(): JSXInternal.Element { l10n.config({ contents: contents, }); - setLocalization(contents); } - if ("tab" in event.data) { - setCurrentTab(() => ({ - tab: event.data.tab, - })); + if (event.data.command === "ready") { + if ("tab" in event.data) { + setCurrentTab(() => ({ + tab: event.data.tab, + })); + } } setTimestamp(new Date()); }); PersistentVSCodeAPI.getVSCodeAPI().postMessage({ command: "GET_LOCALIZATION" }); PersistentVSCodeAPI.getVSCodeAPI().postMessage({ command: "ready" }); - }, [localization]); + }, []); return (
diff --git a/packages/zowe-explorer/src/webviews/src/edit-history/components/PersistentTable/PersistentDataPanel.tsx b/packages/zowe-explorer/src/webviews/src/edit-history/components/PersistentTable/PersistentDataPanel.tsx index 9c5a88156..b8b56e6a1 100644 --- a/packages/zowe-explorer/src/webviews/src/edit-history/components/PersistentTable/PersistentDataPanel.tsx +++ b/packages/zowe-explorer/src/webviews/src/edit-history/components/PersistentTable/PersistentDataPanel.tsx @@ -55,12 +55,14 @@ export default function PersistentDataPanel({ type }: Readonly<{ type: Readonly< if (!isSecureOrigin(event.origin)) { return; } - setData(event.data); + if (event.data.command === "ready") { + setData(event.data); - if ("selection" in event.data) { - setSelection(() => ({ - [type]: event.data.selection[type], - })); + if ("selection" in event.data) { + setSelection(() => ({ + [type]: event.data.selection[type], + })); + } } }); }, []); diff --git a/packages/zowe-explorer/src/zosconsole/ZosConsolePanel.ts b/packages/zowe-explorer/src/zosconsole/ZosConsolePanel.ts index 549541477..b6d690167 100644 --- a/packages/zowe-explorer/src/zosconsole/ZosConsolePanel.ts +++ b/packages/zowe-explorer/src/zosconsole/ZosConsolePanel.ts @@ -91,9 +91,6 @@ export class ZosConsoleViewProvider implements vscode.WebviewViewProvider { // File doesn't exist, fallback to English strings return; } - if (!webviewView) { - return; - } webviewView.webview.postMessage({ type: "GET_LOCALIZATION", contents: data,