import {describe, expect, test} from 'bun:test' import m from '@garage44/mithril' import mServer from '@garage44/mithril/server' import {PageView} from './frame' import {renderPage} from './ssr' import {type PageData, pageHref, parsePath, registry} from './tree' /** A page with every component in it, its texts in Dutch with one falling back to English. */ function everything(slug: string): PageData { const t = (key: string) => ({t: key}) return { lang: 'nl', languages: ['nl', 'en'], menu: { home: true, pages: [ {label: {lang: 'nl', text: 'Over'}, slug: 'over'}, {label: {lang: 'nl', text: 'Je eigen server'}, slug: 'server'}, ], }, revision: 3, slug, source_lang: 'nl', texts: { alt: {lang: 'nl', text: 'De Grote Markt'}, by: {lang: 'nl', text: 'Een buur'}, button: {lang: 'nl', text: 'Doe mee'}, description: {lang: 'nl', text: 'Over ons'}, feature: {lang: 'nl', text: 'Praat op de kaart.'}, heading: {lang: 'nl', text: 'Jouw stad, **gedeeld**'}, lead: {lang: 'en', text: 'Talk and build *together*.'}, place: {lang: 'nl', text: 'Naar de markt'}, quote: {lang: 'nl', text: 'Eindelijk.'}, title: {lang: 'nl', text: 'Over'}, }, tree: { c: 'Page', ch: [ {c: 'Hero', ch: [{c: 'Text', ch: [t('lead')], p: {lead: true}}, {c: 'ButtonLink', ch: [t('button')], p: {href: '/chat'}}, {c: 'DownloadButtons'}], p: {heading: t('heading')}}, { c: 'Section', ch: [ { c: 'Columns', ch: [ {c: 'Feature', ch: [t('feature')], p: {heading: t('heading'), icon: 'chat'}}, {c: 'Image', p: {alt: t('alt'), src: '/img/groningen.svg'}}, {c: 'Quote', ch: [t('quote')], p: {by: t('by')}}, ], }, {c: 'PlaceLink', ch: [t('place')], p: {place: '53.2190,6.5680'}}, ], p: {heading: t('heading')}, }, ], p: {description: t('description'), title: t('title')}, }, } } describe('the server render', () => { test('draws every component, with its texts', async () => { const used = new Set() const walk = (n: PageData['tree']) => { used.add(n.c) for (const c of n.ch ?? []) if ('c' in c) walk(c) } const data = everything('over') walk(data.tree) expect([...used].sort()).toEqual(Object.keys(registry.components).sort()) const html = await renderPage(data) for (const text of ['Jouw stad, gedeeld', 'Doe mee', 'Praat op de kaart.', 'Eindelijk.', 'Naar de markt']) { expect(html).toContain(text) } // The text that fell back says which language it is in. expect(html).toContain('

Talk and build together.

') expect(html).not.toContain('undefined') expect(html).not.toContain('[object') }) test('escapes what a page says', async () => { const data = everything('over') data.texts.quote = {lang: 'nl', text: ' & "zo"'} const html = await renderPage(data) expect(html).not.toContain('