import { AdaptorInstance , Adaptor } from '../../adaptor' ; import { Container } from '../../container' ; import { applyDiff } from '../../utils/diff' ; export class TestingAdaptorInstance extends AdaptorInstance { public data : any = { } ; public setData = ( val ) => { } ; private shouldManuallyResolveUpdateCallback = false ; private updateCallbacks : Array < string > = [ ] ; private updateCallbackMap : { [ key ] : ( ) => void ; } = { } ; private checkUpdateDataIsPlainObject ( data ) { const pured = JSON . parse ( JSON . stringify ( data ) ) ; expect ( data ) . toEqual ( pured ) ; } public updateData ( data , callback , renderId ) { this . checkUpdateDataIsPlainObject ( data ) ; this . autoResolveUpdateCallback ( ) ; this . updateCallbacks . push ( renderId ) ; this . updateCallbackMap [ renderId ] = callback ; if ( Object . keys ( data ) . length === 0 ) { this . autoResolveUpdateCallback ( ) ; } else { this . data = applyDiff ( this . data , data ) ; this . setData ( data ) ; } } public registerEventHandling ( ) { } public unregisterEventHandler ( ) { } private autoResolveUpdateCallback ( ) { if ( ! this . shouldManuallyResolveUpdateCallback ) { this . resolveUpdateCallback ( ) ; } } public resolveUpdateCallback ( renderId ? ) { if ( ! renderId ) { if ( this . updateCallbacks . length > 0 ) { const id = this . updateCallbacks . shift ( ) ; const updateCallback = this . updateCallbackMap [ id ! ] ; delete this . updateCallbackMap [ id ! ] ; updateCallback ( ) ; return true ; } return false ; } const updateCallback = this . updateCallbackMap [ renderId ] ; if ( updateCallback ) { delete this . updateCallbackMap [ renderId ] ; this . updateCallbacks = this . updateCallbacks . filter ( val => val !== renderId ) ; updateCallback ( ) ; return true ; } return false ; } public setManuallyResolvedUpdateCallback ( enabled ) { this . shouldManuallyResolveUpdateCallback = enabled ; } } export class TestingAdaptor extends Adaptor { public run ( element ) { const testingAdaptorInstance = new TestingAdaptorInstance ( ) ; const container = new Container ( testingAdaptorInstance ) ; container . render ( element ) ; return testingAdaptorInstance ; } }