|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Browser; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\Blade; |
| 6 | +use Illuminate\Support\Facades\Route; |
| 7 | +use Livewire\Component; |
| 8 | +use Livewire\Livewire; |
| 9 | +use PowerComponents\Partials\Attribute\PartialRender; |
| 10 | + |
| 11 | +class PerformanceTestComponent extends Component |
| 12 | +{ |
| 13 | + public int $count = 0; |
| 14 | + |
| 15 | + public array $data = []; |
| 16 | + |
| 17 | + public function mount() |
| 18 | + { |
| 19 | + for ($i = 0; $i < 100; $i++) { |
| 20 | + $this->data[] = "Row $i: ".str_repeat('Massive Data ', 10); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + #[PartialRender('performance-partial', 'count-partial')] |
| 25 | + public function incWithPartial() |
| 26 | + { |
| 27 | + $this->count++; |
| 28 | + } |
| 29 | + |
| 30 | + public function incWithoutPartial() |
| 31 | + { |
| 32 | + $this->count++; |
| 33 | + } |
| 34 | + |
| 35 | + public function render() |
| 36 | + { |
| 37 | + return <<<'BLADE' |
| 38 | + <div> |
| 39 | + <button id="btn-partial" wire:click="incWithPartial">Partial Update</button> |
| 40 | + <button id="btn-full" wire:click="incWithoutPartial">Full Update</button> |
| 41 | + |
| 42 | + <div id="partial-target" wire:partial="count-partial"> |
| 43 | + @include('performance-partial', ['__partial' => $this]) |
| 44 | + </div> |
| 45 | +
|
| 46 | + <div id="large-payload" style="display:none"> |
| 47 | + @foreach($data as $row) |
| 48 | + <p>{{ $row }}</p> |
| 49 | + @endforeach |
| 50 | + </div> |
| 51 | + </div> |
| 52 | +BLADE; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +beforeEach(function () { |
| 57 | + Livewire::component('perf-test-comp', PerformanceTestComponent::class); |
| 58 | + |
| 59 | + $viewPath = __DIR__.'/../views/performance-partial.blade.php'; |
| 60 | + file_put_contents($viewPath, '<div id="partial-root">Count: {{ $__partial->count }} (ID: {{ uniqid() }})</div>'); |
| 61 | + |
| 62 | + Route::get('/perf-test', fn () => Blade::render(' |
| 63 | + <html> |
| 64 | + <head>@livewireStyles</head> |
| 65 | + <body> |
| 66 | + <livewire:perf-test-comp /> |
| 67 | + @livewireScripts |
| 68 | + <script type="module" src="/powergrid-partials/partials.js"></script> |
| 69 | + </body> |
| 70 | + </html> |
| 71 | + '))->middleware('web'); |
| 72 | +}); |
| 73 | + |
| 74 | +it('measures the performance benefit of partial renders', function () { |
| 75 | + $page = $this->visit('/perf-test'); |
| 76 | + |
| 77 | + $page->script(<<<'JS' |
| 78 | + () => { |
| 79 | + window.__lastEffects = null; |
| 80 | + window.__lastPayloadSize = null; |
| 81 | + Livewire.interceptMessage(({ message, onSuccess }) => { |
| 82 | + onSuccess(({ payload }) => { |
| 83 | + window.__lastEffects = payload.effects; |
| 84 | + window.__lastPayloadSize = JSON.stringify(payload).length; |
| 85 | + }); |
| 86 | + }); |
| 87 | + } |
| 88 | + JS); |
| 89 | + |
| 90 | + // 1. Partial Render |
| 91 | + $page->click('#btn-partial')->waitForEvent('networkidle'); |
| 92 | + $page->assertScript('() => window.__lastEffects !== null'); |
| 93 | + $partialSize = (int) $page->script('() => window.__lastPayloadSize'); |
| 94 | + |
| 95 | + // Check that partial payload doesn't have the full HTML in effects |
| 96 | + $hasFullHtmlInPartial = $page->script('() => !!window.__lastEffects.html'); |
| 97 | + expect($hasFullHtmlInPartial)->toBeFalse('Partial should NOT return full html'); |
| 98 | + |
| 99 | + // Reset |
| 100 | + $page->script('() => { window.__lastEffects = null; window.__lastPayloadSize = null; }'); |
| 101 | + |
| 102 | + // 2. Full Render |
| 103 | + $page->click('#btn-full')->waitForEvent('networkidle'); |
| 104 | + $page->assertScript('() => window.__lastEffects !== null'); |
| 105 | + $fullSize = (int) $page->script('() => window.__lastPayloadSize'); |
| 106 | + |
| 107 | + // Check that full payload DOES have the full HTML |
| 108 | + $hasFullHtmlInFull = $page->script('() => !!window.__lastEffects.html'); |
| 109 | + expect($hasFullHtmlInFull)->toBeTrue('Full render should return full html') |
| 110 | + ->and($partialSize)->toBeGreaterThan(10) |
| 111 | + ->and($fullSize)->toBeGreaterThan($partialSize); |
| 112 | +}); |
0 commit comments