kiana1300
kiana1300
FFilament
Created by kiana1300 on 4/25/2024 in #❓┊help
How to test SelectColumn in a table?
I am trying to test my SelectColum that when I change a role, that user's role is updated in the database. From the testing docs https://filamentphp.com/docs/3.x/tables/testing#select-columns , it doesn't seem like this type of functionality is testable through a method much like a filter can be applied with
->filterTable(
->filterTable(
... I'm wondering if there is a method such as
selectColumn
selectColumn
that I just can't find in the docs/through the ide. Here is a barebones test:
/** @test */
public function it_will_correctly_reassign_a_users_role(): void
{
// Given I have a user with an 'Admin' role
$superadmin_role_id = Role::where('name', 'Superadmin')->first()->id;
$admin_role_id = Role::where('name', 'Admin')->first()->id;
$user = User::factory()->create(['role_id' => $admin_role_id]);

// When I change the role to 'Superadmin'
Livewire::test(AdminClinicPageComponent::class, ['clinic' => $this->clinicA])
->selectColumn('role_id', $superadmin_role_id, record: $user);

// Then my user's role should be 'Superadmin'
$this->assertEquals($superadmin_role_id, $user->fresh()->role_id);
}
/** @test */
public function it_will_correctly_reassign_a_users_role(): void
{
// Given I have a user with an 'Admin' role
$superadmin_role_id = Role::where('name', 'Superadmin')->first()->id;
$admin_role_id = Role::where('name', 'Admin')->first()->id;
$user = User::factory()->create(['role_id' => $admin_role_id]);

// When I change the role to 'Superadmin'
Livewire::test(AdminClinicPageComponent::class, ['clinic' => $this->clinicA])
->selectColumn('role_id', $superadmin_role_id, record: $user);

// Then my user's role should be 'Superadmin'
$this->assertEquals($superadmin_role_id, $user->fresh()->role_id);
}
Here is my code snippet
public function table(Table $table): Table
{
return $table
->query(
User::query()
)
->columns([
// other text columns in my tables
SelectColumn::make('role_id')
->options($this->allowed_roles)
->afterStateUpdated(function ($record, $state): void
{
$record->assignRole($state);
$record->refresh();
}),
]);
}
public function table(Table $table): Table
{
return $table
->query(
User::query()
)
->columns([
// other text columns in my tables
SelectColumn::make('role_id')
->options($this->allowed_roles)
->afterStateUpdated(function ($record, $state): void
{
$record->assignRole($state);
$record->refresh();
}),
]);
}
1 replies
FFilament
Created by kiana1300 on 2/14/2024 in #❓┊help
Testing ImportAction within a table headerActions
Scenario: I have a livewire component using filament table with the headerActions of ImportAction. I have a csv file that when uploaded, will update the database with the values within in. A snippet of my table:
public function table(Table $table): Table
{
return $table
->query(Model::query()->orderByDesc('created_at'))
->columns($this->table_columns())
->headerActions([
ImportAction::make()
->name('import_choices')
->importer(DatasetImporter::class),
]);
}
public function table(Table $table): Table
{
return $table
->query(Model::query()->orderByDesc('created_at'))
->columns($this->table_columns())
->headerActions([
ImportAction::make()
->name('import_choices')
->importer(DatasetImporter::class),
]);
}
My test
/** @test */
public function test_example()
{
$this->csv = file_get_contents(base_path('tests/data/mapping_data/filament_import.csv'));
$this->file = UploadedFile::fake()->createWithContent('filament_import.csv', $this->csv);
// given that my field does not have a field choice
$this->assertNull($this->field->choice_id);
$this->assertCount(0, Import::get());

// when I import the file
Livewire::test(RedcapBackfillMissingOptionsComponent::class)
->callTableAction('import_choices'); // what do I need to be passing through here?

// then my field should have a field choice
$this->assertCount(1, Import::get());
$this->assertEquals($this->choice->id, $this->field->fresh()->choice_id);
}
/** @test */
public function test_example()
{
$this->csv = file_get_contents(base_path('tests/data/mapping_data/filament_import.csv'));
$this->file = UploadedFile::fake()->createWithContent('filament_import.csv', $this->csv);
// given that my field does not have a field choice
$this->assertNull($this->field->choice_id);
$this->assertCount(0, Import::get());

// when I import the file
Livewire::test(RedcapBackfillMissingOptionsComponent::class)
->callTableAction('import_choices'); // what do I need to be passing through here?

// then my field should have a field choice
$this->assertCount(1, Import::get());
$this->assertEquals($this->choice->id, $this->field->fresh()->choice_id);
}
Problem: when I ->callTableAction('import_choices'), I know I can pass through the $record, $data, $arguments from drilling into it, but I'm not too sure what I need to pass in order to get my import, and see that the $this->field->choice_id is set. When I run my test currently, I don't get any errors but my Import count is 0. How do I test this import?
2 replies