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');
});
it('provides theme context with auto scheme', () => {
it('provides theme context with fallback to light scheme', () => {
const wrapper = createWrapper('auto');
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');
});
@@ -178,10 +178,9 @@ describe('ThemeContext', () => {
result.current.updateColorScheme('light');
});
expect(mockSetColorScheme).toHaveBeenCalledTimes(3);
expect(mockSetColorScheme).toHaveBeenCalledTimes(2);
expect(mockSetColorScheme).toHaveBeenNthCalledWith(1, 'dark');
expect(mockSetColorScheme).toHaveBeenNthCalledWith(2, 'auto');
expect(mockSetColorScheme).toHaveBeenNthCalledWith(3, 'light');
expect(mockSetColorScheme).toHaveBeenNthCalledWith(2, 'light');
});
it('calls setColorScheme immediately without batching', () => {

View File

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

View File

@@ -171,22 +171,6 @@ describe('useWorkspace', () => {
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', () => {