Skip to content

Commit af38d90

Browse files
committed
test: improve test coverage for store app settings
1 parent 5a314d5 commit af38d90

1 file changed

Lines changed: 106 additions & 0 deletions

File tree

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { saveAppSetting } from '../../../src/store/slices/appSettingSlice';
2+
3+
type StoreModule = typeof import('../../../src/store/store');
4+
type MatchMediaMock = MediaQueryList & { dispatchChange: (matches: boolean) => void };
5+
6+
const defaultAppSetting = {
7+
darkMode: 'auto',
8+
contentMode: 'compact',
9+
fileTreeOpen: true,
10+
outlineOpen: true,
11+
} as const;
12+
13+
const importStoreModule = async (): Promise<StoreModule> => {
14+
jest.resetModules();
15+
return import('../../../src/store/store');
16+
};
17+
18+
const createMatchMedia = (initialMatches: boolean): MatchMediaMock => {
19+
const listeners = new Set<(event: MediaQueryListEvent) => void>();
20+
const mediaQueryList = {
21+
matches: initialMatches,
22+
media: '(prefers-color-scheme: dark)',
23+
onchange: null,
24+
addEventListener: jest.fn((event: string, listener: EventListenerOrEventListenerObject) => {
25+
if (event === 'change' && typeof listener === 'function') listeners.add(listener);
26+
}),
27+
removeEventListener: jest.fn((event: string, listener: EventListenerOrEventListenerObject) => {
28+
if (event === 'change' && typeof listener === 'function') listeners.delete(listener);
29+
}),
30+
addListener: jest.fn(),
31+
removeListener: jest.fn(),
32+
dispatchEvent: jest.fn(),
33+
dispatchChange: (matches: boolean) => {
34+
mediaQueryList.matches = matches;
35+
[...listeners].map((listener) => listener({ matches } as MediaQueryListEvent));
36+
},
37+
};
38+
39+
return mediaQueryList as MatchMediaMock;
40+
};
41+
42+
describe('store', () => {
43+
beforeEach(() => {
44+
jest.restoreAllMocks();
45+
localStorage.clear();
46+
document.body.removeAttribute('data-theme');
47+
});
48+
49+
it('uses the default app setting when localStorage is empty', async () => {
50+
const { store } = await importStoreModule();
51+
52+
expect(store.getState().appSetting).toEqual(defaultAppSetting);
53+
});
54+
55+
it('loads persisted app settings from localStorage', async () => {
56+
localStorage.setItem('appSetting', JSON.stringify({
57+
darkMode: 'light',
58+
contentMode: 'full',
59+
fileTreeOpen: false,
60+
outlineOpen: false,
61+
}));
62+
63+
const { store } = await importStoreModule();
64+
65+
expect(store.getState().appSetting).toEqual({
66+
darkMode: 'light',
67+
contentMode: 'full',
68+
fileTreeOpen: false,
69+
outlineOpen: false,
70+
});
71+
});
72+
73+
it('falls back to the default app setting when localStorage read fails', async () => {
74+
const error = new Error('localStorage read failed');
75+
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
76+
jest.spyOn(Storage.prototype, 'getItem').mockImplementation(() => {
77+
throw error;
78+
});
79+
80+
const { store } = await importStoreModule();
81+
82+
expect(store.getState().appSetting).toEqual(defaultAppSetting);
83+
expect(consoleErrorSpy).toHaveBeenCalledWith('Could not load state from localStorage', error);
84+
});
85+
86+
it('applies explicit and auto themes when app settings change', async () => {
87+
const matchMedia = createMatchMedia(false);
88+
Object.defineProperty(window, 'matchMedia', {
89+
writable: true,
90+
value: jest.fn().mockReturnValue(matchMedia),
91+
});
92+
93+
const { store } = await importStoreModule();
94+
95+
store.dispatch(saveAppSetting({ darkMode: 'dark', contentMode: 'compact' }));
96+
expect(document.body).toHaveAttribute('data-theme', 'dark');
97+
98+
store.dispatch(saveAppSetting({ darkMode: 'auto', contentMode: 'compact' }));
99+
expect(window.matchMedia).toHaveBeenCalledWith('(prefers-color-scheme: dark)');
100+
expect(document.body).toHaveAttribute('data-theme', 'light');
101+
102+
matchMedia.dispatchChange(true);
103+
expect(document.body).toHaveAttribute('data-theme', 'dark');
104+
expect(matchMedia.addEventListener).toHaveBeenCalledWith('change', expect.any(Function));
105+
});
106+
});

0 commit comments

Comments
 (0)