|
| 1 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +import { |
| 4 | + finalizeAddOns, |
| 5 | + populateAddOnOptionsDefaults, |
| 6 | +} from '../src/add-ons.js' |
| 7 | +import { createApp } from '../src/create-app.js' |
| 8 | +import { createMemoryEnvironment } from '../src/environment.js' |
| 9 | +import { createFrameworkDefinition } from '../src/frameworks/react/index.js' |
| 10 | + |
| 11 | +import type { Framework, FrameworkDefinition, Options } from '../src/types.js' |
| 12 | + |
| 13 | +function frameworkFromDefinition(definition: FrameworkDefinition): Framework { |
| 14 | + const { addOns, base, ...framework } = definition |
| 15 | + |
| 16 | + return { |
| 17 | + ...framework, |
| 18 | + getFiles: () => Promise.resolve(Object.keys(base)), |
| 19 | + getFileContents: (path: string) => Promise.resolve(base[path]), |
| 20 | + getDeletedFiles: () => Promise.resolve([]), |
| 21 | + getAddOns: () => addOns, |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +afterEach(() => { |
| 26 | + vi.unstubAllGlobals() |
| 27 | +}) |
| 28 | + |
| 29 | +describe('Paraglide add-on', () => { |
| 30 | + it('configures localized routing and request-scoped SSR', async () => { |
| 31 | + vi.stubGlobal( |
| 32 | + 'fetch', |
| 33 | + vi.fn( |
| 34 | + async () => |
| 35 | + new Response(JSON.stringify({ version: '1.0.0' }), { status: 200 }), |
| 36 | + ), |
| 37 | + ) |
| 38 | + |
| 39 | + const framework = frameworkFromDefinition(createFrameworkDefinition()) |
| 40 | + const chosenAddOns = await finalizeAddOns(framework, 'file-router', [ |
| 41 | + 'paraglide', |
| 42 | + ]) |
| 43 | + const options = { |
| 44 | + projectName: 'paraglide-app', |
| 45 | + targetDir: '/paraglide-app', |
| 46 | + framework, |
| 47 | + mode: 'file-router', |
| 48 | + typescript: true, |
| 49 | + tailwind: true, |
| 50 | + packageManager: 'pnpm', |
| 51 | + git: false, |
| 52 | + install: false, |
| 53 | + intent: false, |
| 54 | + includeExamples: true, |
| 55 | + chosenAddOns, |
| 56 | + addOnOptions: populateAddOnOptionsDefaults(chosenAddOns), |
| 57 | + } as Options |
| 58 | + const { environment, output } = createMemoryEnvironment() |
| 59 | + |
| 60 | + await createApp(environment, options) |
| 61 | + |
| 62 | + const router = output.files['/paraglide-app/src/router.tsx'] |
| 63 | + expect(router).toContain( |
| 64 | + "import { deLocalizeUrl, localizeUrl } from './paraglide/runtime'", |
| 65 | + ) |
| 66 | + expect(router).toContain('input: ({ url }) => deLocalizeUrl(url)') |
| 67 | + expect(router).toContain('output: ({ url }) => localizeUrl(url)') |
| 68 | + |
| 69 | + const server = output.files['/paraglide-app/src/server.ts'] |
| 70 | + expect(server).toContain( |
| 71 | + "import { paraglideMiddleware } from './paraglide/server.js'", |
| 72 | + ) |
| 73 | + expect(server).toContain( |
| 74 | + "import handler from '@tanstack/react-start/server-entry'", |
| 75 | + ) |
| 76 | + expect(server).toContain( |
| 77 | + 'return paraglideMiddleware(req, () => handler.fetch(req))', |
| 78 | + ) |
| 79 | + }) |
| 80 | +}) |
0 commit comments