import { IAttachment , IMessage } from 'botbuilder' ; import * as chai from 'chai' ; import { IExpectation } from './IExpectation' ; const chaiSubset = require ( 'chai-subset' ) ; chai . use ( chaiSubset ) ; const expect = chai . expect ; type DeepMatchReturn = { to : { deep : { match ( arg ) : { } ; } ; } ; } ; type DeepMatch = ( args : { } , errMsg ? : string ) => DeepMatchReturn ; const expectWithDeepMatch = expect as any ; /**
 * Expectations for chai based frameworks
 */ export class ChaiExpectation implements IExpectation { private subject : { } ; private message : string ; constructor ( subject , message ? ) { this . subject = subject ; this . message = message ; } public notToBeEmpty ( ) { expect ( this . subject , this . message ) . not . to . be . empty ; } public toEqual ( value ) { expect ( this . subject , this . message ) . to . equal ( value ) ; } public toInclude ( value ) { expect ( this . subject , this . message ) . to . include ( value ) ; } public toBeTrue ( ) { expect ( this . subject , this . message ) . to . be . true ; } param outgoingMessage public toDeeplyInclude ( outgoingMessage ) { const expectedResponseCollectionAsIMessage = this . subject as IMessage [ ] ; const expectWithDeepMatch = expect as any ; let matchExists = false ; expectedResponseCollectionAsIMessage . forEach ( ( expectedResponse ) => { const exp = expect ; if ( matchExists ) { return ; } const outgoingMessageClone = Object . assign ( { } , outgoingMessage ) ; const expectedResponseClone = Object . assign ( { } , expectedResponse ) ; const outgoingMessageAttachments = outgoingMessageClone . attachments || [ ] ; const expectedResponseAttachments = expectedResponseClone . attachments || [ ] ; outgoingMessageClone . attachments = [ ] ; expectedResponseClone . attachments = [ ] ; delete expectedResponseClone . source ; delete outgoingMessageClone . inputHint ; delete expectedResponseClone . agent ; try { expect ( outgoingMessageClone ) . to . containSubset ( expectedResponseClone ) ; matchExists = true ; if ( matchExists && expectedResponseAttachments . length ) { matchExists = this . checkForMatchingAttachments ( outgoingMessageAttachments , expectedResponseAttachments ) ; } } catch ( e ) { } } ) ; if ( ! matchExists ) { expect . fail ( null , null , `expected ${ JSON . stringify ( outgoingMessage ) }\
            to be a subset of one of ${ JSON . stringify ( expectedResponseCollectionAsIMessage , null , 2 ) }` ) ; } } private checkForMatchingAttachments ( outgoingAttachments , expectedAttachments ) { let matchExists = false ; outgoingAttachments . forEach ( ( outgoingAttachment ) => { if ( matchExists ) { return ; } expectedAttachments . forEach ( ( expectedAttachment ) => { if ( matchExists ) { return ; } try { expect ( outgoingAttachment ) . to . containSubset ( expectedAttachment ) ; matchExists = true ; } catch ( e ) { } } ) ; } ) ; return matchExists ; } }