license import { Action , CompoundAction , Reducer , TypedAction , reduceCompoundActions , } from "../index" ; const SetMessage = TypedAction . define ( "test::set_message" ) < string > ( ) ; function replaceMessageReducer ( state , action ) { return SetMessage . is ( action ) ? action . payload : state ; } describe ( "reduceCompoundActions" , ( ) => { let delegate ; let reduce ; beforeAll ( ( ) => { delegate = jest . fn ( replaceMessageReducer ) ; reduce = reduceCompoundActions ( delegate ) ; } ) ; beforeEach ( ( ) => { delegate . mockClear ( ) ; } ) ; it ( "should delegate normal actions" , ( ) => { reduce ( "" , SetMessage . create ( "hello" ) ) ; expect ( delegate . mock . calls ) . toEqual ( [ [ "" , SetMessage . create ( "hello" ) ] ] ) ; } ) ; it ( "should reduce normal actions to delegate's value" , ( ) => { const result = reduce ( "" , SetMessage . create ( "hello" ) ) ; expect ( result ) . toEqual ( "hello" ) ; } ) ; it ( "should not delegate anything for empty compound actions" , ( ) => { reduce ( "" , CompoundAction . create ( [ ] ) ) ; expect ( delegate . mock . calls ) . toHaveLength ( 0 ) ; } ) ; it ( "should reduce empty compound actions to initial state" , ( ) => { const result = reduce ( "empty" , CompoundAction . create ( [ ] ) ) ; expect ( result ) . toEqual ( "empty" ) ; } ) ; it ( "should delegate a single wrapped action" , ( ) => { reduce ( "" , CompoundAction . create ( [ SetMessage . create ( "hello" ) ] ) ) ; expect ( delegate . mock . calls ) . toEqual ( [ [ "" , SetMessage . create ( "hello" ) ] ] ) ; } ) ; it ( "should reduce a single wrapped action to delegate's value" , ( ) => { const result = reduce ( "" , CompoundAction . create ( [ SetMessage . create ( "hello" ) ] ) ) ; expect ( result ) . toEqual ( "hello" ) ; } ) ; it ( "should delegate multiple wrapped actions in order" , ( ) => { reduce ( "" , CompoundAction . create ( [ SetMessage . create ( "one" ) , SetMessage . create ( "two" ) , SetMessage . create ( "three" ) , ] ) ) ; expect ( delegate . mock . calls ) . toEqual ( [ [ "" , SetMessage . create ( "one" ) ] , [ "one" , SetMessage . create ( "two" ) ] , [ "two" , SetMessage . create ( "three" ) ] , ] ) ; } ) ; it ( "should reduce multiple wrapped actions to final delegate value" , ( ) => { const result = reduce ( "" , CompoundAction . create ( [ SetMessage . create ( "one" ) , SetMessage . create ( "two" ) , SetMessage . create ( "three" ) , ] ) ) ; expect ( result ) . toEqual ( "three" ) ; } ) ; it ( "should delegate and unwrap deeply nested compound actions in order" , ( ) => { reduce ( "" , CompoundAction . create ( [ SetMessage . create ( "one" ) , CompoundAction . create ( [ SetMessage . create ( "two" ) , SetMessage . create ( "three" ) , ] ) , SetMessage . create ( "four" ) , ] ) ) ; expect ( delegate . mock . calls ) . toEqual ( [ [ "" , SetMessage . create ( "one" ) ] , [ "one" , SetMessage . create ( "two" ) ] , [ "two" , SetMessage . create ( "three" ) ] , [ "three" , SetMessage . create ( "four" ) ] , ] ) ; } ) ; it ( "should reduce deeply nested compound actions to final delegate value" , ( ) => { const result = reduce ( "" , CompoundAction . create ( [ SetMessage . create ( "one" ) , CompoundAction . create ( [ SetMessage . create ( "two" ) , SetMessage . create ( "three" ) , ] ) , SetMessage . create ( "four" ) , ] ) ) ; expect ( result ) . toEqual ( "four" ) ; } ) ; } ) ;