Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simpler and fluent syntax to test for props #659

Open
Adesin-fr opened this issue Sep 7, 2024 · 0 comments
Open

Simpler and fluent syntax to test for props #659

Adesin-fr opened this issue Sep 7, 2024 · 0 comments

Comments

@Adesin-fr
Copy link

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 :


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 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant