Trying to test action on specific tab

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);
/* How can i set which tab is active ? */
livewire(ViewOpportunite::class, ['record' => $opportunite->id])
->assertActionExists(\App\Filament\Backend\Resources\Opportunites\Actions\AddMessageAction::class);
5 Replies
Oscar Carvajal Mora
Are these tabs relation managers or schemas?
XOTYND17
XOTYND17OP2w ago
They are in the schema
Oscar Carvajal Mora
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')
]),
]),
]);
}
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')
]),
]),
]);
}
2. 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');
},
);
});
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');
},
);
});
Oscar Carvajal Mora
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;
},
// ...
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;
},
XOTYND17
XOTYND17OP7d ago
When trying your solution, i couldn't check that a tab exists directly. I needed to do it in the checkComponentUsing like this :
it('can add new message', function () {
$prestataire = Prestataire::factory()->create();

$beneficiaire = Beneficiaire::factory()->create([
'prestataire_id' => $prestataire->id
]);

$opportunite = Opportunite::factory()->visible()->create();

$this->actingAs($this->getBackendUser(
['prestataire_id' => $prestataire->id],
RolesEnum::PRESTATAIRE)
);

Candidature::factory()->create([
'beneficiaire_id' => $beneficiaire->id,
'opportunite_id' => $opportunite->id,
]);

livewire(ViewOpportunite::class, ['record' => $opportunite->id])
->assertSchemaComponentExists(
key: 'opportunite-tabs',
checkComponentUsing: function (\Filament\Schemas\Components\Tabs $component) {
$tab = $component->getChildSchema()->getComponent('candidature-1');
$action = $tab->getChildSchema()->getAction('add_message');
return isset($tab, $action) $action->isVisible();
},
);
});
it('can add new message', function () {
$prestataire = Prestataire::factory()->create();

$beneficiaire = Beneficiaire::factory()->create([
'prestataire_id' => $prestataire->id
]);

$opportunite = Opportunite::factory()->visible()->create();

$this->actingAs($this->getBackendUser(
['prestataire_id' => $prestataire->id],
RolesEnum::PRESTATAIRE)
);

Candidature::factory()->create([
'beneficiaire_id' => $beneficiaire->id,
'opportunite_id' => $opportunite->id,
]);

livewire(ViewOpportunite::class, ['record' => $opportunite->id])
->assertSchemaComponentExists(
key: 'opportunite-tabs',
checkComponentUsing: function (\Filament\Schemas\Components\Tabs $component) {
$tab = $component->getChildSchema()->getComponent('candidature-1');
$action = $tab->getChildSchema()->getAction('add_message');
return isset($tab, $action) $action->isVisible();
},
);
});
Thank you nonetheless for the tip.

Did you find this page helpful?