F
Filament15mo ago
Yaeger

How do you test repeaters?

Livewire::test(CreateFlexListing::class)
->fillForm([
'occupationId' => Occupation::factory()->create()->id,
'description' => 'Looking for help with our Laravel app!',
'shifts' => [
[
'date' => '2023-02-16',
'to' => '17:00',
'from' => '09:00',
],
]
])
->call('save')
->assertHasNoFormErrors();
Livewire::test(CreateFlexListing::class)
->fillForm([
'occupationId' => Occupation::factory()->create()->id,
'description' => 'Looking for help with our Laravel app!',
'shifts' => [
[
'date' => '2023-02-16',
'to' => '17:00',
'from' => '09:00',
],
]
])
->call('save')
->assertHasNoFormErrors();
I have a test like this but it doesn't seem to work:
Component has errors: "data.shifts.5c4b1833-1895-4afb-9689-c763674b6672.date", "data.shifts.5c4b1833-1895-4afb-9689-c763674b6672.startsAt", "data.shifts.5c4b1833-1895-4afb-9689-c763674b6672.endsAt"
Component has errors: "data.shifts.5c4b1833-1895-4afb-9689-c763674b6672.date", "data.shifts.5c4b1833-1895-4afb-9689-c763674b6672.startsAt", "data.shifts.5c4b1833-1895-4afb-9689-c763674b6672.endsAt"
8 Replies
Yaeger
YaegerOP15mo ago
It's creating a shifts entry immediately with some random UUID, which needs to be filled in.
Repeater::make('shifts')
->schema([
DatePicker::make('date'),
TimePicker::make('startsAt'),
TimePicker::make('endsAt')
])
Repeater::make('shifts')
->schema([
DatePicker::make('date'),
TimePicker::make('startsAt'),
TimePicker::make('endsAt')
])
Yaeger
YaegerOP15mo ago
Ok, I guess this is just not possible 🥲 https://github.com/filamentphp/filament/discussions/6070
GitHub
How to test elements in a repeater? · filamentphp filament · Discus...
I am trying to make sure the first element I feed into my form address repeater is marked primary. When I run the following test $page = Livewire::test(Create::class) ->assertFormExists('for...
Yaeger
YaegerOP15mo ago
Hm this seems like a different issue
Patrick Boivin
Patrick Boivin15mo ago
Someone else was having a similar issue here https://discord.com/channels/883083792112300104/1151260286490247229 I couldn't replicate it on my end
mariusticha
mariusticha15mo ago
we have the exact same issue. it was working in v2 but got this error in v3. in our case it seems that a fast fix is using livewires native ->set($key, $value) method to set the properties. but i have no clue why ->fillForm() doesn't work because under the hood it just calls ->set() would love to hear if someone find's a proper solution for this
Patricio
Patricio13mo ago
Ok bringing this up There's also same question here: https://discord.com/channels/883083792112300104/1150043364792733737 I came to the forum, to post this question, but I found existing questions, so here I am Most likely people are running into the same issue as me. In the Webapp, the repeater starts with the default element amount (1) with blank fields meaning that when we go to test, and use:
->fillForm([
'answers' => [
[
'text' => "test_text",
'correct' => true
],
[
'text' => "test_text2",
'correct' => false
]
]
])
->fillForm([
'answers' => [
[
'text' => "test_text",
'correct' => true
],
[
'text' => "test_text2",
'correct' => false
]
]
])
instead of having 2 elements, we get a surprise of a fail, because there are 3 elements when the component mounts, it starts with 1 so, the solution came from @Leandro Ferreira who just investigated and told me: set the intial state for the repeater, by using for example: ->set('data.answers', null) so, such sequece must be like:
->set('data.answers', null)
->fillForm([
'answers' => [
[
'text' => "test_text",
'correct' => true
],
[
'text' => "test_text2",
'correct' => false
]
]
])
->set('data.answers', null)
->fillForm([
'answers' => [
[
'text' => "test_text",
'correct' => true
],
[
'text' => "test_text2",
'correct' => false
]
]
])
so now we can ensure that the repeater state will be what we provided
mariusticha
mariusticha13mo ago
we've found another solution, pointing into the same direction:
->set('data.answers', [
[
'text' => "test_text",
'correct' => true
],
[
'text' => "test_text2",
'correct' => false
]
])
->set('data.answers', [
[
'text' => "test_text",
'correct' => true
],
[
'text' => "test_text2",
'correct' => false
]
])
this sets the content of data.answers regardless of its previous value, overriding the default(1) item indexed by the uuid
Patricio
Patricio13mo ago
yep, perfect! 🤩
Want results from more Discord servers?
Add your server