Skip to content

Not possible to test replaceMountedAction()  #14455

@joseph-d

Description

@joseph-d

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

  1. Clone the repo https://github.com/joseph-d/filamentIssue20241007
  2. No need to migrate as sqlite database is included. Just log in to Filament with [email protected] / password
  3. Open app/Livewire/Button.php and tests/Feature/ButtonTest.php
  4. Keep an eye on the log tail -f storage/logs/laravel.log

Successful execution within the Filament application itself:

  1. In Filament, click on the 'Users' resource and click on the first user
  2. Observe the HTML of the button called first, specifically wire:click="mountAction('first')"
  3. Click on the button "first" and notice that the modal is from the secondAction() method of Button.php and not from the firstAction()
  4. Observe log output is first action
  5. Click on the "Confirm" button
  6. Observe log output is second action

Unsuccessful execution when testing:

  1. Run php artisan test
  2. Observe log output is first action
  3. 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

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    Status

    Todo

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions