mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-08-17 00:31:00 +00:00
fix(frontend): preserve theme body classes (#6157)
* fix(storybook): preserve preview body classes * fix(frontend): retain theme body classes * fix(storybook): mirror panel theme attributes * test(storybook): cover theme switches * test(storybook): strengthen theme DOM coverage * fix(frontend): preserve message container classes --------- Co-authored-by: PathGao <gaoyanbo@gaoyanbodeMacBook-Air.local>
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { render } from '@testing-library/react';
|
||||
import { afterEach, expect, test } from 'vitest';
|
||||
|
||||
import { withTheme } from '../../.storybook/preview';
|
||||
import { ThemeProvider } from '@/hooks/useTheme';
|
||||
|
||||
function Story() {
|
||||
return <div>Story</div>;
|
||||
}
|
||||
|
||||
function StorybookTheme({ theme }: { theme: 'light' | 'dark' }) {
|
||||
return withTheme(Story, { globals: { theme } } as Partial<Parameters<typeof withTheme>[1]> as Parameters<typeof withTheme>[1]);
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
document.body.className = '';
|
||||
document.documentElement.removeAttribute('data-theme');
|
||||
});
|
||||
|
||||
test('preserves unrelated body classes when applying the Storybook theme', () => {
|
||||
document.body.className = 'storybook-fixture dark';
|
||||
document.documentElement.setAttribute('data-theme', 'ultra-dark');
|
||||
const { rerender } = render(<StorybookTheme theme="light" />);
|
||||
|
||||
expect(document.body.classList.contains('storybook-fixture')).toBe(true);
|
||||
expect(document.body.classList.contains('light')).toBe(true);
|
||||
expect(document.body.classList.contains('dark')).toBe(false);
|
||||
expect(document.documentElement.hasAttribute('data-theme')).toBe(false);
|
||||
|
||||
rerender(<StorybookTheme theme="dark" />);
|
||||
|
||||
expect(document.body.classList.contains('storybook-fixture')).toBe(true);
|
||||
expect(document.body.classList.contains('dark')).toBe(true);
|
||||
expect(document.body.classList.contains('light')).toBe(false);
|
||||
});
|
||||
|
||||
test('preserves unrelated body classes when applying the panel theme', () => {
|
||||
document.body.className = 'panel-fixture';
|
||||
const message = document.createElement('div');
|
||||
message.id = 'message';
|
||||
message.className = 'message-fixture';
|
||||
document.body.append(message);
|
||||
|
||||
render(<ThemeProvider><div>Panel</div></ThemeProvider>);
|
||||
|
||||
expect(document.body.classList.contains('panel-fixture')).toBe(true);
|
||||
expect(document.body.classList.contains('dark')).toBe(true);
|
||||
expect(document.body.classList.contains('light')).toBe(false);
|
||||
expect(message.classList.contains('message-fixture')).toBe(true);
|
||||
expect(message.classList.contains('dark')).toBe(true);
|
||||
});
|
||||
Reference in New Issue
Block a user