Skip to content

Commit

Permalink
test: add useParagonTheme test
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoa committed May 11, 2024
1 parent 4c50878 commit e81b549
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 40 deletions.
91 changes: 91 additions & 0 deletions src/react/hooks/paragon/useParagonTheme.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { act, renderHook } from '@testing-library/react-hooks';
import useParagonTheme from './useParagonTheme';
import { getConfig } from '../../../config';

jest.mock('../../../config', () => ({
...jest.requireActual('.../../../config'),
getConfig: jest.fn().mockReturnValue({
PARAGON_THEME_URLS: {
core: {
urls: {
default: 'core.css',
},
},
defaults: {
light: 'light',
dark: 'dark',
},
variants: {
light: {
urls: {
default: 'https://cdn.jsdelivr.net/npm/@edx/paragon@$21.0.0/dist/light.min.css',
},
},
dark: {
urls: {
default: 'https://cdn.jsdelivr.net/npm/@edx/paragon@$21.0.0/dist/dark.min.css',
},
},
},
},
}),
}));

let mockMediaQueryListEvent;
const mockAddEventListener = jest.fn((dispatch, fn) => fn(mockMediaQueryListEvent));
const mockRemoveEventListener = jest.fn();

Object.defineProperty(window, 'matchMedia', {
value: jest.fn(() => ({
addEventListener: mockAddEventListener,
removeEventListener: mockRemoveEventListener,
})),
});

Object.defineProperty(window, 'localStorage', {
value: {
getItem: jest.fn(),
},
});

describe('useParagonTheme', () => {
beforeEach(() => {
document.head.innerHTML = '';
mockMediaQueryListEvent = { matches: true };
mockAddEventListener.mockClear();
mockRemoveEventListener.mockClear();
window.localStorage.getItem.mockClear();
});

it('should configure theme variants according with system preference and add the change event listener', () => {
const { 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"]');
act(() => { themeLinks.forEach((link) => link.onload()); });

expect(window.matchMedia).toHaveBeenCalledWith('(prefers-color-scheme: dark)');
expect(mockAddEventListener).toHaveBeenCalled();
expect(darkLink.rel).toBe('stylesheet');
expect(lightLink.rel).toBe('alternate stylesheet');
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 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"]');
act(() => { themeLinks.forEach((link) => link.onload()); });

expect(window.matchMedia).toHaveBeenCalledWith('(prefers-color-scheme: dark)');
expect(mockAddEventListener).toHaveBeenCalled();

expect(darkLink.rel).toBe('alternate stylesheet');
expect(lightLink.rel).toBe('stylesheet');
unmount();
expect(mockRemoveEventListener).toHaveBeenCalled();
});
});
40 changes: 0 additions & 40 deletions src/react/hooks/paragon/useParagonThemeVariants.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,6 @@ import useParagonThemeVariants from './useParagonThemeVariants';

jest.mock('../../../logging');

const mockAddEventListener = jest.fn();
const mockRemoveEventListener = jest.fn();
const mockOnChange = jest.fn();

Object.defineProperty(window, 'matchMedia', {
value: jest.fn(() => ({
addEventListener: mockAddEventListener,
removeEventListener: mockRemoveEventListener,
onchange: mockOnChange,
})),
});

describe('useParagonThemeVariants', () => {
const themeOnLoad = jest.fn();

Expand Down Expand Up @@ -85,34 +73,6 @@ describe('useParagonThemeVariants', () => {
expect(document.querySelector('link').href).toBe(`${getConfig().BASE_URL}/${PARAGON_THEME.paragon.themeUrls.variants.light.fileName}`);
});

it('should configure theme variants according with system preference and add the change event listener', () => {
window.matchMedia['prefers-color-scheme'] = 'dark';

const themeVariants = {
light: {
urls: {
default: 'https://cdn.jsdelivr.net/npm/@edx/paragon@$21.0.0/dist/light.min.css',
brandOverride: 'https://cdn.jsdelivr.net/npm/@edx/brand@$2.0.0/dist/light.min.css',
},
},
dark: {
urls: {
default: 'https://cdn.jsdelivr.net/npm/@edx/paragon@$21.0.0/dist/dark.min.css',
brandOverride: 'https://cdn.jsdelivr.net/npm/@edx/brand@$2.0.0/dist/dark.min.css',
},
},
};

const currentThemeVariant = 'light';

renderHook(() => useParagonThemeVariants({ themeVariants, currentThemeVariant, onLoad: themeOnLoad }));

const themeLinks = document.head.querySelectorAll('link');
act(() => { themeLinks.forEach((link) => link.onload()); });

expect(mockAddEventListener).toHaveBeenCalledTimes(1);
});

it('should do nothing if themeVariants is not configured', () => {
const themeVariants = null;
const currentTheme = 'light';
Expand Down

0 comments on commit e81b549

Please sign in to comment.