Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kianaassenheimer's avatar

Filament Testing Import - 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),
            ]);
    }

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);
    }

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?

0 likes
1 reply
aizensoosuke's avatar

You can use the following code. It's made for pest but you can adapt it for phpunit.

it('can import a one line csv', function () {
        $csv = file_get_contents(base_path('tests/Feature/FakeData/import.csv'));
        $file = UploadedFile::fake()->createWithContent('import.csv', $csv);

        livewire(ManageYourModel::class)
            ->assertSuccessful()
            ->assertTableActionExists('import')
            ->mountTableAction('import')
            ->setTableActionData([
                'file' => $file,
            ])
            ->callMountedTableAction()
            ->assertHasNoTableActionErrors();

        expect(YourModel::count())->toBe(1);
    });

Please or to participate in this conversation.