Refactor ThemeContext to ensure fallback to light scheme and update color scheme logic

This commit is contained in:
2025-06-04 19:17:09 +02:00
parent 54feefcd5c
commit 07af3f6e39
3 changed files with 10 additions and 24 deletions

View File

@@ -59,11 +59,11 @@ describe('ThemeContext', () => {
expect(typeof result.current.updateColorScheme).toBe('function'); expect(typeof result.current.updateColorScheme).toBe('function');
}); });
it('provides theme context with auto scheme', () => { it('provides theme context with fallback to light scheme', () => {
const wrapper = createWrapper('auto'); const wrapper = createWrapper('auto');
const { result } = renderHook(() => useTheme(), { wrapper }); const { result } = renderHook(() => useTheme(), { wrapper });
expect(result.current.colorScheme).toBe('auto'); expect(result.current.colorScheme).toBe('light'); // Mantine defaults to light if auto is used
expect(typeof result.current.updateColorScheme).toBe('function'); expect(typeof result.current.updateColorScheme).toBe('function');
}); });
@@ -178,10 +178,9 @@ describe('ThemeContext', () => {
result.current.updateColorScheme('light'); result.current.updateColorScheme('light');
}); });
expect(mockSetColorScheme).toHaveBeenCalledTimes(3); expect(mockSetColorScheme).toHaveBeenCalledTimes(2);
expect(mockSetColorScheme).toHaveBeenNthCalledWith(1, 'dark'); expect(mockSetColorScheme).toHaveBeenNthCalledWith(1, 'dark');
expect(mockSetColorScheme).toHaveBeenNthCalledWith(2, 'auto'); expect(mockSetColorScheme).toHaveBeenNthCalledWith(2, 'light');
expect(mockSetColorScheme).toHaveBeenNthCalledWith(3, 'light');
}); });
it('calls setColorScheme immediately without batching', () => { it('calls setColorScheme immediately without batching', () => {

View File

@@ -22,16 +22,19 @@ export const ThemeProvider: React.FC<ThemeProviderProps> = ({ children }) => {
const updateColorScheme = useCallback( const updateColorScheme = useCallback(
(newTheme: MantineColorScheme): void => { (newTheme: MantineColorScheme): void => {
if (newTheme === 'light' || newTheme === 'dark') {
if (setColorScheme) { if (setColorScheme) {
setColorScheme(newTheme); setColorScheme(newTheme);
} }
}
}, },
[setColorScheme] [setColorScheme]
); );
// Ensure colorScheme is never undefined by falling back to light theme // Ensure colorScheme is never undefined by falling back to light theme
const value: ThemeContextType = { const value: ThemeContextType = {
colorScheme: colorScheme || 'light', colorScheme:
colorScheme === 'light' || colorScheme === 'dark' ? colorScheme : 'light',
updateColorScheme, updateColorScheme,
}; };

View File

@@ -171,22 +171,6 @@ describe('useWorkspace', () => {
mockTheme.updateColorScheme mockTheme.updateColorScheme
); );
}); });
it('handles light theme', () => {
mockTheme.colorScheme = 'light';
const { result } = renderHook(() => useWorkspace());
expect(result.current.colorScheme).toBe('light');
});
it('handles auto theme', () => {
mockTheme.colorScheme = 'auto';
const { result } = renderHook(() => useWorkspace());
expect(result.current.colorScheme).toBe('auto');
});
}); });
describe('workspace operations integration', () => { describe('workspace operations integration', () => {