Skip to content

Commit

Permalink
fix: ensure learner portal works without curation config (#677)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamstankiewicz authored Jan 30, 2023
1 parent 4ab9185 commit 3c465c9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
9 changes: 4 additions & 5 deletions src/components/enterprise-banner/EnterpriseBanner.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ const EnterpriseBanner = () => {
const { enterpriseConfig } = useContext(AppContext);
const { enterpriseConfig: { uuid: enterpriseUUID } } = useContext(AppContext);
const isSearchPage = `/${ enterpriseConfig.slug }/search` === location.pathname;
const {
enterpriseCuration: {
canOnlyViewHighlightSets,
},
} = useEnterpriseCuration(enterpriseUUID);

const { enterpriseCuration } = useEnterpriseCuration(enterpriseUUID);
const { canOnlyViewHighlightSets } = enterpriseCuration;

return (
<div className="enterprise-banner bg-brand-secondary border-brand-tertiary">
<Container size="lg">
Expand Down
3 changes: 2 additions & 1 deletion src/components/search/content-highlights/data/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export const useEnterpriseCuration = (enterpriseUUID) => {
setIsLoading(true);
const response = await getEnterpriseCuration(enterpriseUUID);
const results = camelCaseObject(response.data.results);
const enterpriseCurationConfig = results[0];
// if no enterprise curation config is found, fallback to an empty object to match the initial state.
const enterpriseCurationConfig = results[0] || { canOnlyViewHighlightSets: false };
setEnterpriseCuration(enterpriseCurationConfig);
} catch (err) {
logError(err);
Expand Down
25 changes: 15 additions & 10 deletions src/components/search/content-highlights/data/tests/hooks.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { logError } from '@edx/frontend-platform/logging';
import { camelCaseObject } from '@edx/frontend-platform/utils';
import { getConfig } from '@edx/frontend-platform/config';
import { renderHook } from '@testing-library/react-hooks';
import {
Expand Down Expand Up @@ -27,9 +26,6 @@ jest.mock('../utils', () => ({
jest.mock('@edx/frontend-platform/logging', () => ({
logError: jest.fn(),
}));
jest.mock('@edx/frontend-platform/utils', () => ({
camelCaseObject: jest.fn(() => []),
}));
jest.mock('@edx/frontend-platform/config', () => ({
getConfig: jest.fn(() => ({ FEATURE_CONTENT_HIGHLIGHTS: true })),
}));
Expand All @@ -40,7 +36,6 @@ describe('useContentHighlights', () => {
const { result, waitForNextUpdate } = renderHook(() => useContentHighlights(enterpriseUUID));
await waitForNextUpdate();
expect(getContentHighlights).toHaveBeenCalledWith(enterpriseUUID);
expect(camelCaseObject).toHaveBeenCalled();
expect(result.current.isLoading).toBe(false);
expect(result.current.contentHighlights).toEqual([]);
expect(result.current.fetchError).toBeUndefined();
Expand Down Expand Up @@ -98,18 +93,24 @@ describe('useEnterpriseCuration', () => {
jest.clearAllMocks();
});

it('should fetch enterprise curation and set it to state', async () => {
it.each([
{ hasResults: true, hasFallbackCuration: false },
{ hasResults: false, hasFallbackCuration: true },
])('should fetch enterprise curation and set it to state', async ({ hasResults, hasFallbackCuration }) => {
const enterpriseUUID = '123';
const enterpriseCuration = {
id: '123',
name: 'Test Enterprise',
canOnlyViewHighlightSets: false,
};
const fallbackEnterpriseCuration = {
canOnlyViewHighlightSets: false,
};
getEnterpriseCuration.mockResolvedValue({
data: {
results: [enterpriseCuration],
results: hasResults ? [enterpriseCuration] : [],
},
});
camelCaseObject.mockReturnValue([enterpriseCuration]);
getConfig.mockReturnValue({ FEATURE_CONTENT_HIGHLIGHTS: true });

const { result, waitForNextUpdate } = renderHook(() => useEnterpriseCuration(enterpriseUUID));
Expand All @@ -120,9 +121,13 @@ describe('useEnterpriseCuration', () => {
await waitForNextUpdate();

expect(getEnterpriseCuration).toHaveBeenCalledWith(enterpriseUUID);
expect(camelCaseObject).toHaveBeenCalledWith([enterpriseCuration]);
expect(result.current.isLoading).toBe(false);
expect(result.current.enterpriseCuration).toEqual(enterpriseCuration);

if (hasFallbackCuration) {
expect(result.current.enterpriseCuration).toEqual(fallbackEnterpriseCuration);
} else {
expect(result.current.enterpriseCuration).toEqual(enterpriseCuration);
}
});

it('should handle fetch errors and set error to state', async () => {
Expand Down

0 comments on commit 3c465c9

Please sign in to comment.