Skip to content

Commit 8300338

Browse files
committed
refactor: use getConfig diractly in useParagonUrls
1 parent bc22970 commit 8300338

File tree

6 files changed

+20
-27
lines changed

6 files changed

+20
-27
lines changed

src/react/AppProvider.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export default function AppProvider({ store, children, wrapWithRouter }) {
6969
});
7070

7171
useTrackColorSchemeChoice();
72-
const [paragonThemeState, paragonThemeDispatch] = useParagonTheme(config);
72+
const [paragonThemeState, paragonThemeDispatch] = useParagonTheme();
7373

7474
const appContextValue = useMemo(() => ({
7575
authenticatedUser,

src/react/AppProvider.test.jsx

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,6 @@ describe('AppProvider', () => {
163163
it('calls useParagonTheme', () => {
164164
render(Component);
165165
expect(useParagonTheme).toHaveBeenCalled();
166-
expect(useParagonTheme).toHaveBeenCalledWith(
167-
expect.objectContaining({
168-
BASE_URL: 'localhost:8080',
169-
LMS_BASE_URL: 'localhost:18000',
170-
LOGIN_URL: 'localhost:18000/login',
171-
LOGOUT_URL: 'localhost:18000/logout',
172-
REFRESH_ACCESS_TOKEN_ENDPOINT: 'localhost:18000/oauth2/access_token',
173-
ACCESS_TOKEN_COOKIE_NAME: 'access_token',
174-
CSRF_TOKEN_API_PATH: 'localhost:18000/csrf',
175-
}),
176-
);
177166
});
178167

179168
it('blocks rendering until paragon theme is loaded', () => {

src/react/hooks/paragon/useParagonTheme.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,12 @@ export const getDefaultThemeVariant = ({ themeVariants, themeVariantDefaults = {
8989
* variant will already be loaded.
9090
*
9191
* @memberof module:React
92-
* @param {object} config An object containing the URLs for the theme's core CSS and any theme (i.e., `getConfig()`)
9392
*
9493
* @returns An array containing 2 elements: 1) an object containing the app
9594
* theme state, and 2) a dispatch function to mutate the app theme state.
9695
*/
97-
const useParagonTheme = (config) => {
98-
const paragonThemeUrls = useParagonThemeUrls(config);
96+
const useParagonTheme = () => {
97+
const paragonThemeUrls = useParagonThemeUrls();
9998
const {
10099
core: themeCore,
101100
defaults: themeVariantDefaults,

src/react/hooks/paragon/useParagonTheme.test.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { act, renderHook } from '@testing-library/react-hooks';
22

3-
import { getConfig } from '../../../config';
4-
53
import useParagonTheme from './useParagonTheme';
64

75
jest.mock('../../../config', () => ({
@@ -60,7 +58,7 @@ describe('useParagonTheme', () => {
6058
});
6159

6260
it('should configure theme variants according with system preference and add the change event listener', () => {
63-
const { result, unmount } = renderHook(() => useParagonTheme(getConfig()));
61+
const { result, unmount } = renderHook(() => useParagonTheme());
6462
const themeLinks = document.head.querySelectorAll('link');
6563
const darkLink = document.head.querySelector('link[data-paragon-theme-variant="dark"]');
6664
const lightLink = document.head.querySelector('link[data-paragon-theme-variant="light"]');
@@ -78,7 +76,7 @@ describe('useParagonTheme', () => {
7876

7977
it('should configure theme variants according with user preference if is defined (localStorage)', () => {
8078
window.localStorage.getItem.mockReturnValue('light');
81-
const { result, unmount } = renderHook(() => useParagonTheme(getConfig()));
79+
const { result, unmount } = renderHook(() => useParagonTheme());
8280
const themeLinks = document.head.querySelectorAll('link');
8381
const darkLink = document.head.querySelector('link[data-paragon-theme-variant="dark"]');
8482
const lightLink = document.head.querySelector('link[data-paragon-theme-variant="light"]');

src/react/hooks/paragon/useParagonThemeUrls.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useMemo } from 'react';
22

33
import { fallbackThemeUrl, isEmptyObject } from './utils';
4+
import { getConfig } from '../../../config';
45

56
export const handleVersionSubstitution = ({ url, wildcardKeyword, localVersion }) => {
67
if (!url || !url.includes(wildcardKeyword) || !localVersion) {
@@ -12,14 +13,14 @@ export const handleVersionSubstitution = ({ url, wildcardKeyword, localVersion }
1213
/**
1314
* Returns an object containing the URLs for the theme's core CSS and any theme variants.
1415
*
15-
* @param {*} config
1616
* @returns {ParagonThemeUrls|undefined} An object containing the URLs for the theme's core CSS and any theme variants.
1717
*/
18-
const useParagonThemeUrls = (config) => useMemo(() => {
19-
if (!config?.PARAGON_THEME_URLS) {
18+
const useParagonThemeUrls = () => useMemo(() => {
19+
const { PARAGON_THEME_URLS: paragonThemeUrls } = getConfig();
20+
if (!paragonThemeUrls || isEmptyObject(paragonThemeUrls)) {
2021
return undefined;
2122
}
22-
const paragonThemeUrls = config.PARAGON_THEME_URLS;
23+
2324
const paragonCoreCssUrl = typeof paragonThemeUrls?.core?.urls === 'object' ? paragonThemeUrls.core.urls.default : paragonThemeUrls?.core?.url;
2425
const brandCoreCssUrl = typeof paragonThemeUrls?.core?.urls === 'object' ? paragonThemeUrls.core.urls.brandOverride : undefined;
2526
const defaultThemeVariants = paragonThemeUrls.defaults;
@@ -94,6 +95,6 @@ const useParagonThemeUrls = (config) => useMemo(() => {
9495
defaults: defaultThemeVariants,
9596
variants: themeVariantsCss,
9697
};
97-
}, [config?.PARAGON_THEME_URLS]);
98+
}, []);
9899

99100
export default useParagonThemeUrls;

src/react/hooks/paragon/useParagonThemeUrls.test.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { renderHook } from '@testing-library/react-hooks';
22

33
import useParagonThemeUrls from './useParagonThemeUrls';
4+
import { mergeConfig } from '../../../config';
45

56
describe('useParagonThemeUrls', () => {
7+
beforeEach(() => { jest.resetAllMocks(); });
68
it.each([
79
undefined,
810
{},
9-
])('handles when `config.PARAGON_THEME_URLS` is not present', (config) => {
10-
const { result } = renderHook(() => useParagonThemeUrls(config));
11+
])('handles when `config.PARAGON_THEME_URLS` is not present', (paragonThemeUrls) => {
12+
mergeConfig({ PARAGON_THEME_URLS: paragonThemeUrls });
13+
const { result } = renderHook(() => useParagonThemeUrls());
1114
expect(result.current).toEqual(undefined);
1215
});
1316

@@ -28,6 +31,7 @@ describe('useParagonThemeUrls', () => {
2831
},
2932
},
3033
};
34+
mergeConfig(config);
3135
const { result } = renderHook(() => useParagonThemeUrls(config));
3236
expect(result.current).toEqual(
3337
expect.objectContaining({
@@ -74,6 +78,7 @@ describe('useParagonThemeUrls', () => {
7478
},
7579
},
7680
};
81+
mergeConfig(config);
7782
const { result } = renderHook(() => useParagonThemeUrls(config));
7883
expect(result.current).toEqual(
7984
expect.objectContaining({
@@ -111,7 +116,8 @@ describe('useParagonThemeUrls', () => {
111116
variants: {},
112117
},
113118
};
114-
const { result } = renderHook(() => useParagonThemeUrls(config));
119+
mergeConfig(config);
120+
const { result } = renderHook(() => useParagonThemeUrls());
115121
expect(result.current).toEqual(
116122
expect.objectContaining({
117123
core: {

0 commit comments

Comments
 (0)