// @vitest-environment jsdom // // `/login` demo sign-in. Clicking "Continue" sets the demo session cookie and // navigates to `?from=` (defaulting to /admin). We mock useNavigate from // @voltro/web and drive the click. import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest' import { act, createElement, type ReactNode } from 'react' import { createRoot, type Root } from 'react-dom/client' import { I18nProvider } from '@voltro/i18n' import enCatalog from '../../../locales/en' const navigate = vi.fn<(to: string) => void>() vi.mock('@voltro/web', () => ({ useNavigate: () => navigate })) const { default: Login, meta, renderMode } = await import('./page') ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true let container: HTMLDivElement let root: Root // The page uses , so wrap the render in an (auto-wired in app). const render = (node: ReactNode): void => { container = document.createElement('div') document.body.appendChild(container) act(() => { root = createRoot(container) root.render( createElement(I18nProvider, { locale: 'en', messages: enCatalog, defaultLocale: 'en', children: node }), ) }) } const clickContinue = (): void => { const btn = container.querySelector('button')! act(() => { btn.dispatchEvent(new MouseEvent('click', { bubbles: true })) }) } beforeEach(() => { navigate.mockReset() document.cookie = 'demo_session=; path=/; max-age=0' }) afterEach(() => { if (root) act(() => root.unmount()) container?.remove() document.body.innerHTML = '' }) describe('login — config', () => { test('is a static page titled Sign in', () => { expect(renderMode).toBe('static') expect(meta({ locale: 'en' }).title).toBe('Sign in') }) }) describe('login — sign in', () => { test('sets the session cookie and navigates to /admin by default', () => { render(createElement(Login)) clickContinue() expect(document.cookie).toContain('demo_session=ok') expect(navigate).toHaveBeenCalledWith('/admin') }) test('honours the ?from= redirect target', () => { window.history.replaceState({}, '', '/login?from=/admin/todos') render(createElement(Login)) clickContinue() expect(navigate).toHaveBeenCalledWith('/admin/todos') }) })