How to test for missing component parameter?

What seems to be the problem:
Test passed even though the component was missing parameter passed into mount(). You must pass the parameter in the test for it not to break the test…

Steps to Reproduce:
If you have the following in a blade template…

@livewire('my-component', ['my_parameter' => $my_parameter])

Don’t pass my_parameter

@livewire('my-component')

Tests will pass, but the code will break.

Are you using the latest version of Livewire:
Yes

Any suggestion to test for this. I stumped myself for an hour, doh!

Not a perfect solution, but if you are assigning what was passed in to a property on the component, you could test that property equals what you passed.

Thanks @xxdalexx. I can’t seem to get my collection to match. I keep getting… Array (…) does not match expected type “object”.

I think I need to figure out how Livewire is changing my collection into an array.

I think the simplest way is you could do the inverse, assign that property an arbitrary default value and assert that it doesn’t equal that.

You could try to ->toArray() it, or also casting that property to a collection. I never dug into the tests far enough to see if there’s any emulation of converting an array to a javascript object and back, but I would guess there isn’t. Either way, things can get tricky and give false negatives when it comes to comparing collections/objects/arrays that are mutated causing their memory location and object id to change.

1 Like

Thanks @xxdalexx, I like the inverse approach.