import { act, cleanup, render } from '@testing-library/react'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { DESKTOP_COOKIE_NAME } from '@/lib/desktop-client'; import { DesktopThemeSync } from './desktop-theme-sync'; /** * desktop-theme-sync.test (quick-261002) — meldet hell/dunkel per * `plugin:window|set_theme` an das Desktop-Fenster, im Browser nichts. */ const theme = vi.hoisted(() => ({ resolvedTheme: 'dark' as string | undefined })); vi.mock('next-themes', () => ({ useTheme: () => theme })); const invoke = vi.fn(() => Promise.resolve()); function setCookie() { document.cookie = `${DESKTOP_COOKIE_NAME}=1; path=/`; } beforeEach(() => { invoke.mockClear(); (window as unknown as Record).__TAURI_INTERNALS__ = { invoke }; theme.resolvedTheme = 'dark'; }); afterEach(() => { cleanup(); document.cookie = `${DESKTOP_COOKIE_NAME}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`; delete (window as unknown as Record).__TAURI_INTERNALS__; }); describe('DesktopThemeSync', () => { it('meldet im Desktop-Client das dunkle Erscheinungsbild', async () => { setCookie(); await act(async () => { render(); }); expect(invoke).toHaveBeenCalledWith('plugin:window|set_theme', { value: 'dark' }); }); it('meldet einen Wechsel auf hell erneut', async () => { setCookie(); const view = await act(async () => render()); theme.resolvedTheme = 'light'; await act(async () => { view.rerender(); }); expect(invoke).toHaveBeenLastCalledWith('plugin:window|set_theme', { value: 'light' }); }); it('ruft im Browser nichts auf', async () => { await act(async () => { render(); }); expect(invoke).not.toHaveBeenCalled(); }); it('wartet, solange das Erscheinungsbild noch nicht feststeht', async () => { setCookie(); theme.resolvedTheme = undefined; await act(async () => { render(); }); expect(invoke).not.toHaveBeenCalled(); }); });