Skip to content

Commit 8bc4be8

Browse files
committed
added unit tests for JDK Downloader view
1 parent bc6c56b commit 8bc4be8

File tree

7 files changed

+204
-140
lines changed

7 files changed

+204
-140
lines changed

vscode/.nycrc.json

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
{
22
"extends": "@istanbuljs/nyc-config-typescript",
3-
"reporter":[
3+
"reporter": [
44
"text",
55
"html",
66
"text-summary",
77
"cobertura"
88
],
9-
"all": true,
10-
"cache": false,
9+
"all": false,
1110
"include": [
12-
"src/**/*.ts", "out/**/*.js"
11+
"src/**/*.ts",
12+
"out/**/*.js"
1313
],
14-
"exclude": [ "src/test/**/*.ts", "out/test/**/*.js"]
14+
"exclude": [
15+
"src/test/**/*.ts",
16+
"out/test/**/*.js",
17+
"out/utils.js",
18+
"out/logger.js",
19+
"out/constants.js"
20+
]
1521
}

vscode/src/test/unit/mocks/vscode/namespaces/window.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const mockWindowNamespace = (mockedVSCode: Partial<VSCode>) => {
99
mockedVSCode.window = instance(mockedWindow);
1010
mockCreateWebViewPanel();
1111
mockCreateOutputChannel();
12+
mockMessageView();
1213
}
1314

1415
const mockCreateWebViewPanel = () => {
@@ -27,3 +28,9 @@ const mockCreateOutputChannel = () => {
2728
anyString()
2829
)).thenReturn(instance(mockedOutputChannel));
2930
}
31+
32+
const mockMessageView = () => {
33+
when(mockedWindow.showErrorMessage(anyString())).thenReturn(Promise.resolve(anyString()));
34+
when(mockedWindow.showInformationMessage(anyString())).thenReturn(Promise.resolve(undefined));
35+
when(mockedWindow.showWarningMessage(anyString())).thenReturn(Promise.resolve(undefined));
36+
}

