@@ -13,14 +13,17 @@ const reduxKeys = keyStore(reduxHooks);
1313jest . mock ( 'data/services/lms/utils' , ( ) => ( {
1414 post : jest . fn ( ( ...args ) => ( { post : args } ) ) ,
1515} ) ) ;
16+
1617jest . mock ( 'data/services/lms/api' , ( ) => ( {
1718 initializeList : jest . fn ( ) ,
1819 updateEntitlementEnrollment : jest . fn ( ) ,
1920 unenrollFromCourse : jest . fn ( ) ,
2021 deleteEntitlementEnrollment : jest . fn ( ) ,
2122 updateEmailSettings : jest . fn ( ) ,
2223 createCreditRequest : jest . fn ( ) ,
24+ getProgramsConfig : jest . fn ( ) ,
2325} ) ) ;
26+
2427jest . mock ( 'data/redux/hooks' , ( ) => ( {
2528 useCardCourseRunData : jest . fn ( ) ,
2629 useCardCreditData : jest . fn ( ) ,
@@ -110,6 +113,31 @@ describe('api hooks', () => {
110113 } ) ;
111114 } ) ;
112115
116+ describe ( 'useProgramsConfig' , ( ) => {
117+ const mockState = { } ;
118+ const setState = jest . fn ( ( newState ) => { Object . assign ( mockState , newState ) ; } ) ;
119+ React . useState . mockReturnValue ( [ mockState , setState ] ) ;
120+ it ( 'should return the programs configuration when the API call is successful' , async ( ) => {
121+ api . getProgramsConfig . mockResolvedValue ( { data : { enabled : true } } ) ;
122+ const config = apiHooks . useProgramsConfig ( ) ;
123+ const [ cb ] = React . useEffect . mock . calls [ 0 ] ;
124+ await cb ( ) ;
125+ expect ( setState ) . toHaveBeenCalled ( ) ;
126+ expect ( config ) . toEqual ( { enabled : true } ) ;
127+ } ) ;
128+
129+ it . only ( 'should return an empty object if the api call fails' , async ( ) => {
130+ api . getProgramsConfig . mockRejectedValue ( new Error ( 'error test' ) ) ;
131+ const consoleSpy = jest . spyOn ( global . console , 'error' ) . mockImplementation ( ( ) => { } ) ;
132+ const config = apiHooks . useProgramsConfig ( ) ;
133+ const [ cb ] = React . useEffect . mock . calls [ 0 ] ;
134+ await cb ( ) ;
135+
136+ expect ( config ) . toEqual ( { } ) ;
137+ expect ( consoleSpy ) . toHaveBeenCalled ( ) ;
138+ } ) ;
139+ } ) ;
140+
113141 describe ( 'entitlement enrollment hooks' , ( ) => {
114142 beforeEach ( ( ) => {
115143 jest . spyOn ( apiHooks , moduleKeys . useInitializeApp ) . mockReturnValue ( initializeApp ) ;
0 commit comments