import { should } from 'chai' ; should ( ) ; import { Source } from '../../pin/source' ; import { Switch } from '../switch' ; describe ( 'Switch' , ( ) => { it ( 'should activate output based on which given `case` matched the given `value`.' , ( ) => { let a = new Source ( ) ; let s = new Switch ( 1 , 'hellow' , false ) ; s . target . from ( a ) ; let res = '' ; s . case ( 0 ) . subscribe ( ( ) => res = 'A' ) ; s . case ( 1 ) . subscribe ( ( ) => res = 'B' ) ; s . case ( 2 ) . subscribe ( ( ) => res = 'C' ) ; a . send ( 'hellow' ) ; res . should . equal ( 'B' ) ; a . send ( false ) ; res . should . equal ( 'C' ) ; } ) ; it ( 'should check with `cases` that are functions by calling them on the given data.' , ( ) => { let a = new Source ( ) ; let b = new Switch ( ( n ) => n % 2 == 0 , ( n ) => n % 2 == 1 ) ; b . target . from ( a ) ; let res = '' ; b . case ( 0 ) . subscribe ( ( ) => res = 'even' ) ; b . case ( 1 ) . subscribe ( ( ) => res = 'odd' ) ; a . send ( 2 ) ; res . should . equal ( 'even' ) ; a . send ( 3 ) ; res . should . equal ( 'odd' ) ; } ) ; it ( 'should work with async functions in `cases` as well.' , done => { let a = new Source ( ) ; let b = new Switch ( ( n ) => n % 2 == 0 , ( n , done ) => { setTimeout ( ( ) => done ( n % 2 == 1 ) , 5 ) ; } ) ; b . target . from ( a ) ; let res = '' ; b . case ( 0 ) . subscribe ( ( ) => res = 'even' ) ; b . case ( 1 ) . subscribe ( val => { val . should . equal ( 3 ) ; res . should . not . equal ( 'even' ) ; done ( ) ; } ) ; a . send ( 3 ) ; } ) ; it ( 'should hanlde errors thrown by sync case functions.' , done => { let s = new Switch ( ( ) => { throw new Error ( 'well ...' ) ; } ) ; let a = new Source ( ) ; a . to ( s . target ) ; s . case ( 0 ) . subscribe ( ( ) => { } , ( ) => done ( ) ) ; a . send ( ) ; } ) ; it ( 'should allow async case functions to throw errors through the second callback passed to them.' , done => { let s = new Switch ( ( _ , __ , err ) => err ( new Error ( ) ) ) ; let a = new Source ( ) ; a . to ( s . target ) ; s . case ( 0 ) . subscribe ( ( ) => { } , ( ) => done ( ) ) ; a . send ( ) ; } ) ; it ( 'should activate as many outputs as they match.' , ( ) => { let a = new Source ( ) ; let b = new Switch ( 1 , ( ) => true ) ; let calls = 0 ; b . target . from ( a ) ; b . case ( 0 ) . subscribe ( ( ) => calls ++ ) ; b . case ( 1 ) . subscribe ( ( ) => calls ++ ) ; a . send ( 1 ) ; calls . should . equal ( 2 ) ; } ) ; describe ( '.target' , ( ) => { it ( 'should be equal to `.in("target")`' , ( ) => { let s = new Switch ( ) ; s . target . should . equal ( s . in ( 'target' ) ) ; } ) ; } ) ; describe ( '.case()' , ( ) => { it ( 'should be equal to `.out(<n>)`' , ( ) => { let s = new Switch ( true ) ; s . case ( 0 ) . should . equal ( s . out ( 0 ) ) ; } ) ; } ) ; } ) ;