vscode/src/test/unit/testUtils.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { LOGGER } from "../../logger";
2+
3+
export const getOsType = (): string => {
4+
switch (process.platform) {
5+
case "linux":
6+
return "linux";
7+
case "darwin":
8+
return "macOS";
9+
default:
10+
return "windows";
11+
}
12+
}
13+
14+
export const getMachineArch = (): string => {
15+
switch (process.arch) {
16+
case "arm64":
17+
return "aarch64";
18+
default:
19+
return "x64";
20+
}
21+
}
22+
23+
export const getHtmlTagContent = (html: string, tagName: string): string => {
24+
const regex = new RegExp(`<${tagName}[^>]*>(.*?)<\/${tagName}>`, 'is');
25+
const match = html.match(regex);
26+
return match?.[1]?.trim() || '';
27+
}
28+
29+
export const checkTagContentNotEmpty = (html: string, tagName: string): boolean => {
30+
const content = getHtmlTagContent(html, tagName);
31+
return content.length !== 0;
32+
}
33+
34+
export const enableMockedLoggers = (sandbox: sinon.SinonSandbox) => {
35+
sandbox.stub(LOGGER, 'log').callsFake((message) => {
36+
console.log(message);
37+
});
38+
sandbox.stub(LOGGER, 'error').callsFake((message) => {
39+
console.error(message);
40+
});
41+
sandbox.stub(LOGGER, 'warn').callsFake((message) => {
42+
console.warn(message);
43+
});
44+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import { describe, it, beforeEach, afterEach } from 'mocha';
2+
import { expect } from 'chai';
3+
import * as sinon from 'sinon';
4+
import { WebviewPanel, window } from 'vscode';
5+
import { JdkDownloaderView } from '../../../../webviews/jdkDownloader/view';
6+
import { checkTagContentNotEmpty, enableMockedLoggers, getMachineArch, getOsType } from '../../testUtils';
7+
8+
describe('JDK Downloader view tests', () => {
9+
let jdkDownloaderView: JdkDownloaderView;
10+
const sandbox = sinon.createSandbox();
11+
12+
beforeEach(() => {
13+
jdkDownloaderView = new JdkDownloaderView();
14+
if (process.env.ENABLE_CONSOLE_LOG) {
15+
enableMockedLoggers(sandbox);
16+
}
17+
});
18+
19+
afterEach(() => {
20+
sandbox.restore();
21+
});
22+
23+
describe('JDK Downloader createView tests', () => {
24+
let onDidReceiveMessageStub: sinon.SinonStub;
25+
let createWebviewPanelStub: sinon.SinonStub;
26+
let webviewPanel: WebviewPanel;
27+
28+
beforeEach(() => {
29+
createWebviewPanelStub = sandbox.stub(window, 'createWebviewPanel');
30+
onDidReceiveMessageStub = sandbox.stub();
31+
32+
webviewPanel = {
33+
webview: {
34+
html: '',
35+
onDidReceiveMessage: onDidReceiveMessageStub
36+
},
37+
dispose: sandbox.stub()
38+
} as unknown as WebviewPanel;
39+
40+
createWebviewPanelStub.returns(webviewPanel);
41+
42+
jdkDownloaderView.createView();
43+
});
44+
45+
afterEach(() => {
46+
sandbox.restore();
47+
});
48+
49+
describe("Webview creation tests", () => {
50+
it("should create a webview panel", () => {
51+
expect(createWebviewPanelStub.calledOnce).to.be.true;
52+
});
53+
54+
it("should check arguments while creating webview panel", () => {
55+
const [, title, , options] = createWebviewPanelStub.firstCall.args;
56+
expect(title).to.be.a('string').and.not.to.be.empty;
57+
expect(options).to.deep.include({ enableScripts: true });
58+
});
59+
});
60+
61+
describe("Default dropdown options tests", () => {
62+
it("should detect correct OS type", () => {
63+
const actualOsType = (jdkDownloaderView as any).osType;
64+
const expectedOs = getOsType();
65+
66+
expect(actualOsType).equals(expectedOs);
67+
});
68+
69+
it("should detect correct machine architecture type", () => {
70+
const actualMachineArch = (jdkDownloaderView as any).machineArch;
71+
const expectedMachineArch = getMachineArch();
72+
73+
expect(actualMachineArch).equals(expectedMachineArch);
74+
});
75+
});
76+
77+
describe("Webview HTML tests", () => {
78+
let jdkDownloaderHtml: string;
79+
beforeEach(() => {
80+
jdkDownloaderHtml = webviewPanel.webview.html;
81+
});
82+
83+
it("should set the webview HTML", () => {
84+
expect(jdkDownloaderHtml).to.be.a('string').and.not.to.be.empty;
85+
});
86+
87+
it("should check HTML has all the high level tags", () => {
88+
expect(jdkDownloaderHtml).to.include('<!DOCTYPE html>');
89+
expect(jdkDownloaderHtml).to.include('<head>');
90+
expect(jdkDownloaderHtml).to.include('<body>');
91+
expect(jdkDownloaderHtml).to.include('<script>');
92+
expect(jdkDownloaderHtml).to.include('<style>');
93+
});
94+
95+
it("should check important html tags are not empty", () => {
96+
expect(checkTagContentNotEmpty(jdkDownloaderHtml, 'body')).to.be.true;
97+
expect(checkTagContentNotEmpty(jdkDownloaderHtml, 'head')).to.be.true;
98+
expect(checkTagContentNotEmpty(jdkDownloaderHtml, 'script')).to.be.true;
99+
expect(checkTagContentNotEmpty(jdkDownloaderHtml, 'style')).to.be.true;
100+
expect(checkTagContentNotEmpty(jdkDownloaderHtml, 'title')).to.be.true;
101+
});
102+
103+
it("should check if correct default OS type is chosen on the options", () => {
104+
const expectedOs = getOsType();
105+
const osOptionRegex = new RegExp(`<option value="${expectedOs}"[^>]*selected[^>]*>`);
106+
expect(jdkDownloaderHtml).to.match(osOptionRegex);
107+
});
108+
109+
it("should check if correct default machine architecture is chosen on the options", () => {
110+
const expectedArch = getMachineArch();
111+
const archOptionRegex = new RegExp(`<option value="${expectedArch}"[^>]*selected[^>]*>`);
112+
expect(jdkDownloaderHtml).to.match(archOptionRegex);
113+
});
114+
});
115+
116+
it("should attach a message listener to the webview", () => {
117+
expect(onDidReceiveMessageStub.calledOnce).to.be.true;
118+
const listener = onDidReceiveMessageStub.firstCall.args[0];
119+
expect(listener).to.be.a('function');
120+
});
121+
122+
});
123+
124+
it("should dispose the webview", () => {
125+
const disposeStub = sandbox.stub();
126+
(jdkDownloaderView as any).jdkDownloaderWebView = { dispose: disposeStub };
127+
128+
jdkDownloaderView.disposeView();
129+
130+
expect(disposeStub.calledOnce).to.be.true;
131+
});
132+
133+
it("should handle errors when creating view", () => {
134+
const errorMessage = "Test error";
135+
sandbox.stub(window, 'createWebviewPanel').throws(new Error(errorMessage));
136+
const showErrorMessageStub = sandbox.stub(window, 'showErrorMessage');
137+
jdkDownloaderView.createView();
138+
139+
expect(showErrorMessageStub.calledOnce).to.be.true;
140+
});
141+
});

vscode/src/test/unit/webviews/jdkDownloaderAction.unit.test.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

vscode/src/test/unit/webviews/jdkDownloaderView.unit.test.ts

Lines changed: 0 additions & 101 deletions
This file was deleted.

vscode/src/webviews/jdkDownloader/action.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
limitations under the License.
1515
*/
1616

17-
import { commands, OpenDialogOptions, OutputChannel, window, workspace } from "vscode";
17+
import { commands, OpenDialogOptions, window, workspace } from "vscode";
1818
import { JdkDownloaderView } from "./view";
1919
import { jdkDownloaderConstants } from "../../constants";
2020
import * as path from 'path';

0 commit comments

Comments
 (0)