Skip to content

Commit 2a19d40

Browse files
committed
test: improve test coverage for useTheme
1 parent 8f05632 commit 2a19d40

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

‎packages/frontend/test/unit/hooks/useTheme.test.tsx‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ import { useTheme } from '../../../src/hooks/useTheme';
55
import { createMockStore } from '../../utils';
66

77
describe('useTheme', () => {
8+
const mockMatchMedia = (matches: boolean) => {
9+
Object.defineProperty(window, 'matchMedia', {
10+
writable: true,
11+
value: jest.fn().mockReturnValue({
12+
matches,
13+
addEventListener: jest.fn(),
14+
removeEventListener: jest.fn(),
15+
}),
16+
});
17+
};
18+
819
test('should return a light theme when darkMode is false', () => {
920
const store = createMockStore({ appSetting: { darkMode: 'light' } });
1021
const { result } = renderHook(() => useTheme(), {
@@ -38,4 +49,32 @@ describe('useTheme', () => {
3849
});
3950
expect(darkThemeResult.current.palette.primary.main).toBe('#1976d2');
4051
});
52+
53+
test('should use the system dark preference when darkMode is auto', () => {
54+
mockMatchMedia(true);
55+
const store = createMockStore({
56+
appSetting: { darkMode: 'auto' },
57+
config: { theme: 'aurora' },
58+
});
59+
const { result } = renderHook(() => useTheme(), {
60+
wrapper: ({ children }) => <Provider store={store}>{children}</Provider>,
61+
});
62+
expect(result.current.palette.mode).toBe('dark');
63+
expect(result.current.palette.background.default).toBe('#00251a');
64+
expect(result.current.palette.background.paper).toBe('#012a4a');
65+
});
66+
67+
test('should fall back to the default light theme for an unknown theme id', () => {
68+
mockMatchMedia(false);
69+
const store = createMockStore({
70+
appSetting: { darkMode: 'auto' },
71+
config: { theme: 'unknown-theme' },
72+
});
73+
const { result } = renderHook(() => useTheme(), {
74+
wrapper: ({ children }) => <Provider store={store}>{children}</Provider>,
75+
});
76+
expect(result.current.palette.mode).toBe('light');
77+
expect(result.current.palette.background.default).toBe('#f5f7fa');
78+
expect(result.current.palette.background.paper).toBe('#eef1f5');
79+
});
4180
});

0 commit comments

Comments
 (0)