also looking at this
fix(layouts): fill @parent in a layout that extends another layout
#2103
2 files
+88
-1
| @@ -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 | */ | |
| 13 | import fs from 'node:fs' | |
| 14 | import path from 'node:path' | |
| 15 | import { afterAll, beforeAll, describe, expect, it } from 'bun:test' | |
| 16 | import { processDirectives } from '../../src/process' | |
| 17 | import { createTempDir } from '../../src/temp-dir' | |
| 18 | import type { StxOptions } from '../../src/types' | |
| 19 | ||
| 20 | const tmp = createTempDir('nested-layout-parent') | |
| 21 | const opts: StxOptions = { debug: false, componentsDir: 'components', layoutsDir: tmp } | |
| 22 | ||
| 23 | const 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 | ||
| 33 | beforeAll(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 | }) | |
| 38 | afterAll(async () => { await fs.promises.rm(tmp, { recursive: true, force: true }) }) | |
| 39 | ||
| 40 | async 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 | ||
| 45 | describe('@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 | }) | |