import { describe, expect, test } from 'vitest' import { render } from '@solidjs/testing-library' import { HeadContent, RouterProvider, createMemoryHistory, createRootRoute, createRoute, createRouter, } from '../src' import { Scripts } from '../src/Scripts' describe('ssr scripts', () => { test('it works', async () => { const rootRoute = createRootRoute({ // loader: () => new Promise((r) => setTimeout(r, 1)), head: () => { return { scripts: [ { src: 'script.js', }, { src: 'script2.js', }, ], } }, component: () => { return }, }) const indexRoute = createRoute({ path: '/', getParentRoute: () => rootRoute, // loader: () => new Promise((r) => setTimeout(r, 2)), head: () => { return { scripts: [ { src: 'script3.js', }, ], } }, }) const router = createRouter({ history: createMemoryHistory({ initialEntries: ['/'], }), routeTree: rootRoute.addChildren([indexRoute]), }) router.isServer = true await router.load() expect(router.state.matches.map((d) => d.headScripts).flat(1)).toEqual([ { src: 'script.js' }, { src: 'script2.js' }, { src: 'script3.js' }, ]) }) test('excludes `undefined` script values', async () => { const rootRoute = createRootRoute({ scripts: () => [ { src: 'script.js' }, undefined, // 'script2.js' opted out by certain conditions, such as `NODE_ENV=production`. ], component: () => { return }, }) const indexRoute = createRoute({ path: '/', getParentRoute: () => rootRoute, scripts: () => [{ src: 'script3.js' }], }) const router = createRouter({ history: createMemoryHistory({ initialEntries: ['/'], }), routeTree: rootRoute.addChildren([indexRoute]), }) router.isServer = true await router.load() expect(router.state.matches.map((d) => d.scripts).flat(1)).toEqual([ { src: 'script.js' }, undefined, { src: 'script3.js' }, ]) const { container } = render(() => ) expect(container.innerHTML).toEqual( ``, ) }) }) describe('ssr HeadContent', () => { test('derives title, dedupes meta, and allows non-loader HeadContent', async () => { const rootRoute = createRootRoute({ loader: () => new Promise((r) => setTimeout(r, 1)).then(() => ({ description: 'Root', })), head: ({ loaderData }) => { return { meta: [ { title: 'Root', }, { name: 'description', content: loaderData?.description, }, { name: 'image', content: 'image.jpg', }, { property: 'og:image', content: 'root-image.jpg', }, { property: 'og:description', content: 'Root description', }, ], } }, component: () => { return }, }) const indexRoute = createRoute({ path: '/', getParentRoute: () => rootRoute, loader: () => new Promise((r) => setTimeout(r, 2)).then(() => ({ description: 'Index', })), head: ({ loaderData }) => { return { meta: [ { title: 'Index', }, { name: 'description', content: loaderData?.description, }, { name: 'last-modified', content: '2021-10-10', }, { property: 'og:image', content: 'index-image.jpg', }, ], } }, }) const router = createRouter({ history: createMemoryHistory({ initialEntries: ['/'], }), routeTree: rootRoute.addChildren([indexRoute]), }) router.isServer = true await router.load() expect(router.state.matches.map((d) => d.meta).flat(1)).toEqual([ { title: 'Root' }, { name: 'description', content: 'Root' }, { name: 'image', content: 'image.jpg' }, { property: 'og:image', content: 'root-image.jpg' }, { property: 'og:description', content: 'Root description' }, { title: 'Index' }, { name: 'description', content: 'Index' }, { name: 'last-modified', content: '2021-10-10' }, { property: 'og:image', content: 'index-image.jpg' }, ]) }) })