Skip to content

Commit

Permalink
test: refactor testing fot app provider, theme core hook, theme varia…
Browse files Browse the repository at this point in the history
…nts hook
  • Loading branch information
dcoa committed May 28, 2024
1 parent b73bc06 commit 317b0c1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 106 deletions.
47 changes: 13 additions & 34 deletions src/react/AppProvider.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,24 +142,19 @@ describe('AppProvider', () => {
});

describe('paragon theme and brand', () => {
let Component = (
<AppProvider>
<div>Child One</div>
<div>Child Two</div>
</AppProvider>
);

it('calls trackColorSchemeChoice', () => {
const Component = (
<AppProvider>
<div>Child One</div>
<div>Child Two</div>
</AppProvider>
);
render(Component);
expect(useTrackColorSchemeChoice).toHaveBeenCalled();
});

it('calls useParagonTheme', () => {
const Component = (
<AppProvider>
<div>Child One</div>
<div>Child Two</div>
</AppProvider>
);
render(Component);
expect(useParagonTheme).toHaveBeenCalled();
expect(useParagonTheme).toHaveBeenCalledWith(
Expand All @@ -180,12 +175,6 @@ describe('AppProvider', () => {
{ isThemeLoaded: false },
jest.fn(),
]);
const Component = (
<AppProvider>
<div>Child One</div>
<div>Child Two</div>
</AppProvider>
);
const { container } = render(Component);
expect(container).toBeEmptyDOMElement();
});
Expand All @@ -196,7 +185,7 @@ describe('AppProvider', () => {
{ isThemeLoaded: true, themeVariant: 'light' },
mockUseParagonThemeDispatch,
]);
const Component = (
Component = (
<AppProvider>
<AppContext.Consumer>
{({ paragonTheme }) => (
Expand Down Expand Up @@ -235,12 +224,12 @@ describe('AppProvider', () => {
});

describe('useAppEvent', () => {
const Component = (
<AppProvider>
<div>Child</div>
</AppProvider>
);
it('subscribes to `AUTHENTICATED_USER_CHANGED`', async () => {
const Component = (
<AppProvider>
<div>Child</div>
</AppProvider>
);
render(Component);
expect(useAppEvent).toHaveBeenCalledWith(AUTHENTICATED_USER_CHANGED, expect.any(Function));
const useAppEventMockCalls = useAppEvent.mock.calls;
Expand All @@ -252,11 +241,6 @@ describe('AppProvider', () => {
});

it('subscribes to `CONFIG_CHANGED`', async () => {
const Component = (
<AppProvider>
<div>Child</div>
</AppProvider>
);
render(Component);
expect(useAppEvent).toHaveBeenCalledWith(CONFIG_CHANGED, expect.any(Function));
const useAppEventMockCalls = useAppEvent.mock.calls;
Expand All @@ -268,11 +252,6 @@ describe('AppProvider', () => {
});

it('subscribes to `LOCALE_CHANGED`', async () => {
const Component = (
<AppProvider>
<div>Child</div>
</AppProvider>
);
render(Component);
expect(useAppEvent).toHaveBeenCalledWith(LOCALE_CHANGED, expect.any(Function));
const useAppEventMockCalls = useAppEvent.mock.calls;
Expand Down
8 changes: 6 additions & 2 deletions src/react/hooks/paragon/useParagonTheme.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('useParagonTheme', () => {
});

it('should configure theme variants according with system preference and add the change event listener', () => {
const { unmount } = renderHook(() => useParagonTheme(getConfig()));
const { result, unmount } = renderHook(() => useParagonTheme(getConfig()));
const themeLinks = document.head.querySelectorAll('link');
const darkLink = document.head.querySelector('link[data-paragon-theme-variant="dark"]');
const lightLink = document.head.querySelector('link[data-paragon-theme-variant="light"]');
Expand All @@ -70,13 +70,15 @@ describe('useParagonTheme', () => {
expect(mockAddEventListener).toHaveBeenCalled();
expect(darkLink.rel).toBe('stylesheet');
expect(lightLink.rel).toBe('alternate stylesheet');
expect(result.current[0]).toEqual({ isThemeLoaded: true, themeVariant: 'dark' });

unmount();
expect(mockRemoveEventListener).toHaveBeenCalled();
});

it('should configure theme variants according with user preference if is defined (localStorage)', () => {
window.localStorage.getItem.mockReturnValue('light');
const { unmount } = renderHook(() => useParagonTheme(getConfig()));
const { result, unmount } = renderHook(() => useParagonTheme(getConfig()));
const themeLinks = document.head.querySelectorAll('link');
const darkLink = document.head.querySelector('link[data-paragon-theme-variant="dark"]');
const lightLink = document.head.querySelector('link[data-paragon-theme-variant="light"]');
Expand All @@ -87,6 +89,8 @@ describe('useParagonTheme', () => {

expect(darkLink.rel).toBe('alternate stylesheet');
expect(lightLink.rel).toBe('stylesheet');
expect(result.current[0]).toEqual({ isThemeLoaded: true, themeVariant: 'light' });

unmount();
expect(mockRemoveEventListener).toHaveBeenCalled();
});
Expand Down
63 changes: 11 additions & 52 deletions src/react/hooks/paragon/useParagonThemeCore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ jest.mock('../../../logging');

describe('useParagonThemeCore', () => {
const themeOnLoad = jest.fn();
let coreConfig;

afterEach(() => {
beforeEach(() => {
document.head.innerHTML = '';
jest.clearAllMocks();
});

it('should load the core url and change the loading state to true', () => {
const coreConfig = {
coreConfig = {
themeCore: {
urls: { default: 'https://cdn.jsdelivr.net/npm/@edx/paragon@$21.0.0/dist/core.min.css' },
urls: {
default: 'https://cdn.jsdelivr.net/npm/@edx/paragon@$21.0.0/dist/core.min.css',
},
},
onLoad: themeOnLoad,
};
jest.clearAllMocks();
});

it('should load the core url and change the loading state to true', () => {
renderHook(() => useParagonThemeCore(coreConfig));
const createdLinkTag = document.head.querySelector('link');
act(() => createdLinkTag.onload());
Expand All @@ -31,15 +33,7 @@ describe('useParagonThemeCore', () => {
});

it('should load the core default and brand url and change the loading state to true', () => {
const coreConfig = {
themeCore: {
urls: {
default: 'https://cdn.jsdelivr.net/npm/@edx/paragon@$21.0.0/dist/core.min.css',
brandOverride: 'https://cdn.jsdelivr.net/npm/@edx/brand@$2.0.0Version/dist/core.min.css',
},
},
onLoad: themeOnLoad,
};
coreConfig.themeCore.urls.brandOverride = 'https://cdn.jsdelivr.net/npm/@edx/brand@$2.0.0Version/dist/core.min.css';

renderHook(() => useParagonThemeCore(coreConfig));
const createdLinkTag = document.head.querySelector('link[data-paragon-theme-core="true"]');
Expand All @@ -52,33 +46,6 @@ describe('useParagonThemeCore', () => {
});

it('should dispatch a log error and fallback to PARAGON_THEME if can not load the core theme link', () => {
global.PARAGON_THEME = {
paragon: {
version: '1.0.0',
themeUrls: {
core: {
fileName: 'core.min.css',
},
defaults: {
light: 'light',
},
variants: {
light: {
fileName: 'light.min.css',
},
},
},
},
};
const coreConfig = {
themeCore: {
urls: {
default: 'https://cdn.jsdelivr.net/npm/@edx/paragon@$21.0.0/dist/core.min.css',
},
},
onLoad: themeOnLoad,
};

renderHook(() => useParagonThemeCore(coreConfig));
const createdLinkTag = document.head.querySelector('link[data-paragon-theme-core="true"]');

Expand All @@ -90,14 +57,6 @@ describe('useParagonThemeCore', () => {

it('should not create a new link if the core theme is already loaded', () => {
document.head.innerHTML = '<link rel="preload" as="style" href="https://cdn.jsdelivr.net/npm/@edx/paragon@$21.0.0/dist/core.min.css" onerror="this.remove();">';
const coreConfig = {
themeCore: {
urls: {
default: 'https://cdn.jsdelivr.net/npm/@edx/paragon@$21.0.0/dist/core.min.css',
},
},
onLoad: themeOnLoad,
};

renderHook(() => useParagonThemeCore(coreConfig));
const themeCoreLinks = document.head.querySelectorAll('link');
Expand All @@ -107,7 +66,7 @@ describe('useParagonThemeCore', () => {
});

it('should not create any core link if can not find themeCore urls definition', () => {
const coreConfig = {
coreConfig = {
themeCore: {
default: 'https://cdn.jsdelivr.net/npm/@edx/paragon@$21.0.0/dist/core.min.css',
},
Expand Down
18 changes: 0 additions & 18 deletions src/react/hooks/paragon/useParagonThemeVariants.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,6 @@ describe('useParagonThemeVariants', () => {
});

it('should dispatch a log error and fallback to PARAGON_THEME if can not load the variant theme link', () => {
global.PARAGON_THEME = {
paragon: {
version: '1.0.0',
themeUrls: {
core: {
fileName: 'core.min.css',
},
defaults: {
light: 'light',
},
variants: {
light: {
fileName: 'light.min.css',
},
},
},
},
};
const themeVariants = {
light: {
urls: {
Expand Down

0 comments on commit 317b0c1

Please sign in to comment.