I always struggle to remember the correct syntax to test for props in my inertia responses.
Sometimes Copilot helps me a bit, but it also seems to struggle remembering ;)
Since the whole majority of my tests consist of testing if the correct component is returned, but also get props and assert against their value, I wrote two helper functions in my pest testcase :
function getInertiaProps($response)
{
return json_decode(json_encode($response->viewData('page')), true)['props'];
}
function getInertiaPropKey($response, $key)
{
return Arr::get(getInertiaProps($response), $key);
}
I mostly use the second, like this :
test('I can fetch an order', function () {
$order = Order::factory()->create();
OrderLine::factory(5)->create(['order_id' => $order->id]);
$response = get(route('orders.edit', ['order' => $order->id]));
expect($response->status())->toBe(200)
->and(getInertiaPropKey($response, 'order.lines'))->toBeArray()->toHaveCount(5)
->and(getInertiaPropKey($response, 'order.id'))->toBe($order->id);
});
It would have be even greater if I did extend the response class so I could write expect($response->inertiaProp('order.lines))->toBeArray, but I haven't looked at how to do it.
Am I the only one who finds the ->assertInertia a bit too complex to write ?
Perhaps there might be another simpler syntax that I missed ?
Hi
I always struggle to remember the correct syntax to test for props in my inertia responses. Sometimes Copilot helps me a bit, but it also seems to struggle remembering ;)
Since the whole majority of my tests consist of testing if the correct component is returned, but also get props and assert against their value, I wrote two helper functions in my pest testcase :
I mostly use the second, like this :
It would have be even greater if I did extend the response class so I could write
expect($response->inertiaProp('order.lines))->toBeArray
, but I haven't looked at how to do it.Am I the only one who finds the
->assertInertia
a bit too complex to write ? Perhaps there might be another simpler syntax that I missed ?