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,6 +11,10 @@ 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} ) ) ;
@@ -114,9 +119,13 @@ describe('api hooks', () => {
114119 } ) ;
115120
116121 describe ( 'useProgramsConfig' , ( ) => {
117- const mockState = { } ;
122+ let mockState ;
118123 const setState = jest . fn ( ( newState ) => { Object . assign ( mockState , newState ) ; } ) ;
119- React . useState . mockReturnValue ( [ mockState , setState ] ) ;
124+ beforeEach ( ( ) => {
125+ mockState = { } ;
126+ React . useState . mockReturnValue ( [ mockState , setState ] ) ;
127+ } ) ;
128+
120129 it ( 'should return the programs configuration when the API call is successful' , async ( ) => {
121130 api . getProgramsConfig . mockResolvedValue ( { data : { enabled : true } } ) ;
122131 const config = apiHooks . useProgramsConfig ( ) ;
@@ -126,15 +135,14 @@ describe('api hooks', () => {
126135 expect ( config ) . toEqual ( { enabled : true } ) ;
127136 } ) ;
128137
129- it . only ( 'should return an empty object if the api call fails' , async ( ) => {
138+ it ( 'should return an empty object if the api call fails' , async ( ) => {
139+ mockState = { } ;
130140 api . getProgramsConfig . mockRejectedValue ( new Error ( 'error test' ) ) ;
131- const consoleSpy = jest . spyOn ( global . console , 'error' ) . mockImplementation ( ( ) => { } ) ;
132141 const config = apiHooks . useProgramsConfig ( ) ;
133142 const [ cb ] = React . useEffect . mock . calls [ 0 ] ;
134143 await cb ( ) ;
135-
136144 expect ( config ) . toEqual ( { } ) ;
137- expect ( consoleSpy ) . toHaveBeenCalled ( ) ;
145+ expect ( logError ) . toHaveBeenCalled ( ) ;
138146 } ) ;
139147 } ) ;
140148
0 commit comments