ReviewOS

also looking at this

stacks/stx

fix(layouts): fill @parent in a layout that extends another layout

#2103
Open mkantautas wants to merge fix/nested-layout-parent into main
2 files +88 -1
packages/stx/test/directives/nested-layout-parent.test.tsadded+71-0
Changes to packages/stx/test/directives/nested-layout-parent.test.ts
@@ -0,0 +1,71 @@
1/**
2 * `@parent` in a layout that extends another layout.
3 *
4 * A page's `@parent` is marked when its sections are extracted, so the layout
5 * splices its own section in there. The sections of an intermediate layout
6 * were stored with `@parent` left as text: the next layout never filled it,
7 * its own section content was lost, and the page rendered the literal
8 * `@parent`.
9 *
10 * Rendered via the src `processDirectives` (not Bun.build) so it exercises the
11 * fix, not a stale dist.
12 */
13import fs from 'node:fs'
14import path from 'node:path'
15import { afterAll, beforeAll, describe, expect, it } from 'bun:test'
16import { processDirectives } from '../../src/process'
17import { createTempDir } from '../../src/temp-dir'
18import type { StxOptions } from '../../src/types'
19
20const tmp = createTempDir('nested-layout-parent')
21const opts: StxOptions = { debug: false, componentsDir: 'components', layoutsDir: tmp }
22
23const layouts: Record<string, string> = {
24 'base.stx': `<nav>@section('nav')BASE@show</nav><main>@yield('content')</main>`,
25 'mid.stx': `@extends('base')@section('nav')MID @parent@endsection`,
26 'top.stx': `@extends('mid')@section('nav')TOP @parent@endsection`,
27 'bare.stx': `<nav>@yield('nav')</nav><main>@yield('content')</main>`,
28 'mid-bare.stx': `@extends('bare')@section('nav')MID @parent@endsection`,
29 'mid-text.stx': `@extends('base')@section('nav')MID @@parent a@parents.com <!-- @parent -->@endsection`,
30 'mid-commented.stx': `@extends('base')@section('nav')<!-- @parent -->{⁠{-- @parent --}}MID @parent@endsection`,
31}
32
33beforeAll(async () => {
34 await fs.promises.mkdir(tmp, { recursive: true })
35 for (const [name, content] of Object.entries(layouts))
36 await fs.promises.writeFile(path.join(tmp, name), content)
37})
38afterAll(async () => { await fs.promises.rm(tmp, { recursive: true, force: true }) })
39
40async function nav(view: string): Promise<string> {
41 const out = await processDirectives(`${view}@section('content')C@endsection`, {}, path.join(tmp, 'page.stx'), opts, new Set())
42 return out.match(/<nav>([\s\S]*?)<\/nav>/)?.[1] ?? out
43}
44
45describe('@parent in an intermediate layout', () => {
46 it('splices the parent layout\'s section into the intermediate layout\'s', async () => {
47 expect(await nav(`@extends('mid')@section('nav')PAGE @parent@endsection`)).toBe('PAGE MID BASE')
48 expect(await nav(`@extends('mid')`)).toBe('MID BASE')
49 })
50
51 it('carries through three levels', async () => {
52 expect(await nav(`@extends('top')@section('nav')PAGE @parent@endsection`)).toBe('PAGE TOP MID BASE')
53 expect(await nav(`@extends('top')`)).toBe('TOP MID BASE')
54 })
55
56 it('renders nothing for @parent when the parent layout has no such section', async () => {
57 expect(await nav(`@extends('mid-bare')`)).toBe('MID ')
58 })
59
60 it('still lets the page replace the section without @parent', async () => {
61 expect(await nav(`@extends('mid')@section('nav')PAGE@endsection`)).toBe('PAGE')
62 })
63
64 it('leaves @@parent, a word starting with @parent and a commented @parent as text', async () => {
65 expect(await nav(`@extends('mid-text')`)).toBe('MID @@parent a@parents.com <!-- @parent -->')
66 })
67
68 it('fills the @parent that follows a commented one', async () => {
69 expect(await nav(`@extends('mid-commented')`)).toBe('<!-- @parent -->MID BASE')
70 })
71})