Files
Rosea/packages/ui/test/Shell.test.ts
T

132 lines
5.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';
import { said } from './said.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('the licence credit (CPAL-1.0 Exhibit B; domi-ui.md D9)', () => {
const credit = 'Built on Domi · https://aula.marek-novak.cz · Copyright (c) 2026 Marek Novák';
/** The credit line as a reader gets it: @domi/ui's DomiCredit sets its parts in spans. */
const creditLine = (): string => document.querySelector('.domi-credit')?.textContent?.replace(/\s+/g, ' ').trim() ?? '';
it('stands at the foot of the first screen in its exact words, as plain text that leads nowhere', () => {
render(Shell);
expect(creditLine()).toBe(credit);
expect(document.querySelector('.domi-credit a')).toBeNull();
expect(document.querySelectorAll('a[href^="https://aula"]')).toHaveLength(0);
});
it('stands at the foot of the Letter too', async () => {
window.location.hash = '#letter';
render(Shell, { letter: LETTER });
await waitFor(() => expect(h1().textContent).toBe(en.letter.title));
expect(creditLine()).toBe(credit);
});
});
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();
});
});
describe('Shell and a new version waiting (calm-refresh.md)', () => {
it('says nothing while no new version waits', () => {
render(Shell, { onrefresh: () => undefined });
expect(screen.queryByText(en.update.ready)).toBeNull();
expect(screen.queryByRole('button', { name: en.update.refresh })).toBeNull();
});
it('when one waits: one quiet line and Refresh, which only the player\'s tap sends', async () => {
let refreshed = 0;
render(Shell, { updateReady: true, onrefresh: () => (refreshed += 1) });
expect(said().textContent).toBe(en.update.ready);
expect(refreshed).toBe(0);
await fireEvent.click(screen.getByRole('button', { name: en.update.refresh }));
expect(refreshed).toBe(1);
});
it('is there on the Letter too', () => {
window.location.hash = '#letter';
render(Shell, { letter: LETTER, updateReady: true, onrefresh: () => undefined });
expect(h1().textContent).toBe(en.letter.title);
expect(screen.getByRole('button', { name: en.update.refresh })).toBeTruthy();
});
});