import * as chai from "chai" ; import * as chaiAsPromised from "chai-as-promised" ; import { AbandonedError , OneAtATimeAction } from "../OneAtATimeAction" ; import { BeDuration } from "../Time" ; const assert = chai . assert ; const expect = chai . expect ; chai . use ( chaiAsPromised ) ; describe ( "OneAtATime test" , ( ) => { it ( "OneAtATime" , async ( ) => { let calls = 0 ; const operation = new OneAtATimeAction ( async ( a , b ) => { if ( a === 10 ) throw new Error ( "cancelled" ) ; assert . equal ( a , 200 ) ; assert . equal ( b , "hello" ) ; await BeDuration . wait ( 100 ) ; return ++ calls ; } , "testAbandon" ) ; expect ( operation . request ( 200 , "hello" ) ) . to . be . eventually . fulfilled ; expect ( operation . request ( 200 , "hello" ) ) . to . be . rejectedWith ( AbandonedError ) ; expect ( operation . request ( 200 , "hello" ) ) . to . be . rejectedWith ( AbandonedError ) ; let count = await operation . request ( 200 , "hello" ) ; assert . equal ( count , 2 ) ; expect ( operation . request ( 10 , "hello" ) ) . to . be . rejectedWith ( AbandonedError , "cancelled" ) ; expect ( operation . request ( 200 , "hello" ) ) . to . be . rejectedWith ( AbandonedError , "testAbandon" ) ; expect ( operation . request ( 200 , "hello" ) ) . to . be . rejectedWith ( AbandonedError , "testAbandon" ) ; count = await operation . request ( 200 , "hello" ) ; assert . equal ( count , 3 ) ; } ) ; } ) ;