How to use fillForm() with a repeater?

Hey, I'm trying to set up some tests for my application but how do I use fillForm with a repeater?
livewire(DeliveryResource\Pages\CreateDelivery::class)
->fillForm([
'products' => [
'description' => 'Test',
'qty' => 1
],
])
->call('create')
->assertHasNoFormErrors();
livewire(DeliveryResource\Pages\CreateDelivery::class)
->fillForm([
'products' => [
'description' => 'Test',
'qty' => 1
],
])
->call('create')
->assertHasNoFormErrors();
This doesn't seem to work.
16 Replies
awcodes
awcodes17mo ago
is products your repeater? repeaters are an array of arrays
bahamagician
bahamagicianOP17mo ago
Yea, products is my repeater
cheesegrits
cheesegrits17mo ago
Nope, that's an array of properties.
bahamagician
bahamagicianOP17mo ago
Would you be able to give me an example of what it should look like?
cheesegrits
cheesegrits17mo ago
Wrap an extra set of [] around it. Although it may need a string key, iirc. Lemme check something, brb.
bahamagician
bahamagicianOP17mo ago
Ok thanks
cheesegrits
cheesegrits17mo ago
So might need to be sometrhing like ...
'products' => [
Str::uuid() => [
// properties
]
]
'products' => [
Str::uuid() => [
// properties
]
]
bahamagician
bahamagicianOP17mo ago
Thanks, I'm still getting an error but it's no longer about the products repater lol. Appreciate the help!
cheesegrits
cheesegrits17mo ago
Well, that's progress, at least. Is it a relationship repeater? I'm not sure what they use as temporary keys when adding, whether it's a random uuid or some special key.
bahamagician
bahamagicianOP17mo ago
Nah, it's just a basic json field repeater The error I'm getting now says Illegal offset type
cheesegrits
cheesegrits17mo ago
Try it without the key. Looking at my code, it was a relationship repeater I'm using uuid's with. Maybe JSON repeaters don't use string keys.
bahamagician
bahamagicianOP17mo ago
Without the key I'm back to getting the previous error which is
Tests\Feature\DeliveryResourceTest > it can create delivery
Component has errors: "data.products.9e93a0bc-373d-4608-a6f3-9c8774cbae04.description", "data.products.9e93a0bc-373d-4608-a6f3-9c8774cbae04.qty"
Failed asserting that false is true.
Tests\Feature\DeliveryResourceTest > it can create delivery
Component has errors: "data.products.9e93a0bc-373d-4608-a6f3-9c8774cbae04.description", "data.products.9e93a0bc-373d-4608-a6f3-9c8774cbae04.qty"
Failed asserting that false is true.
The above error is if I'm using
'products' => [
[
'description' => 'Test',
'qty' => 1
]
],
'products' => [
[
'description' => 'Test',
'qty' => 1
]
],
cheesegrits
cheesegrits17mo ago
Not sure. I'm writing a test suite atm, and at some point maybe this evening I'll get as far as one of my forms with a repeater. If so I'll check back in here when I figure it out. The code I was looking at which fills a relationship repeater is on an actual form, not using fillForm in a test.
bahamagician
bahamagicianOP17mo ago
Okay, appreciate the help, thanks
cheesegrits
cheesegrits17mo ago
Sorry I couldn't be of more help.
bahamagician
bahamagicianOP17mo ago
No worries So I figured out what's causing the problem but I don't know how to get around it. By default the repeater input type has a single empty item. When I use the form fill method, it's adding to that repeater array instead of replacing it completely. However, because those form fields are required, it's throwing an form error for the empty fields that are initially there. If for example I set "->defaultItems(0)" on the repeater, then it solves my testing woes. However, I'd prefer that the UI actually does show an initial empty field, so I'd rather not have to do that. Is there a way in the test maybe to either replace the existing empty fields in the repeater array altogether or at least get the UUID that's generated for them so I can assign values to those form fields? Figured out how to do it....
livewire(DeliveryResource\Pages\CreateDelivery::class)
->set('data.products', [])
->fillForm([[
'products' => [
'description' => 'Test',
'qty' => 1
]],
])
->call('create')
->assertHasNoFormErrors();
livewire(DeliveryResource\Pages\CreateDelivery::class)
->set('data.products', [])
->fillForm([[
'products' => [
'description' => 'Test',
'qty' => 1
]],
])
->call('create')
->assertHasNoFormErrors();
Have to add ->set('data.products', [])

Did you find this page helpful?