#Trying to test action on specific tab

6 messages · Page 1 of 1 (latest)

vapid ledge
#

I have multiple Tab in my ViewRecord page and i want to test if an action is exists on this specific tab. How can i set the active tab using the livewire() function?

    /* How can i set which tab is active ? */
    livewire(ViewOpportunite::class, ['record' => $opportunite->id])
        ->assertActionExists(\App\Filament\Backend\Resources\Opportunites\Actions\AddMessageAction::class);
kind gulchBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

ruby dawn
#

Are these tabs relation managers or schemas?

vapid ledge
#

They are in the schema

ruby dawn
#

Following this testing docs section, I came to this:

  1. Add a key() to the tab that you want to test in order to make it accesible by the test itself:
public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Tabs::make('My tabs')
                    ->tabs([
                        Tab::make('My tab')
                            ->key('my-tab') // give a key to the tab
                            ->schema([
                                Action::make('myCustomAction')
                            ]),
                    ]),
            ]);
    }
  1. Now, you can point to that specific tab and check with checkComponentUsing if there is the action that you expect:
it('has myCustomAction inside My Tab', function () {
    // Arrange - create a record from your model factory
    $record = Model::factory()->create();

    // Act - render the view with the recenlty created record
    $component = livewire(ViewAsset::class, [
        'record' => $record->id, 
    ])
    // Assert - here you can use a custom function to evaluate certain conditions
        ->assertSchemaComponentExists(
            key: 'my-tab', 
            checkComponentUsing: function (Tab $tab): bool {
                return (bool) $tab->getChildSchema()->getAction('myCustomAction');
            },
        );
});
#

Also, inside the checkComponentUsing you can do a lot of validations against your action. For example, you can test if is the expected action has a specific label, url, etc. Just an example:

// ...
checkComponentUsing: function (Tabs\Tab $tab): bool {
    $myCustomAction = $tab->getChildSchema()->getAction('myCustomAction');

    if (
        $myCustomAction->getLabel() === 'My custom action'
        && $myCustomAction->getUrl() === '/my-custom-action'
        && ...
    ) {
        return true;
    }

    return false;
},