import { MockLog } from 'mock-jest-console' ; import { Action , Actor , IMap , Store } from '../index' ; class HelloActor extends Actor { defaultState ( ) { return { name : 'plume' } ; } @ Action ( 'change' ) change ( state ) { return state . set ( 'name' , 'plume++' ) ; } @ Action ( 'changeText' ) changeTexst ( state , text ) { return state . set ( 'name' , text ) ; } } class LoadingActor extends Actor { defaultState ( ) { return { loading : false } ; } @ Action ( 'change' ) change ( state ) { return state . set ( 'loading' , true ) ; } } class AppStore extends Store { bindActor ( ) { return [ HelloActor , LoadingActor ] ; } change = ( ) => { this . dispatch ( 'change' ) ; } ; changeTransation ( ) { this . transaction ( ( ) => { this . dispatch ( 'changeText' , 'iflux2' ) ; this . dispatch ( 'changeText' , 'plume' ) ; this . dispatch ( 'changeText' , 'plume2' ) ; } ) ; } } describe ( 'store test suite' , ( ) => { it ( 'default state' , ( ) => { const store = new AppStore ( ) as any ; expect ( store . state ( ) . toJS ( ) ) . toEqual ( { loading : false , name : 'plume' } ) ; expect ( store . _actors . length ) . toEqual ( 2 ) ; const actorState = store . _actorsState ; expect ( [ actorState [ 0 ] . toJS ( ) , actorState [ 1 ] . toJS ( ) ] ) . toEqual ( [ { name : 'plume' } , { loading : false } ] ) ; } ) ; it ( 'store dispatch' , ( ) => { const store = new AppStore ( ) as any ; store . change ( ) ; const storeState = store . state ( ) ; expect ( storeState . toJS ( ) ) . toEqual ( { loading : true , name : 'plume++' } ) ; const actorsState = store . _actorsState ; expect ( [ actorsState [ 0 ] . toJS ( ) , actorsState [ 1 ] . toJS ( ) ] ) . toEqual ( [ { name : 'plume++' } , { loading : true } ] ) ; } ) ; it ( 'store transation disptch' , ( ) => { const store = new AppStore ( ) ; store . changeTransation ( ) ; store . subscribe ( state => { expect ( state . toJS ( ) ) . toEqual ( { loading : false , name : 'plume2' } ) ; } ) ; } ) ; it ( 'store subscribe' , ( ) => { const store = new AppStore ( { } ) as any ; const _handleStoreChange = ( state ) => { expect ( store . state ( ) ) . toEqual ( { loading : true , name : 'plume++' } ) ; } ; store . subscribe ( _handleStoreChange ) ; expect ( store . _uiSubscribers . length ) . toEqual ( 1 ) ; store . unsubscribe ( _handleStoreChange ) ; expect ( store . _uiSubscribers . length ) . toEqual ( 0 ) ; } ) ; it ( 'dispatch false' , ( ) => { class LoadingActor extends Actor { defaultState ( ) { return { loading : true } ; } @ Action ( 'loading:end' ) end ( state , status ) { return state . set ( 'loading' , status ) ; } } class AppStore extends Store { bindActor ( ) { return [ LoadingActor ] ; } } const mock = new MockLog ( ) ; const store = new AppStore ( { debug : true } ) ; store . dispatch ( 'loading:end' , false ) ; expect ( store . state ( ) . toJS ( ) ) . toEqual ( { loading : false } ) ; expect ( mock . logs ) . toMatchSnapshot ( ) ; } ) ; } ) ;