Files
Rosea/packages/ui/test/Shell.test.ts
T
mnovak a200832cfb [ui] LetterSlot: Add the page for A Letter from the Gardener, shown once the author writes it
docs/LETTER.md holds slots as comments only, so the player sees no change
until the author writes. The app bundles the letter at build time, a small
Markdown subset renders without {@html}, only https: and tel: become links,
and lint:copy now reads the letter. Review found turbo replaying a stale
build after the letter changed; docs/LETTER.md is now in its cache key.
2026-11-01 15:39:17 +01:00

86 lines
3.2 KiB
TypeScript

import { PRODUCT_NAME } from '@rosea/schema';
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/svelte';
import { afterEach, describe, expect, it } from 'vitest';
import Shell from '../src/Shell.svelte';
import { en } from '../src/copy/en.js';
afterEach(() => {
cleanup();
window.location.hash = '';
});
const LETTER = [
'# Why',
'',
'A line.',
'',
'[Read](https://example.org) or [call](tel:+420123) or [not](javascript:alert(1)).'
].join('\n');
const h1 = (): HTMLElement => screen.getByRole('heading', { level: 1 });
describe('Shell', () => {
it('names the product in the page heading, from the schema constant', () => {
render(Shell);
expect(screen.getByRole('heading', { level: 1 }).textContent).toBe(PRODUCT_NAME);
});
it('shows its one line from copy/, and no control to press', () => {
render(Shell);
expect(screen.getByText(en.shell.preparing)).toBeTruthy();
expect(screen.queryAllByRole('button')).toEqual([]);
expect(screen.queryAllByRole('link')).toEqual([]);
});
it('is a landmark, so a screen reader can find it', () => {
render(Shell);
expect(screen.getByRole('main')).toBeTruthy();
});
});
describe('Shell and the Letter', () => {
it('with an empty letter offers no way to it, and #letter shows the garden', () => {
window.location.hash = '#letter';
render(Shell, { letter: '<!-- Slot: the signature. -->\n' });
expect(h1().textContent).toBe(PRODUCT_NAME);
expect(screen.queryAllByRole('link')).toEqual([]);
});
it('with a letter, a link opens it, Back returns, and focus follows each heading', async () => {
render(Shell, { letter: LETTER });
expect(document.activeElement, 'nothing is focused on first load').toBe(document.body);
await fireEvent.click(screen.getByRole('link', { name: en.letter.title }));
await waitFor(() => expect(h1().textContent).toBe(en.letter.title));
await waitFor(() => expect(document.activeElement).toBe(h1()));
expect(screen.getByRole('heading', { level: 2 }).textContent).toBe('Why');
expect(screen.getByRole('main')).toBeTruthy();
await fireEvent.click(screen.getByRole('link', { name: en.letter.back }));
await waitFor(() => expect(h1().textContent).toBe(PRODUCT_NAME));
await waitFor(() => expect(document.activeElement).toBe(h1()));
});
it('opens straight at #letter, as from a bookmark', () => {
window.location.hash = '#letter';
render(Shell, { letter: LETTER });
expect(h1().textContent).toBe(en.letter.title);
});
it('renders https links in a new tab with no referrer, tel links in place, and nothing else as a link', () => {
window.location.hash = '#letter';
render(Shell, { letter: LETTER });
const read = screen.getByRole('link', { name: 'Read' });
expect(read.getAttribute('href')).toBe('https://example.org');
expect(read.getAttribute('target')).toBe('_blank');
expect(read.getAttribute('rel')).toBe('noopener noreferrer');
const call = screen.getByRole('link', { name: 'call' });
expect(call.getAttribute('href')).toBe('tel:+420123');
expect(call.getAttribute('target')).toBeNull();
expect(screen.queryByRole('link', { name: 'not' })).toBeNull();
expect(screen.getByText(/or not\./)).toBeTruthy();
});
});