import 'mocha' ; import { expect } from 'chai' ; import { ApplicationFactory , ApplicationGetter } from '@/application/ApplicationFactory' ; import { ApplicationStub } from '@tests/unit/stubs/ApplicationStub' ; describe ( 'ApplicationFactory' , ( ) => { describe ( 'ctor' , ( ) => { it ( 'throws if getter is undefined' , ( ) => { const expectedError = 'undefined getter' ; const getter = undefined ; const act = ( ) => new SystemUnderTest ( getter ) ; expect ( act ) . to . throw ( expectedError ) ; } ) ; } ) ; describe ( 'getApp' , ( ) => { it ( 'returns result from the getter' , async ( ) => { const expected = new ApplicationStub ( ) ; const getter = ( ) => expected ; const sut = new SystemUnderTest ( getter ) ; const actual = await Promise . all ( [ sut . getApp ( ) , sut . getApp ( ) , sut . getApp ( ) , sut . getApp ( ) , ] ) ; expect ( actual . every ( ( value ) => value === expected ) ) ; } ) ; it ( 'only executes getter once' , async ( ) => { let totalExecution = 0 ; const expected = new ApplicationStub ( ) ; const getter = ( ) => { totalExecution ++ ; return expected ; } ; const sut = new SystemUnderTest ( getter ) ; await Promise . all ( [ sut . getApp ( ) , sut . getApp ( ) , sut . getApp ( ) , sut . getApp ( ) , ] ) ; expect ( totalExecution ) . to . equal ( 1 ) ; } ) ; } ) ; } ) ; class SystemUnderTest extends ApplicationFactory { public constructor ( costlyGetter ) { super ( costlyGetter ) ; } }