F
Filamentβ€’5mo ago
ahinkle

Writing a test to call toggle on ToggleColumn List Resource

Hey everyone! I'm trying to write a test to toggle the is_enabled ToggleColumn on my Resource. I can't seem to find it in the documentation. Any ideas on how to write a test for this?
it('can toggle feature', function () {
$u = tap(User::factory()->create(), fn (User $user) => $user->givePermissionTo('Access Admin'));
$f = Feature::factory()->disabled()->create();

Livewire::actingAs($u)
->test(ListFeatures::class)
->call('toggleFeature', $f->is_enabled, $f->id); // toggle the feature? guessing here.. I have a ToggleColumn

expect($f->fresh()->is_enabled)->toBeTrue();
});
it('can toggle feature', function () {
$u = tap(User::factory()->create(), fn (User $user) => $user->givePermissionTo('Access Admin'));
$f = Feature::factory()->disabled()->create();

Livewire::actingAs($u)
->test(ListFeatures::class)
->call('toggleFeature', $f->is_enabled, $f->id); // toggle the feature? guessing here.. I have a ToggleColumn

expect($f->fresh()->is_enabled)->toBeTrue();
});
3 Replies
ahinkle
ahinkleβ€’5mo ago
Is a bump allowed? πŸ™‚
Karzer
Karzerβ€’4mo ago
I'm looking for this exact same thing. Any one have any idea how to accomplish this? Alright after a bit of digging I have found a solution. I don't love it but it works. Looking at the toggle-column blade template I found it's calling the updateTableColumnState method.
public function updateTableColumnState(string $column, string $record, mixed $input): mixed
public function updateTableColumnState(string $column, string $record, mixed $input): mixed
So calling the call method like this
call('updateTableColumnState', {{ Your-Column-Name }}, {{ Model-Id }}, {{ Boolean-Value }})
call('updateTableColumnState', {{ Your-Column-Name }}, {{ Model-Id }}, {{ Boolean-Value }})
Will get you there.
ahinkle
ahinkleβ€’4mo ago
Oo perfect, thanks, @Karzer !