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

Testing the value of a prop #227

Closed
Livijn opened this issue Mar 9, 2021 · 8 comments
Closed

Testing the value of a prop #227

Livijn opened this issue Mar 9, 2021 · 8 comments

Comments

@Livijn
Copy link

Livijn commented Mar 9, 2021

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
Copy link
Author

Livijn commented Mar 11, 2021

@RobertBoes
Copy link
Contributor

RobertBoes commented Mar 11, 2021

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
Copy link
Author

Livijn commented Mar 11, 2021

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
Copy link
Contributor

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
Copy link

Hey @Livijn

I assume you are refering to this PR #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: laravel/framework#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
Copy link
Author

Livijn commented Mar 11, 2021

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.

@Livijn Livijn closed this as completed Mar 11, 2021
@reinink
Copy link
Member

reinink commented Mar 11, 2021

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
Copy link

. I guess the idea had to marinate a bit.

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

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

4 participants