shell.test.ts (3787B)
1 import { render } from 'svelte/server'; 2 import type { ComponentProps } from 'svelte'; 3 import { describe, expect, it } from 'vitest'; 4 import AppShellHarness from './AppShellHarness.svelte'; 5 6 function body(props: ComponentProps<typeof AppShellHarness> = {}) { 7 return render(AppShellHarness, { props }).body; 8 } 9 10 describe('shared application shell SSR', () => { 11 it('keeps guest order, semantic landmarks, skip target and route heading', () => { 12 const html = body({ available: true }); 13 const labels = [ 14 'HarvestCircle', 15 'Search', 16 'List food', 17 'Connect extension' 18 ]; 19 for (let index = 1; index < labels.length; index++) 20 expect(html.indexOf(labels[index - 1])).toBeLessThan( 21 html.indexOf(labels[index]) 22 ); 23 expect(html).toContain('<header'); 24 expect(html).toContain('aria-label="Primary"'); 25 expect(html).toContain('href="#main-content"'); 26 expect(html).toContain('id="main-content" tabindex="-1"'); 27 expect(html).toContain('<h1>Fixture route heading</h1>'); 28 expect(html).not.toContain('<h1>HarvestCircle'); 29 expect(html).toContain('<footer'); 30 expect(html).toContain('href="/about#help"'); 31 }); 32 it('keeps connected order, unchanged public key, native disclosure and no unread promise', () => { 33 const publicKey = 'HC_TEST_ONLY_<' + 'b'.repeat(64); 34 const html = body({ connected: true, available: true, publicKey }); 35 const labels = [ 36 'HarvestCircle', 37 'Search', 38 'Messages', 39 'Selling', 40 'Identity' 41 ]; 42 for (let index = 1; index < labels.length; index++) 43 expect(html.indexOf(labels[index - 1])).toBeLessThan( 44 html.indexOf(labels[index]) 45 ); 46 expect(html).toContain('href="/selling"'); 47 expect(html).toContain('<summary>Identity</summary>'); 48 expect(html).toContain('HC_TEST_ONLY_<' + 'b'.repeat(64)); 49 expect(html).toContain('Disconnect'); 50 expect(html).not.toMatch( 51 /unread|aria-live|account|settings|Connect extension/ 52 ); 53 }); 54 it('marks only the real current route and never navigates an invalid current path', () => { 55 const html = body({ available: true, currentPath: '/search' }); 56 expect(html).toMatch(/href="\/search"[^>]*aria-current="page"/); 57 expect(html.match(/aria-current="page"/g)).toHaveLength(1); 58 expect(body({ currentPath: '//invalid.example' })).not.toContain( 59 'href="//invalid.example' 60 ); 61 }); 62 it('keeps unfinished routes and missing callbacks visibly unavailable without links', () => { 63 const html = body({ connected: true }); 64 expect(html).toContain('Unavailable during development'); 65 expect(html).not.toMatch( 66 /href="\/(search|sell|selling|messages|about|privacy)/ 67 ); 68 expect(html).toMatch(/<button[^>]*disabled[^>]*>Disconnect/); 69 expect(html).toContain('aria-disabled="true"'); 70 expect(html).toContain('Messages'); 71 }); 72 it('reports only currently visible unavailable routes and commands', () => { 73 const forbidden = () => { 74 throw new Error('SSR invoked command'); 75 }; 76 expect(body({ available: true, onconnect: forbidden })).not.toContain( 77 'Unavailable during development' 78 ); 79 const html = body({ 80 connected: true, 81 available: true, 82 messages: false, 83 ondisconnect: forbidden 84 }); 85 expect(html).toContain('Unavailable during development'); 86 expect(html).toContain('Messages'); 87 expect(html).not.toContain('href="/messages"'); 88 }); 89 it('never invokes callbacks or browser effects while server rendering', () => { 90 const forbidden = () => { 91 throw new Error('SSR invoked command'); 92 }; 93 expect(() => body({ onconnect: forbidden })).not.toThrow(); 94 expect(() => 95 body({ connected: true, ondisconnect: forbidden }) 96 ).not.toThrow(); 97 }); 98 });