import { useAction , useBinaryAction , useObjectKeyAction , useUnaryAction } from "../src/hooks" ; import { Action } from "../src/reducer" ; /**
 * Using real useModuleAction in Jest environment will error, because the hooks are not called in a React component context.
 */ jest . mock ( "../src/hooks" , ( ) => ( { useAction : ( ) => ( ) => { } , useUnaryAction : ( ) => ( ) => { } , useBinaryAction : ( ) => ( ) => { } , useObjectKeyAction : ( ) => ( ) => { } } ) ) ; type ActionCreator < P extends any [ ] > = ( ... args : P ) => Action < P > ; describe ( "useAction(type test)" , ( ) => { test ( "Should accept ActionCreator with only primitive dependency" , ( ) => { const allPrimitiveActionCreator = ( a , b , c ) => ( { type : "test" , payload : [ a , b , c ] } ) ; const allCurry = useAction ( allPrimitiveActionCreator , 1 , "" , false ) ; const expectAllCurryToPass = allCurry ( ) ; } ) ; test ( "Should reject ActionCreators with object as deps" , ( ) => { const updateAction = ( id , data ) => ( { type : "test" , payload : [ id , data ] } ) ; const updateDataWithObject = useAction ( updateAction , "id" , { value : 2 } ) ; } ) ; test ( "type union test" , ( ) => { const createTabChangeAction = ( tab ) => ( { type : "String Union test" , payload : [ tab ] } ) ; const changeToA = useAction ( createTabChangeAction , "a" ) ; const changeToB = useAction ( createTabChangeAction , "b" ) ; const changeToC = useAction ( createTabChangeAction , "c" ) ; const expectPassA = changeToA ( ) ; const expectPassB = changeToB ( ) ; const expectPassC = changeToC ( ) ; const expectToFail1 = useAction ( createTabChangeAction , "not valid" ) ; const expectToFail2 = useAction ( createTabChangeAction , null ) ; const shouldNotHaveParam = changeToA ( 1 ) ; } ) ; test ( "String literal union with multiple param" , ( ) => { const createTabChangeAction = ( tab , data ) => ( { type : "String Union test" , payload : [ tab , data ] } ) ; const changeToA = useUnaryAction ( createTabChangeAction , "a" ) ; const changeToB = useUnaryAction ( createTabChangeAction , "b" ) ; const changeToC = useUnaryAction ( createTabChangeAction , "c" ) ; const changeToNull = useUnaryAction ( createTabChangeAction , null ) ; const changeToUndefined = useUnaryAction ( createTabChangeAction , undefined ) ; const changeTo100 = useUnaryAction ( createTabChangeAction , 100 ) ; const expectChangeToAToPass = changeToA ( { data : "test" } ) ; const expectToFail1 = changeToA ( ) ; const expectToFail2 = changeToA ( "" ) ; const expectToFail3 = changeToA ( "a" , "" ) ; } ) ; } ) ; describe ( "useUnaryAction(type test)" , ( ) => { test ( "Misuse no-arg action" , ( ) => { const noArgAction = ( ) => ( { type : "test" , payload : [ ] } ) ; const curried = useUnaryAction ( noArgAction ) ; } ) ; test ( "single-arg action" , ( ) => { const noArgAction = ( ) => ( { type : "test" , payload : [ "" ] } ) ; const curried = useUnaryAction ( noArgAction ) ; } ) ; test ( "Should curry id" , ( ) => { const updateAction = ( id , data ) => ( { type : "test" , payload : [ id , data ] } ) ; const updateObjectWithId = useUnaryAction ( updateAction , "id" ) ; updateObjectWithId ( { value : 1 } ) ; updateObjectWithId ( { value : "s" } ) ; } ) ; test ( "Cannot use object as dependency" , ( ) => { const updateAction = ( id , data , number ) => ( { type : "test" , payload : [ id , data , number ] } ) ; const cannotUseObjectAsDeps = useUnaryAction ( updateAction , "" , { value : 1 } ) ; } ) ; test ( "String literal union with multiple param" , ( ) => { const createTabChangeAction = ( tab , data ) => ( { type : "String Union test" , payload : [ tab , data ] } ) ; const changeToA = useUnaryAction ( createTabChangeAction , "a" ) ; const changeToB = useUnaryAction ( createTabChangeAction , "b" ) ; const changeToC = useUnaryAction ( createTabChangeAction , "c" ) ; const changeToNull = useUnaryAction ( createTabChangeAction , null ) ; const changeToUndefined = useUnaryAction ( createTabChangeAction , undefined ) ; const changeTo100 = useUnaryAction ( createTabChangeAction , 100 ) ; const expectChangeToAToPass = changeToA ( { data : "test" } ) ; const expectToFail1 = changeToA ( ) ; const expectToFail2 = changeToA ( "" ) ; } ) ; test ( "Curry with type union args" , ( ) => { const createTabChangeAction = ( id , tabId ) => ( { type : "String Union test" , payload : [ id , tabId ] } ) ; const changeTab = useUnaryAction ( createTabChangeAction , "test" ) ; changeTab ( "a" ) ; changeTab ( "d" ) ; changeTab ( { } ) ; } ) ; } ) ; describe ( "useBinaryAction(type test)" , ( ) => { test ( "Curry two params" , ( ) => { const updateAction = ( id , data ) => ( { type : "test" , payload : [ id , data ] } ) ; const updateObjectWithId = useBinaryAction ( updateAction ) ; updateObjectWithId ( "54" , 50 ) ; const updateObjectUnaryParam = useBinaryAction ( updateAction , "10" ) ; const updateObjectBinaryParam = useBinaryAction ( updateAction , "10" , 5 ) ; updateObjectWithId ( "5" , "100" ) ; updateObjectWithId ( 5 , "10" ) ; updateObjectWithId ( 5 , 100 ) ; updateObjectWithId ( "5" , { value : "5" } ) ; } ) ; test ( "Curry union type params" , ( ) => { const action = ( tab , data ) => ( { type : "String Union test" , payload : [ tab , data ] } ) ; const update = useBinaryAction ( action ) ; update ( "a" , { data : "100" } ) ; update ( null , { data : "100" } ) ; update ( 100 , { data : "100" } ) ; const updateObject1 = useBinaryAction ( action , 100 ) ; const updateObject2 = useBinaryAction ( action , "a" ) ; update ( "d" ) ; update ( "a" , { data : 5 } ) ; update ( "d" , { data : "100" } ) ; } ) ; test ( "Curry union null dep type params" , ( ) => { const action = ( tab , data ) => ( { type : "String Union test" , payload : [ tab , data , "more-data" ] } ) ; const update = useBinaryAction ( action , null ) ; update ( { data : "100" } , "moreData" ) ; const updateA = useBinaryAction ( action , "a" ) ; updateA ( { data : "100" } , "moreData" ) ; const updateB = useBinaryAction ( action , "b" ) ; updateB ( { data : "100" } , "moreData" ) ; const updateObject1 = useBinaryAction ( action , 100 ) ; const updateObject2 = useBinaryAction ( action , "a" ) ; updateObject1 ( { data : "good data" } , "more-data" ) ; updateObject1 ( "d" ) ; updateObject2 ( "a" , { data : 5 } ) ; updateObject2 ( "d" , { data : "100" } ) ; } ) ; test ( "Curry union type union arg test" , ( ) => { const action = ( moreData , tab , data ) => ( { type : "String Union test" , payload : [ moreData , tab , data ] } ) ; const update = useBinaryAction ( action , "null" ) ; update ( "a" , { data : "payload" } ) ; update ( "b" , { data : "payload" } ) ; update ( "c" , { data : "payload" } ) ; update ( null , { data : "payload" } ) ; update ( undefined , { data : "payload" } ) ; update ( 100 , { data : "payload" } ) ; } ) ; test ( "Misuse no-arg action" , ( ) => { const noArgAction = ( ) => ( { type : "test" , payload : [ ] } ) ; useBinaryAction ( noArgAction ) ; } ) ; test ( "Misuse 1-arg action" , ( ) => { const oneArgAction = ( id ) => ( { type : "test" , payload : [ id ] } ) ; useBinaryAction ( oneArgAction , 1 ) ; useBinaryAction ( oneArgAction ) ; } ) ; } ) ; describe ( "useModuleObjectKeyAction(type test)" , ( ) => { test ( "Should accept key of object" , ( ) => { const updateObjectAction = ( object ) => ( { type : "update object" , payload : [ object ] } ) ; const updateA = useObjectKeyAction ( updateObjectAction , "a" ) ; const updateB = useObjectKeyAction ( updateObjectAction , "b" ) ; const updateC = useObjectKeyAction ( updateObjectAction , "c" ) ; const updateD = useObjectKeyAction ( updateObjectAction , "d" ) ; const updateNotKey = useObjectKeyAction ( updateObjectAction , "NOT KEY" ) ; updateA ( "string" ) ; updateB ( 1 ) ; updateC ( false ) ; updateD ( null ) ; updateD ( "a" ) ; updateD ( "b" ) ; updateA ( 1 ) ; updateB ( "s" ) ; updateC ( 3 ) ; updateD ( "e" ) ; updateD ( undefined ) ; } ) ; } ) ;