|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Support\Facades\View; |
| 4 | + |
| 5 | +describe('unblaze directive', function () { |
| 6 | + beforeEach(function () { |
| 7 | + // Configure Blade to find our test components |
| 8 | + app('blade.compiler')->anonymousComponentNamespace('', 'x'); |
| 9 | + app('blade.compiler')->anonymousComponentPath(__DIR__ . '/fixtures/components'); |
| 10 | + |
| 11 | + // Add view path for our test pages |
| 12 | + View::addLocation(__DIR__ . '/fixtures/pages'); |
| 13 | + }); |
| 14 | + |
| 15 | + it('folds component but preserves unblaze block', function () { |
| 16 | + $input = '<x-with-unblaze />'; |
| 17 | + |
| 18 | + $compiled = app('blaze')->compile($input); |
| 19 | + |
| 20 | + // The component should be folded |
| 21 | + expect($compiled)->toContain('<div class="container">'); |
| 22 | + expect($compiled)->toContain('<h1>Static Header</h1>'); |
| 23 | + expect($compiled)->toContain('<footer>Static Footer</footer>'); |
| 24 | + |
| 25 | + // The unblaze block should be preserved as dynamic content |
| 26 | + expect($compiled)->toContain('{{ $dynamicValue }}'); |
| 27 | + expect($compiled)->not->toContain('<x-with-unblaze'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('preserves unblaze content without folding the dynamic parts', function () { |
| 31 | + $input = '<x-with-unblaze />'; |
| 32 | + |
| 33 | + $compiled = app('blaze')->compile($input); |
| 34 | + |
| 35 | + // Static parts should be folded |
| 36 | + expect($compiled)->toContain('Static Header'); |
| 37 | + expect($compiled)->toContain('Static Footer'); |
| 38 | + |
| 39 | + // Dynamic parts inside @unblaze should remain dynamic |
| 40 | + expect($compiled)->toContain('$dynamicValue'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('handles unblaze with scope parameter', function () { |
| 44 | + $input = '<?php $message = "Hello World"; ?> <x-with-unblaze-scope :message="$message" />'; |
| 45 | + |
| 46 | + $compiled = app('blaze')->compile($input); |
| 47 | + |
| 48 | + // The component should be folded |
| 49 | + expect($compiled)->toContain('<div class="wrapper">'); |
| 50 | + expect($compiled)->toContain('<h2>Title</h2>'); |
| 51 | + expect($compiled)->toContain('<p>Static paragraph</p>'); |
| 52 | + |
| 53 | + // The scope should be captured and made available |
| 54 | + expect($compiled)->toContain('$scope'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('encodes scope into compiled view for runtime access', function () { |
| 58 | + $input = '<?php $message = "Test Message"; ?> <x-with-unblaze-scope :message="$message" />'; |
| 59 | + |
| 60 | + $compiled = app('blaze')->compile($input); |
| 61 | + |
| 62 | + // Should contain PHP code to set up the scope |
| 63 | + expect($compiled)->toMatch('/\$scope\s*=\s*array\s*\(/'); |
| 64 | + |
| 65 | + // The dynamic content should reference $scope |
| 66 | + expect($compiled)->toContain('$scope[\'message\']'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('renders unblaze component correctly at runtime', function () { |
| 70 | + $template = '<?php $message = "Runtime Test"; ?> <x-with-unblaze-scope :message="$message" />'; |
| 71 | + |
| 72 | + $rendered = \Illuminate\Support\Facades\Blade::render($template); |
| 73 | + |
| 74 | + // Static content should be present |
| 75 | + expect($rendered)->toContain('<div class="wrapper">'); |
| 76 | + expect($rendered)->toContain('<h2>Title</h2>'); |
| 77 | + expect($rendered)->toContain('<p>Static paragraph</p>'); |
| 78 | + |
| 79 | + // Dynamic content should be rendered with the scope value |
| 80 | + // Note: The actual rendering of scope variables happens at runtime |
| 81 | + expect($rendered)->toContain('class="dynamic"'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('allows punching a hole in static component for dynamic section', function () { |
| 85 | + $input = <<<'BLADE' |
| 86 | +<x-card> |
| 87 | + Static content here |
| 88 | + @unblaze |
| 89 | + <span>{{ $dynamicValue }}</span> |
| 90 | + @endunblaze |
| 91 | + More static content |
| 92 | +</x-card> |
| 93 | +BLADE; |
| 94 | + |
| 95 | + $compiled = app('blaze')->compile($input); |
| 96 | + |
| 97 | + // Card should be folded |
| 98 | + expect($compiled)->toContain('<div class="card">'); |
| 99 | + expect($compiled)->toContain('Static content here'); |
| 100 | + expect($compiled)->toContain('More static content'); |
| 101 | + |
| 102 | + // But dynamic part should be preserved |
| 103 | + expect($compiled)->toContain('{{ $dynamicValue }}'); |
| 104 | + expect($compiled)->not->toContain('<x-card>'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('supports multiple unblaze blocks in same component', function () { |
| 108 | + $template = <<<'BLADE' |
| 109 | +@blaze |
| 110 | +<div> |
| 111 | + <p>Static 1</p> |
| 112 | + @unblaze |
| 113 | + <span>{{ $dynamic1 }}</span> |
| 114 | + @endunblaze |
| 115 | + <p>Static 2</p> |
| 116 | + @unblaze |
| 117 | + <span>{{ $dynamic2 }}</span> |
| 118 | + @endunblaze |
| 119 | + <p>Static 3</p> |
| 120 | +</div> |
| 121 | +BLADE; |
| 122 | + |
| 123 | + $compiled = app('blaze')->compile($template); |
| 124 | + |
| 125 | + // All static parts should be folded |
| 126 | + expect($compiled)->toContain('<p>Static 1</p>'); |
| 127 | + expect($compiled)->toContain('<p>Static 2</p>'); |
| 128 | + expect($compiled)->toContain('<p>Static 3</p>'); |
| 129 | + |
| 130 | + // Both dynamic parts should be preserved |
| 131 | + expect($compiled)->toContain('{{ $dynamic1 }}'); |
| 132 | + expect($compiled)->toContain('{{ $dynamic2 }}'); |
| 133 | + }); |
| 134 | + |
| 135 | + it('handles nested components with unblaze', function () { |
| 136 | + $input = <<<'BLADE' |
| 137 | +<x-card> |
| 138 | + <x-button>Static Button</x-button> |
| 139 | + @unblaze |
| 140 | + <x-button>{{ $dynamicLabel }}</x-button> |
| 141 | + @endunblaze |
| 142 | +</x-card> |
| 143 | +BLADE; |
| 144 | + |
| 145 | + $compiled = app('blaze')->compile($input); |
| 146 | + |
| 147 | + // Outer card and static button should be folded |
| 148 | + expect($compiled)->toContain('<div class="card">'); |
| 149 | + expect($compiled)->toContain('Static Button'); |
| 150 | + |
| 151 | + // Dynamic button inside unblaze should be preserved |
| 152 | + expect($compiled)->toContain('{{ $dynamicLabel }}'); |
| 153 | + }); |
| 154 | + |
| 155 | + it('static folded content with random strings stays the same between renders', function () { |
| 156 | + // First render |
| 157 | + $render1 = \Illuminate\Support\Facades\Blade::render('<x-random-static />'); |
| 158 | + |
| 159 | + // Second render |
| 160 | + $render2 = \Illuminate\Support\Facades\Blade::render('<x-random-static />'); |
| 161 | + |
| 162 | + // The random string should be the same because it was folded at compile time |
| 163 | + expect($render1)->toBe($render2); |
| 164 | + |
| 165 | + // Verify it contains the static structure |
| 166 | + expect($render1)->toContain('class="static-component"'); |
| 167 | + expect($render1)->toContain('This should be folded and not change between renders'); |
| 168 | + }); |
| 169 | + |
| 170 | + it('unblazed dynamic content changes between renders while static parts stay the same', function () { |
| 171 | + // First render with a value |
| 172 | + $render1 = \Illuminate\Support\Facades\Blade::render('<x-mixed-random />', ['dynamicValue' => 'first-value']); |
| 173 | + |
| 174 | + // Second render with a different value |
| 175 | + $render2 = \Illuminate\Support\Facades\Blade::render('<x-mixed-random />', ['dynamicValue' => 'second-value']); |
| 176 | + |
| 177 | + // Extract the static parts (header and footer with random strings) |
| 178 | + preg_match('/<h1>Static Random: (.+?)<\/h1>/', $render1, $matches1); |
| 179 | + preg_match('/<h1>Static Random: (.+?)<\/h1>/', $render2, $matches2); |
| 180 | + $staticRandom1 = $matches1[1] ?? ''; |
| 181 | + $staticRandom2 = $matches2[1] ?? ''; |
| 182 | + |
| 183 | + // The static random strings should be IDENTICAL (folded at compile time) |
| 184 | + expect($staticRandom1)->toBe($staticRandom2); |
| 185 | + expect($staticRandom1)->not->toBeEmpty(); |
| 186 | + |
| 187 | + // But the dynamic parts should be DIFFERENT |
| 188 | + expect($render1)->toContain('Dynamic value: first-value'); |
| 189 | + expect($render2)->toContain('Dynamic value: second-value'); |
| 190 | + expect($render1)->not->toContain('second-value'); |
| 191 | + expect($render2)->not->toContain('first-value'); |
| 192 | + }); |
| 193 | + |
| 194 | + it('multiple renders of unblaze component proves folding optimization', function () { |
| 195 | + // Render the same template multiple times with different dynamic values |
| 196 | + $renders = []; |
| 197 | + foreach (['one', 'two', 'three'] as $value) { |
| 198 | + $renders[] = \Illuminate\Support\Facades\Blade::render( |
| 199 | + '<x-mixed-random />', |
| 200 | + ['dynamicValue' => $value] |
| 201 | + ); |
| 202 | + } |
| 203 | + |
| 204 | + // Extract the static footer random string from each render |
| 205 | + $staticFooters = []; |
| 206 | + foreach ($renders as $render) { |
| 207 | + preg_match('/<footer>Static Footer: (.+?)<\/footer>/', $render, $matches); |
| 208 | + $staticFooters[] = $matches[1] ?? ''; |
| 209 | + } |
| 210 | + |
| 211 | + // All static random strings should be IDENTICAL (proving they were folded) |
| 212 | + expect($staticFooters[0])->toBe($staticFooters[1]); |
| 213 | + expect($staticFooters[1])->toBe($staticFooters[2]); |
| 214 | + expect($staticFooters[0])->not->toBeEmpty(); |
| 215 | + |
| 216 | + // But each render should have its unique dynamic value |
| 217 | + expect($renders[0])->toContain('Dynamic value: one'); |
| 218 | + expect($renders[1])->toContain('Dynamic value: two'); |
| 219 | + expect($renders[2])->toContain('Dynamic value: three'); |
| 220 | + }); |
| 221 | +}); |
0 commit comments