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.
3 Replies
Joseph Kerkhof
Joseph KerkhofOP4mo ago
bump
Lind
Lind4mo ago
Best method i've found to toggle the state is:
->call('updateTableColumnState', 'column', $record->id, ! $record->column)
->call('updateTableColumnState', 'column', $record->id, ! $record->column)
Not sure how you can test if its disabled, but you can assert its state with
->assertTableColumnStateSet('column', false, $record->id)
->assertTableColumnStateSet('column', false, $record->id)
Joseph Kerkhof
Joseph KerkhofOP4mo ago
Thanks, @Lind , that first function does help me call the update function. Still trying to figure out how to check if the ToggleColumn is disabled or not.
Want results from more Discord servers?
Add your server