inertiajs / inertia-laravel

The Laravel adapter for Inertia.js.
https://inertiajs.com
MIT License
2.08k stars 235 forks source link

Testing the value of a prop #227

Closed Livijn closed 3 years ago

Livijn commented 3 years ago

First off, I've looked through a couple of PRs that tried to ease the use of testing with Inertia. I'm not sure why you chose the "unorthodox" way that you did. The testing that exists in Laravel doesn't use closures like this, which is why it feels strange.

$response->assertInertia(function (Assert $page) {
    $page->component('Ads');
    $page->hasAll(['foo', 'bar']);
});

// vs

$response->assertInertiaComponent('Ads');
$response->assertInertiaHasProps(['foo', 'bar']);

So, now to my question. How do I test that a prop is equal to a value?

Based of the name I thought $page->has('foo', ['some', 'array']); would work, but it seems the has() method only allows integers.

Livijn commented 3 years ago

Also, the demo app uses a macro instead: https://github.com/inertiajs/pingcrm/blob/master/tests/TestCase.php#L18

RobertBoes commented 3 years ago

I think has is only used to check if a property exists, in the same way Arr::has() works. From looking at the code I think you'd need to use where, so you can check if foo has the value bar using this:

$response->assertInertia(function (Assert $page) {
    $page->component('Ads');

    // Check if the 'foo' key exists
    $page->has('foo');

    // Check if 'foo' has the expected value 'bar'
    $page->where('foo', 'bar');
});
Livijn commented 3 years ago

Yes, that works. But what if I'd wanted to $this->assertArrayHasKey() or any other assertion? Since the props are set to private on the Assert class, we can't access them.

RobertBoes commented 3 years ago

I suppose you could do this:

// Assuming your data looks like this:
// [
//   'foo' => ['some' => 'array'],
// ]

$response->assertInertia(function (Assert $page) {
    // $foo will be ['some' => 'array']
    $foo = $page->prop('foo');

    // Use PHPUnit assertions on the prop
    $this->assertArrayHasKey('some', $foo);
});
herpaderpaldent commented 3 years ago

Hey @Livijn

I assume you are refering to this PR https://github.com/inertiajs/inertia-laravel/pull/220 ?

This comment made me come here to comment:

The testing that exists in Laravel doesn't use closures like this, which is why it feels strange.

However this is not true at all as you will find new laravel versions having a very similar JSON assertion built in to laravel: https://github.com/laravel/framework/pull/36454 . On this PR you'll find a very extensive description on how to use it. IMHO: @claudiodekker 's work has been amazing on the tester and if pingcrm is not using it yet it just means, the core team didn't have to time to convert it. PingCRM is not aimed to be feature complete nor to achieve feature parity. It's just a demo project laying out the very fundamentic way inertia is helping you as a developer

Livijn commented 3 years ago

However this is not true at all as you will find new laravel versions having a very similar JSON assertion built in to laravel: laravel/framework#36454 .

That is true. However, that PR was merged merely 1 day before i posted this issue. So this is still a pretty atypical way of testing in Laravel.

After playing around with this new method of testing, I have grown fond of it. I guess the idea had to marinate a bit.

reinink commented 3 years ago

So this is still a pretty atypical way of testing in Laravel.

Totally, we're breaking new ground here @Livijn 😎

After playing around with this new method of testing, I have grown fond of it. I guess the idea had to marinate a bit.

🙌

herpaderpaldent commented 3 years ago

. I guess the idea had to marinate a bit.

🤣 great, now i am hungry and want to lighten up the bbq.