Skip to content

Commit

Permalink
test: add testing for useProgramsn config api hook
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoa committed Oct 31, 2024
1 parent b2e017c commit 88c047c
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/hooks/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ const reduxKeys = keyStore(reduxHooks);
jest.mock('data/services/lms/utils', () => ({
post: jest.fn((...args) => ({ post: args })),
}));

jest.mock('data/services/lms/api', () => ({
initializeList: jest.fn(),
updateEntitlementEnrollment: jest.fn(),
unenrollFromCourse: jest.fn(),
deleteEntitlementEnrollment: jest.fn(),
updateEmailSettings: jest.fn(),
createCreditRequest: jest.fn(),
getProgramsConfig: jest.fn(),
}));

jest.mock('data/redux/hooks', () => ({
useCardCourseRunData: jest.fn(),
useCardCreditData: jest.fn(),
Expand Down Expand Up @@ -110,6 +113,31 @@ describe('api hooks', () => {
});
});

describe('useProgramsConfig', () => {
const mockState = {};
const setState = jest.fn((newState) => { Object.assign(mockState, newState); });
React.useState.mockReturnValue([mockState, setState]);
it('should return the programs configuration when the API call is successful', async () => {
api.getProgramsConfig.mockResolvedValue({ data: { enabled: true } });
const config = apiHooks.useProgramsConfig();
const [cb] = React.useEffect.mock.calls[0];
await cb();
expect(setState).toHaveBeenCalled();
expect(config).toEqual({ enabled: true });
});

it.only('should return an empty object if the api call fails', async () => {
api.getProgramsConfig.mockRejectedValue(new Error('error test'));
const consoleSpy = jest.spyOn(global.console, 'error').mockImplementation(() => { });
const config = apiHooks.useProgramsConfig();
const [cb] = React.useEffect.mock.calls[0];
await cb();

expect(config).toEqual({});
expect(consoleSpy).toHaveBeenCalled();
});
});

describe('entitlement enrollment hooks', () => {
beforeEach(() => {
jest.spyOn(apiHooks, moduleKeys.useInitializeApp).mockReturnValue(initializeApp);
Expand Down

0 comments on commit 88c047c

Please sign in to comment.