'use strict' ; import * as chai from 'chai' ; import * as sinon from 'sinon' ; import * as sinonChai from 'sinon-chai' ; import * as vscode from 'vscode' ; import { TimerUtil } from '../../extension/util/TimerUtil' ; import { ExtensionCommands } from '../../ExtensionCommands' ; import { FabricEnvironmentRegistryEntry , EnvironmentType } from 'ibm-blockchain-platform-common' ; chai . use ( sinonChai ) ; describe ( 'TimerUtil Test' , ( ) => { let mySandBox ; let clock ; let executeCommandStub ; before ( async ( ) => { mySandBox = sinon . createSandbox ( ) ; } ) ; beforeEach ( ( ) => { clock = sinon . useFakeTimers ( ) ; executeCommandStub = mySandBox . stub ( vscode . commands , 'executeCommand' ) ; } ) ; afterEach ( ( ) => { clock . restore ( ) ; mySandBox . restore ( ) ; } ) ; describe ( 'setInterval' , ( ) => { it ( 'should call the set command at a given interval' , ( ) => { const entry = new FabricEnvironmentRegistryEntry ( { name : 'myFabric' , environmentType : EnvironmentType . ENVIRONMENT } ) ; const result = TimerUtil . setInterval ( ExtensionCommands . CONNECT_TO_ENVIRONMENT , [ entry ] , 10000 ) ; clock . tick ( 10000 ) ; executeCommandStub . should . have . been . calledOnceWith ( ExtensionCommands . CONNECT_TO_ENVIRONMENT , entry ) ; result . should . exist ; } ) ; } ) ; describe ( 'cancelInterval' , ( ) => { it ( 'should cancel the interval' , ( ) => { const timeoutObj = { name : 'timeout' } ; const clearIntervalStub = mySandBox . stub ( global , 'clearInterval' ) ; TimerUtil . cancelInterval ( timeoutObj ) ; clearIntervalStub . should . have . been . calledWith ( timeoutObj ) ; } ) ; } ) ; describe ( 'sleep' , ( ) => { it ( 'should delay for the specified time' , async ( ) => { const stub = mySandBox . stub ( ) ; const p = TimerUtil . sleep ( 2000 ) . then ( stub ) ; sinon . assert . notCalled ( stub ) ; clock . tick ( 2300 ) ; await p . should . be . eventually . fulfilled ; sinon . assert . calledOnce ( stub ) ; } ) ; } ) ; } ) ;