diff --git a/README.md b/README.md index fc26715..adfd1a0 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,15 @@ It looks for a string like `wire:model="email"` in your component's view file. I Livewire::test(FeedbackForm::class) ->assertMethodWired('submit'); ``` +It looks for a string like `wire:click="submit"` in your component's view file. + +### Check if a Livewire magic action is wired to an HTML field +```php +Livewire::test(FeedbackForm::class) + ->assertMethodWired('$toggle(\'sortAsc\')'); +``` + +It looks for a string like `wire:click="$refresh"`, `wire:click="$toggle('sortAsc')`, `$dispatch('post-created')`, along with all other [magic actions](https://livewire.laravel.com/docs/actions#magic-actions). When testing for magic actions, you must escape single quotes like shown above. ### Check if a Livewire method is wired to an HTML form diff --git a/src/CustomLivewireAssertionsMixin.php b/src/CustomLivewireAssertionsMixin.php index 984981f..6c8069c 100644 --- a/src/CustomLivewireAssertionsMixin.php +++ b/src/CustomLivewireAssertionsMixin.php @@ -93,7 +93,7 @@ public function assertMethodWired(): Closure { return function (string $method) { PHPUnit::assertMatchesRegularExpression( - '/wire:click(\.(prevent))*=(?"|\')'.$method.'(\s*\(.+\)\s*)?\s*(\k\'q\')/', + '/wire:click(\.(prevent))?=(?"|\')'.preg_quote($method).'(\s*\(.+\)\s*)?\s*(\k\'q\')/', $this->html() ); @@ -108,7 +108,7 @@ public function assertMethodNotWired(): Closure { return function (string $method) { PHPUnit::assertDoesNotMatchRegularExpression( - '/wire:click(\.(prevent))*=(?"|\')'.$method.'(\s*\(.+\)\s*)?\s*(\k\'q\')/', + '/wire:click(\.(prevent))?=(?"|\')'.preg_quote($method).'(\s*\(.+\)\s*)?\s*(\k\'q\')/', $this->html() ); diff --git a/tests/AssertionsTest.php b/tests/AssertionsTest.php index 04db3cd..de56946 100644 --- a/tests/AssertionsTest.php +++ b/tests/AssertionsTest.php @@ -75,7 +75,14 @@ public function it_checks_if_livewire_method_is_wired_to_a_field(): void Livewire::test(LivewireTestComponentA::class) ->assertMethodWired('prevent') ->assertMethodWired('submit') - ->assertMethodWired('singlequote'); + ->assertMethodWired('singlequote') + ->assertMethodWired('$refresh') + ->assertMethodWired('$toggle(\'sortAsc\')') + ->assertMethodWired('$dispatch(\'post-created\')') + ->assertMethodWired('search($event.target.value)') + ->assertMethodWired('$wire.$refresh()') + ->assertMethodWired('$parent.removePost({{ $post->id }})') + ->assertMethodWired('$set(\'query\', \'\')'); } /** @test * */ diff --git a/tests/resources/views/livewire-test-component-a.php b/tests/resources/views/livewire-test-component-a.php index 405efc0..778ff42 100644 --- a/tests/resources/views/livewire-test-component-a.php +++ b/tests/resources/views/livewire-test-component-a.php @@ -12,6 +12,13 @@ test + + + + + + +