Table action testing

Im not really sure how to use callTableAction() for testing. Also to verify, this does work for headerActions too, right? I have a button that creates an export of the table contents and I am looking to verify the action works. Would love to verify even the contents of the csv it creates by comparing csv rows to table rows. The problem obviously is that it creates a dynamic file, so not as easy to evaluate. This is what I have so far and i know my code is wrong after the $response. Tried to go super simple to even just check file type and size:
->headerActions([
Action::make('export')
->label('Export')
->icon('heroicon-o-arrow-down-tray')
->action(function ($livewire) {
return (new ProjectsReportExport($livewire->getFilteredTableQuery()))->download('projects_report_' . now()->timestamp . '.csv', Excel::CSV, ['Content-Type' => 'text/csv']);
})
->tooltip('This will export the table along with any applied filters to a CSV file.')
->after(function ($action) {
Notification::make()->success()->title('Export Complete')->send();
}),
]);
->headerActions([
Action::make('export')
->label('Export')
->icon('heroicon-o-arrow-down-tray')
->action(function ($livewire) {
return (new ProjectsReportExport($livewire->getFilteredTableQuery()))->download('projects_report_' . now()->timestamp . '.csv', Excel::CSV, ['Content-Type' => 'text/csv']);
})
->tooltip('This will export the table along with any applied filters to a CSV file.')
->after(function ($action) {
Notification::make()->success()->title('Export Complete')->send();
}),
]);
and
it('can export projects report to csv', function () {
// Set up
login();
Project::factory()->count(10)->create();

// Call the export action and capture the response
$response = Livewire::test(ProjectsReport::class)->callTableAction('export');

// Assert
// Check if the response is not empty
expect($response)->notEmpty();

// Check if the response has the correct Content-Type header
$headers = $response->headers->all();
expect($headers['content-type'][0])->toBe('text/csv');

// Check if the file size is greater than 100 bytes
$fileSize = strlen($response->getContent());
expect($fileSize)->toBeGreaterThan(100);
});
it('can export projects report to csv', function () {
// Set up
login();
Project::factory()->count(10)->create();

// Call the export action and capture the response
$response = Livewire::test(ProjectsReport::class)->callTableAction('export');

// Assert
// Check if the response is not empty
expect($response)->notEmpty();

// Check if the response has the correct Content-Type header
$headers = $response->headers->all();
expect($headers['content-type'][0])->toBe('text/csv');

// Check if the file size is greater than 100 bytes
$fileSize = strlen($response->getContent());
expect($fileSize)->toBeGreaterThan(100);
});
1 Reply
Dan Harrin
Dan Harrin2y ago
i dont think there is an easier way to test livewire file downloads this isnt really related to filament actions this is just a livewire file download test isnt it
Want results from more Discord servers?
Add your server