Skip to content

Commit

Permalink
fix: logError and test implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoa committed Oct 29, 2024
1 parent 9dc0828 commit 04856f5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/hooks/api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';

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

import { RequestKeys } from 'data/constants/requests';
Expand Down Expand Up @@ -40,7 +41,7 @@ export const useProgramsConfig = () => {
const { data } = await api.getProgramsConfig();
setConfig(data);
} catch (error) {
console.error('Error accessing programs configuration', error);
logError(`Error accessing programs configuration ${error}`);
}
};

Expand Down
20 changes: 14 additions & 6 deletions src/hooks/api.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import { logError } from '@edx/frontend-platform/logging';
import { AppContext } from '@edx/frontend-platform/react';
import { keyStore } from 'utils';
import { RequestKeys } from 'data/constants/requests';
Expand All @@ -10,6 +11,10 @@ import * as apiHooks from './api';

const reduxKeys = keyStore(reduxHooks);

jest.mock('@edx/frontend-platform/logging', () => ({
logError: jest.fn(),
}));

jest.mock('data/services/lms/utils', () => ({
post: jest.fn((...args) => ({ post: args })),
}));
Expand Down Expand Up @@ -114,9 +119,13 @@ describe('api hooks', () => {
});

describe('useProgramsConfig', () => {
const mockState = {};
let mockState;
const setState = jest.fn((newState) => { Object.assign(mockState, newState); });
React.useState.mockReturnValue([mockState, setState]);
beforeEach(() => {
mockState = {};
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();
Expand All @@ -126,15 +135,14 @@ describe('api hooks', () => {
expect(config).toEqual({ enabled: true });
});

it.only('should return an empty object if the api call fails', async () => {
it('should return an empty object if the api call fails', async () => {
mockState = {};
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();
expect(logError).toHaveBeenCalled();
});
});

Expand Down

0 comments on commit 04856f5

Please sign in to comment.