'use strict' ; import * as vscode from 'vscode' ; import * as sinon from 'sinon' ; import * as chai from 'chai' ; import * as sinonChai from 'sinon-chai' ; import * as fs from 'fs-extra' ; import { TutorialView } from '../../extension/webview/TutorialView' ; import { View } from '../../extension/webview/View' ; import { Reporter } from '../../extension/util/Reporter' ; import { SettingConfigurations } from '../../extension/configurations' ; import { GlobalState } from '../../extension/util/GlobalState' ; import { ExtensionCommands } from '../../ExtensionCommands' ; import { TestUtil } from '../TestUtil' ; chai . use ( sinonChai ) ; describe ( 'TutorialView' , ( ) => { const mySandBox = sinon . createSandbox ( ) ; let context ; let createWebviewPanelStub ; let postMessageStub ; let executeCommandStub ; let onDidReceiveMessagePromises ; const dummyMarkdown = '# Header\nparagraph' ; const tutorial = { title : 'a1' , length : '4 weeks' , file : 'some/file/path' , } ; const tutorials = [ { name : 'Basic tutorials' , tutorials : [ tutorial , ] } , { name : 'Other tutorials' , tutorials : [ { title : 'something really interesting' , length : '10 minutes' , file : 'another/file/path' } ] } ] ; const initialMessage = { path : '/viewTutorial' , tutorialData : { tutorials , activeTutorial : { ... tutorial , markdown : dummyMarkdown , } , } } ; before ( async ( ) => { await TestUtil . setupTests ( mySandBox ) ; } ) ; beforeEach ( async ( ) => { mySandBox . stub ( Reporter . instance ( ) , 'sendTelemetryEvent' ) ; context = GlobalState . getExtensionContext ( ) ; executeCommandStub = mySandBox . stub ( vscode . commands , 'executeCommand' ) ; executeCommandStub . callThrough ( ) ; createWebviewPanelStub = mySandBox . stub ( vscode . window , 'createWebviewPanel' ) ; postMessageStub = mySandBox . stub ( ) . resolves ( ) ; View [ 'openPanels' ] . splice ( 0 , View [ 'openPanels' ] . length ) ; mySandBox . stub ( fs , 'readJson' ) . resolves ( tutorials ) ; mySandBox . stub ( fs , 'readFile' ) . resolves ( dummyMarkdown ) ; await vscode . workspace . getConfiguration ( ) . update ( SettingConfigurations . HOME_SHOW_ON_STARTUP , true , vscode . ConfigurationTarget . Global ) ; await vscode . workspace . getConfiguration ( ) . update ( SettingConfigurations . HOME_SHOW_ON_NEXT_ACTIVATION , false , vscode . ConfigurationTarget . Global ) ; } ) ; afterEach ( ( ) => { mySandBox . restore ( ) ; } ) ; it ( 'should register and show the tutorial' , async ( ) => { createWebviewPanelStub . returns ( { title : 'Tutorial' , webview : { postMessage : postMessageStub , onDidReceiveMessage : mySandBox . stub ( ) , asWebviewUri : mySandBox . stub ( ) } , reveal : mySandBox . stub ( ) , dispose : mySandBox . stub ( ) , onDidDispose : mySandBox . stub ( ) , onDidChangeViewState : mySandBox . stub ( ) } ) ; const tutorialView = new TutorialView ( context , tutorials [ 0 ] . name , tutorial . title ) ; await tutorialView . openView ( true ) ; createWebviewPanelStub . should . have . been . called ; const call = postMessageStub . getCall ( 0 ) ; call . args [ 0 ] . should . deep . equal ( initialMessage ) ; } ) ; it ( 'should execute a command with args 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_PAGE , data : [ 'my series' , 'my tutorial' ] } ) ; resolve ( ) ; } , asWebviewUri : mySandBox . stub ( ) } , reveal : ( ) : void => { return ; } , onDidDispose : mySandBox . stub ( ) , onDidChangeViewState : mySandBox . stub ( ) } ) ; } ) ) ; executeCommandStub . withArgs ( ExtensionCommands . OPEN_TUTORIAL_PAGE ) . resolves ( ) ; const tutorialView = new TutorialView ( context , tutorials [ 0 ] . name , tutorial . title ) ; await tutorialView . openView ( false ) ; await Promise . all ( onDidReceiveMessagePromises ) ; executeCommandStub . should . have . been . calledWith ( ExtensionCommands . OPEN_TUTORIAL_PAGE , 'my series' , 'my tutorial' ) ; } ) ; it ( 'should load component' , async ( ) => { const tutorialView = new TutorialView ( context , 'Basic tutorials' , 'A1: Introduction' ) ; tutorialView . loadComponent ( { } as vscode . WebviewPanel ) ; } ) ; } ) ;