Skip to content

Commit

Permalink
Add more test suites
Browse files Browse the repository at this point in the history
Signed-off-by: likhithanimma1 <142219673+likhithanimma1@users.noreply.github.com>
  • Loading branch information
likhithanimma1 committed Oct 6, 2024
1 parent cd80771 commit 1765511
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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" });
Expand All @@ -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", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -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
Expand All @@ -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();
});
});
});
14 changes: 7 additions & 7 deletions packages/zowe-explorer/src/webviews/src/edit-history/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import * as l10n from "@vscode/l10n";
export function App(): JSXInternal.Element {
const [timestamp, setTimestamp] = useState<Date | undefined>();
const [currentTab, setCurrentTab] = useState<{ [key: string]: string }>({});
const [localization, setLocalization] = useState<string | undefined>("");

useEffect(() => {
window.addEventListener("message", (event) => {
Expand All @@ -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 (
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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],
}));
}
}
});
}, []);
Expand Down
3 changes: 0 additions & 3 deletions packages/zowe-explorer/src/zosconsole/ZosConsolePanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 1765511

Please sign in to comment.