'use strict' ; import * as vscode from 'vscode' ; import * as sinon from 'sinon' ; import * as chai from 'chai' ; import * as sinonChai from 'sinon-chai' ; import { HomeView } from '../../extension/webview/HomeView' ; import { View } from '../../extension/webview/View' ; import { Reporter } from '../../extension/util/Reporter' ; import { TestUtil } from '../TestUtil' ; import { GlobalState } from '../../extension/util/GlobalState' ; import { ExtensionUtil } from '../../extension/util/ExtensionUtil' ; import { ExtensionCommands } from '../../ExtensionCommands' ; const should = chai . should ( ) ; chai . use ( sinonChai ) ; describe ( 'HomeView' , ( ) => { const mySandBox = sinon . createSandbox ( ) ; let context ; let createWebviewPanelStub ; let postMessageStub ; let executeCommandStub ; let sendTelemetryEventStub ; let onDidReceiveMessagePromises ; const initialMessage = { path : '/home' , version : '1.0.0' } ; before ( async ( ) => { await TestUtil . setupTests ( mySandBox ) ; } ) ; beforeEach ( async ( ) => { context = GlobalState . getExtensionContext ( ) ; executeCommandStub = mySandBox . stub ( vscode . commands , 'executeCommand' ) ; executeCommandStub . callThrough ( ) ; executeCommandStub . withArgs ( ExtensionCommands . OPEN_TUTORIAL_GALLERY ) . resolves ( ) ; executeCommandStub . withArgs ( ExtensionCommands . OPEN_SAMPLE_PAGE ) . resolves ( ) ; createWebviewPanelStub = mySandBox . stub ( vscode . window , 'createWebviewPanel' ) ; postMessageStub = mySandBox . stub ( ) . resolves ( ) ; mySandBox . stub ( ExtensionUtil , 'getPackageJSON' ) . resolves ( { version : '1.0.0' } ) ; sendTelemetryEventStub = mySandBox . stub ( Reporter . instance ( ) , 'sendTelemetryEvent' ) ; View [ 'openPanels' ] . splice ( 0 , View [ 'openPanels' ] . length ) ; } ) ; afterEach ( ( ) => { mySandBox . restore ( ) ; } ) ; it ( 'should register and show the home page' , async ( ) => { createWebviewPanelStub . returns ( { title : 'IBM Blockchain Platform Home' , webview : { postMessage : postMessageStub , onDidReceiveMessage : mySandBox . stub ( ) , asWebviewUri : mySandBox . stub ( ) } , reveal : mySandBox . stub ( ) , dispose : mySandBox . stub ( ) , onDidDispose : mySandBox . stub ( ) , onDidChangeViewState : mySandBox . stub ( ) , _isDisposed : false } ) ; const homeView = new HomeView ( context ) ; await homeView . openView ( false ) ; createWebviewPanelStub . should . have . been . called ; postMessageStub . should . have . been . calledWith ( initialMessage ) ; } ) ; it ( 'should reveal home page if already open' , async ( ) => { createWebviewPanelStub . returns ( { webview : { postMessage : mySandBox . stub ( ) , onDidReceiveMessage : mySandBox . stub ( ) , asWebviewUri : mySandBox . stub ( ) } , title : 'IBM Blockchain Platform Home' , onDidDispose : mySandBox . stub ( ) , reveal : ( ) : void => { return ; } , _isDisposed : false } ) ; const homeView = new HomeView ( context ) ; await homeView . openView ( true ) ; await homeView . openView ( true ) ; createWebviewPanelStub . should . have . been . calledOnce ; should . equal ( createWebviewPanelStub . getCall ( 1 ) , null ) ; } ) ; it ( 'should execute a command specified in a received message' , async ( ) => { onDidReceiveMessagePromises = [ ] ; onDidReceiveMessagePromises . push ( new Promise ( ( resolve ) : void => { createWebviewPanelStub . returns ( { webview : { postMessage : mySandBox . stub ( ) , onDidReceiveMessage : async ( callback ) : Promise < void > => { await callback ( { command : ExtensionCommands . OPEN_TUTORIAL_GALLERY } ) ; resolve ( ) ; } , asWebviewUri : mySandBox . stub ( ) } , reveal : ( ) : void => { return ; } , onDidDispose : mySandBox . stub ( ) , onDidChangeViewState : mySandBox . stub ( ) , _isDisposed : false } ) ; } ) ) ; const homeView = new HomeView ( context ) ; await homeView . openView ( false ) ; await Promise . all ( onDidReceiveMessagePromises ) ; executeCommandStub . should . have . been . calledWith ( ExtensionCommands . OPEN_TUTORIAL_GALLERY ) ; } ) ; it ( 'should execute a command with additional data specified in a received message' , async ( ) => { onDidReceiveMessagePromises = [ ] ; const message = { command : ExtensionCommands . OPEN_SAMPLE_PAGE , data : [ 'fabric-samples' , 'FabCar' ] } ; onDidReceiveMessagePromises . push ( new Promise ( ( resolve ) : void => { createWebviewPanelStub . returns ( { webview : { postMessage : mySandBox . stub ( ) , onDidReceiveMessage : async ( callback ) : Promise < void > => { await callback ( message ) ; resolve ( ) ; } , asWebviewUri : mySandBox . stub ( ) } , reveal : ( ) : void => { return ; } , onDidDispose : mySandBox . stub ( ) , onDidChangeViewState : mySandBox . stub ( ) , _isDisposed : false } ) ; } ) ) ; const homeView = new HomeView ( context ) ; await homeView . openView ( false ) ; await Promise . all ( onDidReceiveMessagePromises ) ; executeCommandStub . should . have . been . calledWith ( ExtensionCommands . OPEN_SAMPLE_PAGE , ... message . data ) ; } ) ; it ( 'should send a telemetry event if a link was clicked' , async ( ) => { onDidReceiveMessagePromises = [ ] ; onDidReceiveMessagePromises . push ( new Promise ( ( resolve ) : void => { createWebviewPanelStub . returns ( { webview : { postMessage : mySandBox . stub ( ) , onDidReceiveMessage : async ( callback ) : Promise < void > => { await callback ( { command : 'telemetry' , data : 'https://cloud.ibm.com/docs/services/blockchain/howto?topic=blockchain-ibp-console-overview&cm_mmc=OSocial_Googleplus-_-Blockchain+and+Watson+Financial+Services_Blockchain-_-WW_WW-_-VS+code+link+-+about+IBM+Blockchain+Platform&cm_mmca1=000026VG&cm_mmca2=10008691' } ) ; resolve ( ) ; } , asWebviewUri : mySandBox . stub ( ) } , reveal : ( ) : void => { return ; } , onDidDispose : mySandBox . stub ( ) , onDidChangeViewState : mySandBox . stub ( ) , _isDisposed : false } ) ; } ) ) ; const homeView = new HomeView ( context ) ; await homeView . openView ( false ) ; await Promise . all ( onDidReceiveMessagePromises ) ; sendTelemetryEventStub . should . have . been . calledWith ( 'Referral' , { source : 'homepage' , destination : 'https://cloud.ibm.com/docs/services/blockchain/howto?topic=blockchain-ibp-console-overview&cm_mmc=OSocial_Googleplus-_-Blockchain+and+Watson+Financial+Services_Blockchain-_-WW_WW-_-VS+code+link+-+about+IBM+Blockchain+Platform&cm_mmca1=000026VG&cm_mmca2=10008691' } ) ; } ) ; it ( 'should send telemetry event on openPanelInner' , async ( ) => { const panel = { title : 'IBM Blockchain Platform Home' , webview : { postMessage : mySandBox . stub ( ) , onDidReceiveMessage : mySandBox . stub ( ) , asWebviewUri : mySandBox . stub ( ) } } as any ; const homeView = new HomeView ( context ) ; await homeView . openPanelInner ( panel ) ; sendTelemetryEventStub . should . have . been . calledOnceWithExactly ( 'openedView' , { openedView : 'IBM Blockchain Platform Home' } ) ; } ) ; } ) ;