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+ } ) ;
0 commit comments