Joseph Kerkhof
Joseph Kerkhof
FFilament
Created by Joseph Kerkhof on 9/4/2024 in #❓┊help
Testing a Table ToggleColumn
I have the following in a relation manager:
class SchoolConsentRelationManager extends RelationManager
{
public function table(Table $table): Table
{
return $table
->columns([
ToggleColumn::make('status')
->label('Opted In')
->disabled(function (School $school) {
return ! $school->is_enabled;
})
->getStateUsing(function (School $record) {
return $record->status === OptedInStatusEnum::OPTED_IN;
})
->updateStateUsing(function (School $record, bool $state) {
$result = SchoolConsent::updateOrCreate(
[
'user_id' => $record->user_id,
'school_id' => $record->school_id,
],
[
'status' => StatusEnum::fromBoolean($state),
]
);
}),
]);
}
}
class SchoolConsentRelationManager extends RelationManager
{
public function table(Table $table): Table
{
return $table
->columns([
ToggleColumn::make('status')
->label('Opted In')
->disabled(function (School $school) {
return ! $school->is_enabled;
})
->getStateUsing(function (School $record) {
return $record->status === OptedInStatusEnum::OPTED_IN;
})
->updateStateUsing(function (School $record, bool $state) {
$result = SchoolConsent::updateOrCreate(
[
'user_id' => $record->user_id,
'school_id' => $record->school_id,
],
[
'status' => StatusEnum::fromBoolean($state),
]
);
}),
]);
}
}
and the following test:
$livewire = Livewire::test(
SchoolConsentRelationManager::class,
[
'ownerRecord' => $user,
'pageClass' => EditUser::class,
]
);
// TODO update "status" column
$livewire->assertSuccessful();
// TODO assert records on SchoolConsent model.
$livewire = Livewire::test(
SchoolConsentRelationManager::class,
[
'ownerRecord' => $user,
'pageClass' => EditUser::class,
]
);
// TODO update "status" column
$livewire->assertSuccessful();
// TODO assert records on SchoolConsent model.
I'd like to test the logic for the ToggleColumn. I can test the initial state based on the data I set up in the test using ->assertTableActionDataSet(), but I want to 1. change the state of the toggle in the test and work the updateStateUsing() function and 2. check if the toggle is disabled (or not) based on the setup data I feed it I have read over the state testing docs, but they only seem to describe read-only tests.
4 replies