Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Exceptions/InvalidPureUsageException.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,13 @@ public static function forOld(string $componentPath): self
);
}

public static function forOnce(string $componentPath): self
{
return new self(
$componentPath,
'@once',
'Components with @once should not use @pure as @once maintains runtime state'
);
}

}
1 change: 1 addition & 0 deletions src/Folder/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ protected function validatePureComponent(string $source, string $componentPath):
{
$problematicPatterns = [
'@aware' => 'forAware',
'@once' => 'forOnce',
'\\$errors' => 'forErrors',
'session\\(' => 'forSession',
'@error\\(' => 'forError',
Expand Down
4 changes: 2 additions & 2 deletions tests/BlazeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
});

it('throws exception for components with invalid pure usage', function () {
expect(fn() => \Illuminate\Support\Facades\Blade::render('<x-invalid-pure>Test</x-invalid-pure>'))
expect(fn() => \Illuminate\Support\Facades\Blade::render('<x-invalid-pure.errors>Test</x-invalid-pure.errors>'))
->toThrow(\Livewire\Blaze\Exceptions\InvalidPureUsageException::class);
});

Expand All @@ -66,4 +66,4 @@
expect($rendered)->not->toContain('<x-card>');
});

});
});
26 changes: 19 additions & 7 deletions tests/FoldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,31 @@ function compile(string $input): string {
expect(compile($input))->toBe($output);
});

it('throws exception for invalid @pure usage', function () {
// Test the folder validation directly
it('throws exception for invalid pure usage with $pattern', function (string $pattern, string $expectedPattern) {
$folder = app('blaze')->folder();
$componentNode = new \Livewire\Blaze\Nodes\ComponentNode('invalid-pure', 'x', '', [], false);
$componentNode = new \Livewire\Blaze\Nodes\ComponentNode("invalid-pure.{$pattern}", 'x', '', [], false);

expect(fn() => $folder->fold($componentNode))
->toThrow(\Livewire\Blaze\Exceptions\InvalidPureUsageException::class);

try {
$folder->fold($componentNode);
expect(false)->toBeTrue('Exception should have been thrown');
} catch (\Livewire\Blaze\Exceptions\InvalidPureUsageException $e) {
expect($e->getMessage())->toContain('Invalid @pure usage');
expect($e->getComponentPath())->toContain('invalid-pure.blade.php');
expect($e->getProblematicPattern())->toBe('\\$errors');
expect($e->getComponentPath())->toContain("invalid-pure/{$pattern}.blade.php");
expect($e->getProblematicPattern())->toBe($expectedPattern);
}
});
})->with([
['aware', '@aware'],
['errors', '\\$errors'],
['session', 'session\\('],
['error', '@error\\('],
['csrf', '@csrf'],
['auth', 'auth\\(\\)'],
['request', 'request\\(\\)'],
['old', 'old\\('],
['once', '@once'],
]);

it('named slots', function () {
$input = '<x-modal>
Expand Down Expand Up @@ -222,4 +233,5 @@ function compile(string $input): string {

expect(compile($input))->toBe($output);
});

});
5 changes: 5 additions & 0 deletions tests/fixtures/components/invalid-pure/auth.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@pure

<div>
Welcome, {{ auth()->user()->name }}
</div>
7 changes: 7 additions & 0 deletions tests/fixtures/components/invalid-pure/aware.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@pure

@aware(['variant' => 'default'])

<div class="item item-{{ $variant }}">
{{ $slot }}
</div>
6 changes: 6 additions & 0 deletions tests/fixtures/components/invalid-pure/csrf.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@pure

<form method="POST">
@csrf
{{ $slot }}
</form>
9 changes: 9 additions & 0 deletions tests/fixtures/components/invalid-pure/error.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@pure

@props(['name'])

<div>
@error($name)
<span class="error">{{ $message }}</span>
@enderror
</div>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@pure

<div class="{{ $errors->has('name') ? 'error' : '' }}">
{{ $slot }}
</div>
</div>
3 changes: 3 additions & 0 deletions tests/fixtures/components/invalid-pure/old.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@pure

<input type="text" name="email" value="{{ old('email') }}" />
10 changes: 10 additions & 0 deletions tests/fixtures/components/invalid-pure/once.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@pure

@props(['title'])

<div class="test-component">
<h1>{{ $title }}</h1>
@once
<script>console.log('This should only run once');</script>
@endonce
</div>
5 changes: 5 additions & 0 deletions tests/fixtures/components/invalid-pure/request.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@pure

<div>
Current URL: {{ request()->url() }}
</div>
5 changes: 5 additions & 0 deletions tests/fixtures/components/invalid-pure/session.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@pure

<div class="message">
{{ session('message') }}
</div>