Not possible to test replaceMountedAction()
Package
filament/filament
Package Version
v3.2.115
Laravel Version
v11.26.0
Livewire Version
v3.5.6
PHP Version
8.3.12
Problem description
A very simple test with no assertions is failing. The failure message refers to an assertion which comes from the Filament package itself, not from my test.
Consider this simple test:
<?php
use App\Livewire\Button;
use function \Pest\Livewire\livewire;
it('works properly', function () {
livewire(Button::class)
->mountAction('first');
});
And running php artisan test gives this failure:
FAILED Tests\Feature\ButtonTest > it works properly
Failed asserting that two arrays are equal.
Array (
- 0 => 'first'
+ 0 => 'second'
)
at vendor/livewire/livewire/src/Features/SupportTesting/MakesAssertions.php:110
106▕
107▕ if (! is_string($value) && is_callable($value)) {
108▕ PHPUnit::assertTrue($value($actual));
109▕ } else {
➜ 110▕ $strict ? PHPUnit::assertSame($value, $actual) : PHPUnit::assertEquals($value, $actual);
111▕ }
112▕
113▕ return $this;
114▕ }
+4 vendor frames
5 tests/Feature/ButtonTest.php:9
Tests: 1 failed (1 assertions)
Duration: 0.23s
This is unexpected because I didn't make any assertions. Usually a basic test like this would just be reported as "risky" due to no assetions.
Obviously my intended test is much longer, but the problem is I cannot make any assertions of my own because the test is failing so early as described.
Expected behavior
I expected the test above to pass but be flagged as "risky" because there are no assertions.
I also expected to be able to write a test something like this:
it('works properly', function () {
livewire(Button::class)
->mountAction('first')
->callMountedAction()
->assertActionMounted('second')
});
In other words, I expected to be able to assert that the Filament replaceMountedAction() method is replacing the mounted action as intended.
Steps to reproduce
Set up
- Clone the repo https://github.com/joseph-d/filamentIssue20241007
- No need to migrate as sqlite database is included. Just log in to Filament with
[email protected]/password - Open
app/Livewire/Button.phpandtests/Feature/ButtonTest.php - Keep an eye on the log
tail -f storage/logs/laravel.log
Successful execution within the Filament application itself:
- In Filament, click on the 'Users' resource and click on the first user
- Observe the HTML of the button called
first, specificallywire:click="mountAction('first')" - Click on the button "first" and notice that the modal is from the
secondAction()method ofButton.phpand not from thefirstAction() - Observe log output is
first action - Click on the "Confirm" button
- Observe log output is
second action
Unsuccessful execution when testing:
- Run
php artisan test - Observe log output is
first action - Observe test fails due to assertion as described above
Reproduction repository (issue will be closed if this is not valid)
https://github.com/joseph-d/filamentIssue20241007
Relevant log output
No response
This one is a tricky one to fix, Filament is expecting that after an action is mounted, the Livewire component's current action is the same one that you just mounted, that is an integral assertion that we use to ensure that the mount worked. Not really sure how to fix but open to ideas and PRs.