import * as chai from 'chai' ; import * as sinon from 'sinon' ; import * as sinonChai from 'sinon-chai' ; import * as vscode from 'vscode' ; import { TestUtil } from '../TestUtil' ; import { GlobalState , ExtensionData , DEFAULT_EXTENSION_DATA , EXTENSION_DATA_KEY } from '../../extension/util/GlobalState' ; import { VSCodeBlockchainOutputAdapter } from '../../extension/logging/VSCodeBlockchainOutputAdapter' ; import { LogType } from 'ibm-blockchain-platform-common' ; chai . use ( sinonChai ) ; describe ( 'GlobalState' , ( ) => { let mySandBox ; let context ; before ( async ( ) => { mySandBox = sinon . createSandbox ( ) ; await TestUtil . setupTests ( mySandBox ) ; } ) ; beforeEach ( async ( ) => { context = GlobalState . getExtensionContext ( ) ; const extensionData = DEFAULT_EXTENSION_DATA ; await GlobalState . update ( extensionData ) ; } ) ; afterEach ( ( ) => { mySandBox . restore ( ) ; } ) ; describe ( 'get' , ( ) => { it ( 'should get global state' , async ( ) => { const getStateSpy = mySandBox . spy ( context . globalState , 'get' ) ; const extensionData = GlobalState . get ( ) ; extensionData . should . deep . equal ( DEFAULT_EXTENSION_DATA ) ; getStateSpy . should . have . been . calledOnceWith ( EXTENSION_DATA_KEY , DEFAULT_EXTENSION_DATA ) ; } ) ; it ( 'should handle any problems getting the global state' , async ( ) => { const error = new Error ( `can't get global state` ) ; const getStateStub = mySandBox . stub ( context . globalState , 'get' ) . throws ( error ) ; const logSpy = mySandBox . spy ( VSCodeBlockchainOutputAdapter . instance ( ) , 'log' ) ; ( ( ) : any => { GlobalState . get ( ) ; } ) . should . throw ( error ) ; getStateStub . should . have . been . calledOnceWith ( EXTENSION_DATA_KEY , DEFAULT_EXTENSION_DATA ) ; logSpy . should . have . been . calledOnceWithExactly ( LogType . ERROR , `Unable to get extension's global state: ${ error . message }` , `Unable to get extension's global state: ${ error . toString ( ) }` ) ; } ) ; } ) ; describe ( 'update' , ( ) => { it ( 'should update global state' , async ( ) => { const updateStateSpy = mySandBox . spy ( context . globalState , 'update' ) ; const newExtensionData = DEFAULT_EXTENSION_DATA ; newExtensionData . generatorVersion = '0.0.0' ; await GlobalState . update ( newExtensionData ) ; updateStateSpy . should . have . been . calledOnceWith ( EXTENSION_DATA_KEY , newExtensionData ) ; } ) ; it ( 'should handle any problems updating the global state' , async ( ) => { const error = new Error ( `can't update global state` ) ; const updateStateStub = mySandBox . stub ( context . globalState , 'update' ) . throws ( error ) ; const newExtensionData = DEFAULT_EXTENSION_DATA ; newExtensionData . generatorVersion = '0.0.0' ; const logSpy = mySandBox . spy ( VSCodeBlockchainOutputAdapter . instance ( ) , 'log' ) ; await GlobalState . update ( newExtensionData ) . should . be . rejectedWith ( error ) ; updateStateStub . should . have . been . calledOnceWith ( EXTENSION_DATA_KEY , newExtensionData ) ; logSpy . should . have . been . calledOnceWithExactly ( LogType . ERROR , `Unable to update extension's global state: ${ error . message }` , `Unable to update extension's global state: ${ error . toString ( ) }` ) ; } ) ; } ) ; describe ( 'reset' , ( ) => { it ( 'should reset global state' , async ( ) => { const extensionData = DEFAULT_EXTENSION_DATA ; extensionData . generatorVersion = '0.0.0' ; await GlobalState . update ( extensionData ) ; const writtenState = GlobalState . get ( ) ; writtenState . should . deep . equal ( extensionData ) ; const updateStateSpy = mySandBox . spy ( context . globalState , 'update' ) ; await GlobalState . reset ( ) ; updateStateSpy . should . have . been . calledOnceWith ( EXTENSION_DATA_KEY , DEFAULT_EXTENSION_DATA ) ; } ) ; } ) ; } ) ;