Testing a form with live events

I'm trying to test a form that when I select a brand it should prefill the related suppliers in the next select. In my form I user the ->live() method to make the fields dependent on each other. In my test i have:
livewire(CreateOrder::class)
->assertFormFieldExists('brand_id')
->fillForm([
'brand_id' => $brand->id,
])->assertFormSet(['supplier_id' => $suppliers->pluck('id')->toArray()]);
livewire(CreateOrder::class)
->assertFormFieldExists('brand_id')
->fillForm([
'brand_id' => $brand->id,
])->assertFormSet(['supplier_id' => $suppliers->pluck('id')->toArray()]);
However this doesn't work, I get the error: Failed asserting that null matches expected 1.
at vendor/livewire/livewire/src/Features/SupportTesting/MakesAssertions.php:93 89▕ 90▕ if (! is_string($value) && is_callable($value)) { 91▕ PHPUnit::assertTrue($value($actual)); 92▕ } else { ➜ 93▕ $strict ? PHPUnit::assertSame($value, $actual) : PHPUnit::assertEquals($value, $actual); 94▕ } 95▕ 96▕ return $this; 97▕ }
I have tried to log what the value is of the suppliers. And I have confirmed that the options are empty. How Can I solve this, or where can I read about testing these dependent fields. Thanks.
3 Replies
JJSanders
JJSanders5mo ago
Bumping this question. Gonna try it once again. Anyone on this testing issue? Looking for people who also test their apps.
awcodes
awcodes5mo ago
Have you created a brand and suppliers for that brand in the test? Pretty sure assertFormSet will fail because there is no value set on the field. You would need to fill the select with an expected supplier id to assert if it’s set. Then fill it with an unexpected supplier id and assert that it fails.
JJSanders
JJSanders5mo ago
Yes I did:
$brand = Brand::factory()->create();
$suppliers = Supplier::factory()
->count(2)
->hasAttached($brand)
->create();

$this->get(OrderResource::getUrl('create'));
livewire(CreateOrder::class)
->assertFormFieldExists('brand_id')
->fillForm([
'brand_id' => $brand->id,
])
->assertFormSet(['supplier_id' => $suppliers->pluck('id')->toArray()]);
$brand = Brand::factory()->create();
$suppliers = Supplier::factory()
->count(2)
->hasAttached($brand)
->create();

$this->get(OrderResource::getUrl('create'));
livewire(CreateOrder::class)
->assertFormFieldExists('brand_id')
->fillForm([
'brand_id' => $brand->id,
])
->assertFormSet(['supplier_id' => $suppliers->pluck('id')->toArray()]);