Files
openchamber/packages/ui/src/contexts/ThemeSystemContext.test.ts
T

77 lines
2.4 KiB
TypeScript
Raw Normal View History

2026-06-16 23:33:29 +03:00
import { afterAll, beforeEach, describe, expect, test } from 'bun:test';
2026-07-26 20:33:04 +08:00
import { getDefaultTheme } from '@/lib/theme/themes';
import { resetEmbeddedSessionChatCache } from '@/components/layout/contextPanelEmbeddedChat';
import {
getInitialSystemPreference,
publishEmbeddedThemeBootstrap,
readEmbeddedThemeBootstrap,
} from './theme-embedded-bootstrap';
2026-06-16 23:33:29 +03:00
const originalWindow = globalThis.window;
2026-07-26 20:33:04 +08:00
const installWindow = (search: string, matchMediaDark: boolean, parentTheme?: unknown) => {
2026-06-16 23:33:29 +03:00
Object.defineProperty(globalThis, 'window', {
configurable: true,
value: {
location: {
search,
},
matchMedia: () => ({ matches: matchMediaDark }),
2026-07-26 20:33:04 +08:00
parent: parentTheme === undefined ? null : { __openchamberEmbeddedThemeBootstrap: parentTheme },
2026-06-16 23:33:29 +03:00
},
});
};
beforeEach(() => {
installWindow('', false);
2026-07-26 20:33:04 +08:00
resetEmbeddedSessionChatCache();
2026-06-16 23:33:29 +03:00
});
afterAll(() => {
Object.defineProperty(globalThis, 'window', {
configurable: true,
value: originalWindow,
});
});
describe('ThemeSystemProvider embedded bootstrap', () => {
test('uses parent effective variant for embedded system theme before iframe matchMedia', () => {
installWindow('?ocPanel=session-chat&themeMode=system&themeVariant=dark', false);
2026-07-26 20:33:04 +08:00
resetEmbeddedSessionChatCache();
2026-06-16 23:33:29 +03:00
expect(getInitialSystemPreference()).toBe(true);
});
2026-07-26 20:33:04 +08:00
test('uses a validated parent custom theme before first render', () => {
const customTheme = {
...getDefaultTheme(true),
metadata: {
...getDefaultTheme(true).metadata,
id: 'custom-dark',
name: 'Custom dark',
variant: 'dark' as const,
},
};
installWindow('?ocPanel=session-chat', false, customTheme);
resetEmbeddedSessionChatCache();
expect(readEmbeddedThemeBootstrap()).toBe(customTheme);
});
test('rejects an invalid parent theme bootstrap', () => {
installWindow('?ocPanel=session-chat', false, { metadata: { id: 'invalid' } });
resetEmbeddedSessionChatCache();
expect(readEmbeddedThemeBootstrap()).toBeNull();
});
test('publishes the current parent theme without using the URL', () => {
const currentTheme = getDefaultTheme(true);
publishEmbeddedThemeBootstrap(currentTheme);
expect((globalThis.window as unknown as { __openchamberEmbeddedThemeBootstrap?: unknown })
.__openchamberEmbeddedThemeBootstrap).toBe(currentTheme);
});
2026-06-16 23:33:29 +03:00
});