11import React from 'react' ;
2+ import { logError } from '@edx/frontend-platform/logging' ;
23import { AppContext } from '@edx/frontend-platform/react' ;
34import { keyStore } from 'utils' ;
45import { RequestKeys } from 'data/constants/requests' ;
@@ -10,17 +11,24 @@ import * as apiHooks from './api';
1011
1112const reduxKeys = keyStore ( reduxHooks ) ;
1213
14+ jest . mock ( '@edx/frontend-platform/logging' , ( ) => ( {
15+ logError : jest . fn ( ) ,
16+ } ) ) ;
17+
1318jest . mock ( 'data/services/lms/utils' , ( ) => ( {
1419 post : jest . fn ( ( ...args ) => ( { post : args } ) ) ,
1520} ) ) ;
21+
1622jest . mock ( 'data/services/lms/api' , ( ) => ( {
1723 initializeList : jest . fn ( ) ,
1824 updateEntitlementEnrollment : jest . fn ( ) ,
1925 unenrollFromCourse : jest . fn ( ) ,
2026 deleteEntitlementEnrollment : jest . fn ( ) ,
2127 updateEmailSettings : jest . fn ( ) ,
2228 createCreditRequest : jest . fn ( ) ,
29+ getProgramsConfig : jest . fn ( ) ,
2330} ) ) ;
31+
2432jest . mock ( 'data/redux/hooks' , ( ) => ( {
2533 useCardCourseRunData : jest . fn ( ) ,
2634 useCardCreditData : jest . fn ( ) ,
@@ -110,6 +118,34 @@ describe('api hooks', () => {
110118 } ) ;
111119 } ) ;
112120
121+ describe ( 'useProgramsConfig' , ( ) => {
122+ let mockState ;
123+ const setState = jest . fn ( ( newState ) => { Object . assign ( mockState , newState ) ; } ) ;
124+ beforeEach ( ( ) => {
125+ mockState = { } ;
126+ React . useState . mockReturnValue ( [ mockState , setState ] ) ;
127+ } ) ;
128+
129+ it ( 'should return the programs configuration when the API call is successful' , async ( ) => {
130+ api . getProgramsConfig . mockResolvedValue ( { data : { enabled : true } } ) ;
131+ const config = apiHooks . useProgramsConfig ( ) ;
132+ const [ cb ] = React . useEffect . mock . calls [ 0 ] ;
133+ await cb ( ) ;
134+ expect ( setState ) . toHaveBeenCalled ( ) ;
135+ expect ( config ) . toEqual ( { enabled : true } ) ;
136+ } ) ;
137+
138+ it ( 'should return an empty object if the api call fails' , async ( ) => {
139+ mockState = { } ;
140+ api . getProgramsConfig . mockRejectedValue ( new Error ( 'error test' ) ) ;
141+ const config = apiHooks . useProgramsConfig ( ) ;
142+ const [ cb ] = React . useEffect . mock . calls [ 0 ] ;
143+ await cb ( ) ;
144+ expect ( config ) . toEqual ( { } ) ;
145+ expect ( logError ) . toHaveBeenCalled ( ) ;
146+ } ) ;
147+ } ) ;
148+
113149 describe ( 'entitlement enrollment hooks' , ( ) => {
114150 beforeEach ( ( ) => {
115151 jest . spyOn ( apiHooks , moduleKeys . useInitializeApp ) . mockReturnValue ( initializeApp ) ;
0 commit comments