primitives.test.ts (3882B)
1 import { render } from 'svelte/server'; 2 import { createRawSnippet } from 'svelte'; 3 import { describe, expect, it } from 'vitest'; 4 import FormField from '../../../src/lib/components/primitives/FormField.svelte'; 5 import Button from '../../../src/lib/components/primitives/Button.svelte'; 6 import Disclosure from '../../../src/lib/components/primitives/Disclosure.svelte'; 7 import PageHeading from '../../../src/lib/components/primitives/PageHeading.svelte'; 8 import ActionGroup from '../../../src/lib/components/primitives/ActionGroup.svelte'; 9 import EmptyState from '../../../src/lib/components/primitives/EmptyState.svelte'; 10 import ConfirmPanel from '../../../src/lib/components/primitives/ConfirmPanel.svelte'; 11 12 describe('presentation primitives SSR', () => { 13 it('associates visible labels, hints and above-control errors', () => { 14 const output = render(FormField, { 15 props: { 16 id: 'terms', 17 label: 'Terms', 18 hint: 'Public terms', 19 error: 'Enter terms', 20 control: createRawSnippet< 21 [{ id: string; describedby: string | undefined; invalid: boolean }] 22 >((get) => ({ 23 render() { 24 const control = get(); 25 return `<input id="${control.id}" aria-describedby="${control.describedby}" aria-invalid="${control.invalid}">`; 26 } 27 })) 28 } 29 }).body; 30 expect(output).toContain('for="terms"'); 31 expect(output).toContain('id="terms-hint"'); 32 expect(output).toContain('id="terms-error"'); 33 expect(output.indexOf('Enter terms')).toBeLessThan( 34 output.indexOf('<input') 35 ); 36 expect(output).toContain('aria-describedby="terms-hint terms-error"'); 37 expect(output).toContain('aria-invalid="true"'); 38 }); 39 it('keeps safe button semantics and finite variants', () => { 40 const output = render(Button, { 41 props: { label: 'Save', disabled: true } 42 }).body; 43 expect(output).toContain('<button'); 44 expect(output).toContain('type="button"'); 45 expect(output).toContain('disabled'); 46 expect(output).not.toContain('<a'); 47 }); 48 it('validates actual navigation sinks even for untyped callers', () => { 49 for (const href of [ 50 '/search', 51 'https://example.org/', 52 'mailto:support@example.org' 53 ]) { 54 const output = render(Button, { 55 props: { kind: 'link', label: 'Help', href } 56 }).body; 57 expect(output).toContain('<a'); 58 expect(output).toContain(`href="${href}"`); 59 } 60 const output = render(Button, { 61 props: { kind: 'link', label: 'Invalid target', href: '//example.org' } 62 }).body; 63 expect(output).not.toContain('href='); 64 expect(output).not.toContain('<a'); 65 }); 66 it('uses native disclosure and semantic presentation', () => { 67 expect( 68 render(Disclosure, { props: { summary: 'Details' } }).body 69 ).toContain('<summary>Details</summary>'); 70 expect( 71 render(PageHeading, { 72 props: { title: 'Review', description: 'Check public terms' } 73 }).body 74 ).toContain('<h1>Review</h1>'); 75 expect(render(ActionGroup).body).toContain('class="cluster"'); 76 expect( 77 render(EmptyState, { 78 props: { title: 'No results', description: 'Try other terms' } 79 }).body 80 ).toContain('No results'); 81 }); 82 it('states specific impact and keeps an explicit safe exit', () => { 83 const output = render(ConfirmPanel, { 84 props: { 85 id: 'discard', 86 title: 'Discard draft?', 87 impact: 'This removes this browser draft. Remote copies remain.', 88 confirmLabel: 'Discard draft', 89 cancelLabel: 'Keep editing', 90 onconfirm: () => {}, 91 oncancel: () => {} 92 } 93 }).body; 94 expect(output).toContain('aria-labelledby="discard-title"'); 95 expect(output).toContain('Remote copies remain.'); 96 expect(output).toContain('Keep editing'); 97 expect(output).not.toContain('role="dialog"'); 98 }); 99 });