F
Filament16mo ago
Diana

assertTableActionDataSet() on a form with FileUpload field

I got this form:
public static function form(Form $form): Form
{
return $form
->schema([
static::getNameFormField(),
Forms\Components\FileUpload::make('file')
->label(___('signeditor.files', 1))
->required()
->disk('fonts')
->getUploadedFileNameForStorageUsing(
fn (TemporaryUploadedFile $file): string => (string) str($file->getClientOriginalName())
->prepend(Str::random(40))->replace(' ', '_')
),
]);
}
public static function form(Form $form): Form
{
return $form
->schema([
static::getNameFormField(),
Forms\Components\FileUpload::make('file')
->label(___('signeditor.files', 1))
->required()
->disk('fonts')
->getUploadedFileNameForStorageUsing(
fn (TemporaryUploadedFile $file): string => (string) str($file->getClientOriginalName())
->prepend(Str::random(40))->replace(' ', '_')
),
]);
}
and this test:
it('can save', function () {
Storage::disk('fonts')->put($this->name, file_get_contents('storage/app/tests/' . $this->name));
Storage::disk('thumbnails')->put($this->thumbnail, file_get_contents('storage/app/tests/' . $this->thumbnail));
$record = $this->modelClass::factory()->create(['file' => $this->name]);
expect(Storage::disk('fonts')->path($record->file))->toBeReadableFile();
expect(Storage::disk('thumbnails')->path($record->thumbnail))->toBeReadableFile();

livewire($this->indexClass)
->mountTableAction(EditAction::class, $record)
->assertTableActionDataSet([
'name' => $record->name,
'file' => $record->file
])->callTableAction(EditAction::class, $record, data: ['name'=>$name = fake()->word(), 'file'=>$this->newFile])
->assertHasNoTableActionErrors();
it('can save', function () {
Storage::disk('fonts')->put($this->name, file_get_contents('storage/app/tests/' . $this->name));
Storage::disk('thumbnails')->put($this->thumbnail, file_get_contents('storage/app/tests/' . $this->thumbnail));
$record = $this->modelClass::factory()->create(['file' => $this->name]);
expect(Storage::disk('fonts')->path($record->file))->toBeReadableFile();
expect(Storage::disk('thumbnails')->path($record->thumbnail))->toBeReadableFile();

livewire($this->indexClass)
->mountTableAction(EditAction::class, $record)
->assertTableActionDataSet([
'name' => $record->name,
'file' => $record->file
])->callTableAction(EditAction::class, $record, data: ['name'=>$name = fake()->word(), 'file'=>$this->newFile])
->assertHasNoTableActionErrors();
** FAILED Tests\Feature\Resources\FontResourceTest** > it can save
** [...] does not match expected type "object".**
** FAILED Tests\Feature\Resources\FontResourceTest** > it can save
** [...] does not match expected type "object".**
Now, obviously, the
$record->file
$record->file
part is a string, while the form field is whatever object type FileUpload takes. I've tried to put an UploadedFile fake in there, but to no avail. Does anyone know how I'd test the FileUpload field?
4 Replies
Tom
Tom2w ago
Did you ever figure this one out? I'm having a similar issue.
Vp
Vp2w ago
Try checking like this, but not sure it will works for you or not https://discord.com/channels/883083792112300104/1331765576812593273/1331834242342195353
Diana
DianaOP2w ago
I'm afraid not, sorry 😔
Tom
Tom2w ago
So with a little assistance from Copilot, I was able to get it working using the ->set() method.
livewire(EditRecipe::class, ['record' => $recipe->getRouteKey()])
->fillForm([
'title' => $recipe->title,
'description' => $recipe->description,
// 'image' => [$newFile],
])
->set('data.image', [$newFile])
->call('save')
->assertHasNoFormErrors();
livewire(EditRecipe::class, ['record' => $recipe->getRouteKey()])
->fillForm([
'title' => $recipe->title,
'description' => $recipe->description,
// 'image' => [$newFile],
])
->set('data.image', [$newFile])
->call('save')
->assertHasNoFormErrors();

Did you find this page helpful?