Skip to content

Commit

Permalink
Merge pull request #27 from jonjakoblich/production
Browse files Browse the repository at this point in the history
Adds support for magic actions
  • Loading branch information
christophrumpel committed Dec 16, 2023
2 parents 29247ea + 11f9836 commit 4c66eea
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/CustomLivewireAssertionsMixin.php
Expand Up @@ -93,7 +93,7 @@ public function assertMethodWired(): Closure
{
return function (string $method) {
PHPUnit::assertMatchesRegularExpression(
'/wire:click(\.(prevent))*=(?<q>"|\')'.$method.'(\s*\(.+\)\s*)?\s*(\k\'q\')/',
'/wire:click(\.(prevent))?=(?<q>"|\')'.preg_quote($method).'(\s*\(.+\)\s*)?\s*(\k\'q\')/',
$this->html()
);

Expand All @@ -108,7 +108,7 @@ public function assertMethodNotWired(): Closure
{
return function (string $method) {
PHPUnit::assertDoesNotMatchRegularExpression(
'/wire:click(\.(prevent))*=(?<q>"|\')'.$method.'(\s*\(.+\)\s*)?\s*(\k\'q\')/',
'/wire:click(\.(prevent))?=(?<q>"|\')'.preg_quote($method).'(\s*\(.+\)\s*)?\s*(\k\'q\')/',
$this->html()
);

Expand Down
9 changes: 8 additions & 1 deletion tests/AssertionsTest.php
Expand Up @@ -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 * */
Expand Down
7 changes: 7 additions & 0 deletions tests/resources/views/livewire-test-component-a.php
Expand Up @@ -12,6 +12,13 @@
<x-button wire:click='singlequote' />
<x-button wire:click="params({{$prop}}, 42)" />
<a href="/test" wire:click.prevent="preventParams({{$prop}}, 42)">test</a>
<x-button wire:click="$refresh" />
<x-button wire:click="$toggle('sortAsc')" />
<x-button wire:click="$dispatch('post-created')" />
<x-button wire:click="search($event.target.value)" />
<x-button wire:click="$wire.$refresh()" />
<x-button wire:click="$parent.removePost({{ $post->id }})" />
<x-button wire:click="$set('query', '')" />
<select wire:change="setSelector($event.target.value)">
<option value="one">One</option>
Expand Down

0 comments on commit 4c66eea

Please sign in to comment.