search-form.test.ts (2910B)
1 import { render } from 'svelte/server'; 2 import { describe, expect, it } from 'vitest'; 3 import SearchForm from '../../../src/lib/components/SearchForm.svelte'; 4 5 describe('shared labelled search form SSR', () => { 6 it('renders a public URL query and linked field error without invoking navigation', () => { 7 const html = render(SearchForm, { 8 props: { 9 initialValue: 'carrots', 10 error: 'Use at most 12 words.', 11 onsubmit: () => { 12 throw new Error('SSR navigation'); 13 } 14 } 15 }).body; 16 expect(html).toContain('carrots'); 17 expect(html).toContain('Use at most 12 words.'); 18 expect(html).toContain('aria-invalid="true"'); 19 const id = html.match(/<textarea[^>]* id="([^"]+)"/)?.[1]; 20 expect(html).toContain(`id="${id}-error"`); 21 expect(html).toContain(`aria-describedby="${id}-hint ${id}-error"`); 22 }); 23 it('renders one two-row textarea, visible associated label and example hint', () => { 24 const html = render(SearchForm).body; 25 expect(html.match(/<textarea\b/g)).toHaveLength(1); 26 expect(html).toContain('rows="2"'); 27 expect(html).toContain('What are you looking for?'); 28 expect(html).toContain('For example, carrots or carrots Victoria.'); 29 const id = html.match(/<textarea[^>]* id="([^"]+)"/)?.[1]; 30 expect(id).toBeTruthy(); 31 expect(html).toContain(`for="${id}"`); 32 expect(html).toContain(`aria-describedby="${id}-hint"`); 33 expect(html).toContain(`id="${id}-hint"`); 34 }); 35 it('uses native form submission and visibly disables absent callers', () => { 36 const html = render(SearchForm).body; 37 expect(html).toContain('<form'); 38 expect(html).toMatch(/<button[^>]*type="submit"[^>]*disabled[^>]*>Search/); 39 expect(html).toContain('Search is unavailable during development.'); 40 expect(html).not.toMatch(/action=|placeholder=|maxlength=|required/); 41 }); 42 it('retains an explicit typed caller but waits for client initialization before enabling controls', () => { 43 const html = render(SearchForm, { 44 props: { 45 onsubmit: (input: string) => { 46 throw new Error(`SSR invoked search caller: ${input}`); 47 } 48 } 49 }).body; 50 expect(html).toMatch(/<button[^>]*disabled/); 51 expect(html).toMatch(/<textarea[^>]*disabled/); 52 expect(html).not.toContain('Search is unavailable'); 53 }); 54 it('honors explicit disabled even with a caller', () => { 55 const html = render(SearchForm, { 56 props: { 57 disabled: true, 58 onsubmit: () => { 59 throw new Error('SSR invoked disabled caller'); 60 } 61 } 62 }).body; 63 expect(html).toMatch(/<button[^>]*disabled/); 64 expect(html).toContain('Search is unavailable'); 65 }); 66 it('does not add search policy, onboarding or browser effects', () => { 67 const html = render(SearchForm).body; 68 expect(html).not.toMatch( 69 /<h1|location|signup|AI|WebSocket|nostr|\/search\?/ 70 ); 71 }); 72 });