Skip to content

Commit 9dc0828

Browse files
committed
test: add testing for useProgramsn config api hook
1 parent add8967 commit 9dc0828

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/hooks/api.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@ const reduxKeys = keyStore(reduxHooks);
1313
jest.mock('data/services/lms/utils', () => ({
1414
post: jest.fn((...args) => ({ post: args })),
1515
}));
16+
1617
jest.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+
2427
jest.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

Comments
 (0)