Skip to content

Commit 04856f5

Browse files
committed
fix: logError and test implementation
1 parent 9dc0828 commit 04856f5

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/hooks/api.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22

3+
import { logError } from '@edx/frontend-platform/logging';
34
import { AppContext } from '@edx/frontend-platform/react';
45

56
import { RequestKeys } from 'data/constants/requests';
@@ -40,7 +41,7 @@ export const useProgramsConfig = () => {
4041
const { data } = await api.getProgramsConfig();
4142
setConfig(data);
4243
} catch (error) {
43-
console.error('Error accessing programs configuration', error);
44+
logError(`Error accessing programs configuration ${error}`);
4445
}
4546
};
4647

src/hooks/api.test.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import { logError } from '@edx/frontend-platform/logging';
23
import { AppContext } from '@edx/frontend-platform/react';
34
import { keyStore } from 'utils';
45
import { RequestKeys } from 'data/constants/requests';
@@ -10,6 +11,10 @@ import * as apiHooks from './api';
1011

1112
const reduxKeys = keyStore(reduxHooks);
1213

14+
jest.mock('@edx/frontend-platform/logging', () => ({
15+
logError: jest.fn(),
16+
}));
17+
1318
jest.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

Comments
 (0)