From 2253151cd39ab803dc1b242cf99cfb2655fd4a5f Mon Sep 17 00:00:00 2001 From: Diana Olarte Date: Fri, 25 Oct 2024 09:29:01 +1100 Subject: [PATCH] test: add testing for useProgramsn config api hook --- src/hooks/api.test.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/hooks/api.test.js b/src/hooks/api.test.js index cd1a5b6b..33c781ed 100644 --- a/src/hooks/api.test.js +++ b/src/hooks/api.test.js @@ -13,6 +13,7 @@ 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(), @@ -20,7 +21,9 @@ jest.mock('data/services/lms/api', () => ({ deleteEntitlementEnrollment: jest.fn(), updateEmailSettings: jest.fn(), createCreditRequest: jest.fn(), + getProgramsConfig: jest.fn(), })); + jest.mock('data/redux/hooks', () => ({ useCardCourseRunData: jest.fn(), useCardCreditData: jest.fn(), @@ -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);