-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
93 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
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 mockMediaQueryList; | ||
const mockAddEventListener = jest.fn((dispatch, fn) => fn(mockMediaQueryList)); | ||
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 = ''; | ||
mockMediaQueryList = { matches: false }; | ||
mockAddEventListener.mockClear(); | ||
mockRemoveEventListener.mockClear(); | ||
window.localStorage.getItem.mockClear(); | ||
}); | ||
|
||
it('should configure theme variants according with system preference and add the change event listener', () => { | ||
mockMediaQueryList.matches = true; | ||
|
||
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)', () => { | ||
mockMediaQueryList.matches = true; | ||
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters