import xs , { Stream , MemoryStream } from 'xstream' ; import { DevToolEnabledSource , FantasyObservable } from '@cycle/run' ; import { VNode } from 'snabbdom' ; import { DOMSource , EventsFnOptions } from './DOMSource' ; import { adapt } from '@cycle/run/lib/adapt' ; export type MockConfig = { [ name ] : FantasyObservable < any > | MockConfig ; } ; const SCOPE_PREFIX = '___' ; export class MockedDOMSource { private _elements : FantasyObservable < any > ; constructor ( private _mockConfig ) { if ( _mockConfig . elements ) { this . _elements = _mockConfig . elements as FantasyObservable < any > ; } else { this . _elements = adapt ( xs . empty ( ) ) ; } } public elements ( ) { const out = this . _elements ; out . _isCycleSource = 'MockedDOM' ; return out ; } public element ( ) { const output$ = this . elements ( ) . filter ( ( arr ) => arr . length > 0 ) . map ( ( arr ) => arr [ 0 ] ) . remember ( ) ; const out = adapt ( output$ ) ; out . _isCycleSource = 'MockedDOM' ; return out ; } public events ( eventType , options ? , bubbles ? ) { const streamForEventType = this . _mockConfig [ eventType ] as any ; const out = adapt ( streamForEventType || xs . empty ( ) ) ; out . _isCycleSource = 'MockedDOM' ; return out ; } public select ( selector ) { const mockConfigForSelector = this . _mockConfig [ selector ] || { } ; return new MockedDOMSource ( mockConfigForSelector as MockConfig ) ; } public isolateSource ( source , scope ) { return source . select ( '.' + SCOPE_PREFIX + scope ) ; } public isolateSink ( sink , scope ) { return adapt ( xs . fromObservable < any > ( sink ) . map ( ( vnode ) => { if ( vnode . sel && vnode . sel . indexOf ( SCOPE_PREFIX + scope ) !== - 1 ) { return vnode ; } else { vnode . sel += `.${ SCOPE_PREFIX }${ scope }` ; return vnode ; } } ) ) ; } } export function mockDOMSource ( mockConfig ) { return new MockedDOMSource ( mockConfig ) ; }