Giter Site home page Giter Site logo

inertia-laravel-testing's Introduction

Latest Version Build Status Quality Score StyleCI Total Downloads

This package has been merged into inertiajs/inertia-laravel. This means that as of right now, this package WILL NOT be receiving any further (security) updates going forward.

If you haven't yet, it is highly recommended to migrate to inertiajs/inertia-laravel v0.4.0 or newer as soon as possible. Migrating only takes a couple of seconds, with no real implementation changes.

inertia-laravel-testing's People

Contributors

aryehraber avatar claudiodekker avatar hotmeteor avatar michaelklopf avatar rennokki avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

inertia-laravel-testing's Issues

Addition of parameter to revert to assertEquals instead of assertSame in where assertions

I'm new to InertiaJS and I'm really enjoying working with it so far and thank you for this testing package.

In the following test example (similar to that in the docs under the Automatic Eloquent heading) the attributes returned in the users prop from the route are the same as those returned from the User factory, but the order of attributes is different as the model factory will return the id as the last element of the array.

I think this difference in order causes the test to fail because of the assertSame assertion, which is looking for an exact match in both attributes and order.

$users = User::factory()->count(2)->create();

$response = $this->get('/users');

$response->assertInertia(fn (InertiaAssert $page) =>
  $page->component('Users/Index')
             ->where('users', $users->toArray())
);

I noticed in #27 the assertEquals assertion was replaced with assertSame.

Would it be worth adding a third parameter, maybe called exact or strict to the where method in the Matching trait.

This could default to true and use assertSame but if it was set to false then it could fallback to the less strict assertEquals to allow a test like this to pass. As an example implementation.

public function where($key, $value, $exact = true): self
    {
        $this->has($key);

        if ($value instanceof Closure) {
            $prop = $this->prop($key);

            PHPUnit::assertTrue(
                $value(is_array($prop) ? Collection::make($prop) : $prop),
                sprintf('Inertia property [%s] was marked as invalid using a closure.', $this->dotPath($key))
            );
        } elseif ($value instanceof Arrayable) {
            if ($exact) {
                PHPUnit::assertSame(
                    $value->toArray(),
                    $this->prop($key),
                    sprintf('Inertia property [%s] does not exactly match the expected Arrayable.', $this->dotPath($key))
                );
            } else {
                PHPUnit::assertEquals(
                    $value->toArray(),
                    $this->prop($key),
                    sprintf('Inertia property [%s] does not match the expected Arrayable.', $this->dotPath($key))
                );
            }
        } elseif ($value instanceof Responsable) {
            if ($exact) {
                PHPUnit::assertSame(
                    json_decode(json_encode($value->toResponse(request())->getData()), true),
                    $this->prop($key),
                    sprintf('Inertia property [%s] does not exactly match the expected Responsable.', $this->dotPath($key))
                );
            } else {
                PHPUnit::assertEquals(
                    json_decode(json_encode($value->toResponse(request())->getData()), true),
                    $this->prop($key),
                    sprintf('Inertia property [%s] does not match the expected Responsable.', $this->dotPath($key))
                );
            }
        } else {
            if ($exact) {
                PHPUnit::assertSame(
                    $value,
                    $this->prop($key),
                    sprintf('Inertia property [%s] does not exactly match the expected value.', $this->dotPath($key))
                );
            } else {
                PHPUnit::assertEquals(
                    $value,
                    $this->prop($key),
                    sprintf('Inertia property [%s] does not match the expected value.', $this->dotPath($key))
                );
            }
        }

        return $this;
    }

Feature request: ability to get page props

Hi, previously in version 1, we were able to do $response->inertiaProps() which will return an array of props for the page.

This is really useful because we can snapshot the props and ensure that all props are there and correct.

In version 2, I'm missing this feature. Is there a way to get the props? I don't want to use has because my test file will be to complex.

Thanks before.

V2 - Testing Packages

I encountered an issue for testing components with "claudiodekker/inertia-laravel-testing": "^2.2". We managed to resolve the issue but i will post its solution here, in order to be of assistance for others that might encounter this issue.

This package tries to verify the existence of such components within

app('inertia.view.finder')->find($value);
.

If you are developing a laravel package you are most porbably using orchestral/testbench and therefore /vendor/orchestra/testbench-core/laravel/resources/js/Pages will be missing the component.

In order to get testing to work one must extend your TestCase.php file:

protected function getEnvironmentSetUp($app)
    {
        ...

        //Setup Inertia for package development
        config()->set('inertia.page.paths', array_merge(
            config()->get('inertia.page.paths', []),
            [realpath(__DIR__ . '/../src/resources/js/Pages')],
        ));
    }

BadMethodCallException : Method Illuminate\Http\Response::assertInertia does not exist.

I have a test package like this, but it seems that testing not working:

public function test_authorized_user_can_access_resource(): void
    {
        $user = factory(User::class)->create();
        $user->assignRole('admin');
        $response = $this->actingAs($user)->get(Manager::createUri('pages'));
        $response->assertStatus(200);
        $response->assertInertia('resources/ResourceIndex');
    }

The error:

BadMethodCallException : Method Illuminate\Http\Response::assertInertia does not exist.

Failed asserting that stdClass Object is of type array

Hi,

First of all thank you for all the hard work of getting V2 of this package released, really excited to use it. I have just tried to do an initial implementation of this where I am returning a Laravel resource via Inertia.

Example

return Inertia::render(
            'Characters',
            [
                'characters' => new CharactersResourceCollection($characters),
            ]
        )->withViewData(['meta' => 'List of characters']);

Then as part of my test I am attempting to do the following:-

$response = $this->get('/characters');
$response->assertStatus(Response::HTTP_OK);
$response->assertInertia(fn (Assert $inertia) => $inertia
            ->component('Characters', false)
            ->where('characters', fn (Assert $inertia) => $inertia
                ->has('data', 7, function (Assert $a) => $inertia
                       ->where('0.name', 'Iron Man')
                       ->where('1.name', 'Thor')
                });

However at this point the assertion is failing as a stdClass is being returned rather than an array, causing PHPUnit::assertIsArray($props, sprintf('Inertia property [%s] is not scopeable.', $path)); to fail in Assert.php

I am not sure whether I am missing something in the documentation, or whether this library currently expecting the props to always be of type array.

`where` assertion: Automatically cast arrays to Collection instances

This would make use-cases like these slightly simpler to work with, by removing the need to manually make this cast:

$inertia->where('month.dates', function ($dates) {
    return collect($dates)
        ->where('date', '2018-06-02')
        ->where(...);

Before doing this we should look into making sure this doesn't cause any issues. I believe Inertia already converts things before we run assertions on it, but I'm not 100% sure of this from the top of my head. If it does, then this should be a very simple (breaking?) change to make.

Add two_factor_enable to user test?

I'm rapidly falling in love with this package.

Just working through the examples in the readme and this one where you assert againsts a model:

    /** @test */
    public function a_random_test(){

        $user = UserFactory::new()->create(['name' => 'John Doe']);

        $response = $this->actingAs($user)->get(route('dashboard'));

        $response->assertInertiaHas('user',$user);
       
    }

...fails (with a clean laravel/jetstream install - not using teams)

Maybe Laravel added the two_factor_enable recently or I need to tweak my user model?

1) Tests\Feature\OrganisationsTest::a_random_test
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
     'created_at' => '2020-10-17T18:07:55.000000Z'
     'id' => 1
     'profile_photo_url' => 'https://ui-avatars.com/api/?n...EBF4FF'
+    'two_factor_enabled' => false
 )

BadMethodCallException: Method Illuminate\Http\Response::assertInertia does not exist.

dont know if this is the right thread to post this, but im getting this on a fresh install laravel

BadMethodCallException: Method Illuminate\Http\Response::assertInertia does not exist.

i tried both vscode phpunit and better php unit.

and they both squawk the same error.

but if im using the native cli command

.\vendor\bin\phpunit --filter test_home_is_using_inertia_handle_request_shared_data tests/Feature/HomePageTest.php

i dont get error...

dont know if the vscode plugin has some error or you have instanciated the class out of scope for the plugin to pickup.

I can live with command line but , the convenience of using a tool like better phpunit to invoke same keystroke
saves more time :)

Can you look into this issue im sure this is very isolated case and mostly laravel people that are doing test will pick this error up.

Tag PHP 8.0 compatibility

Hi

Would it be possible to tag a version with PHP 8.0 compatibility please? I notice the master branch has support for ^8.0 but the latest release (1.1.1) does not.

Installing 1.1.1 via --ignore-platform-reqs appears to work fine, but if there are any outstanding compatibility issues I'm happy to try to help via PR!

Basic usage question

I've scaffolded a new Laravel 8 app with Jetstream/Inertia and I'm working out how to write some tests.

    /** @test */
    public function a_random_test(){

        $response = $this->get(route('dashboard'));

        $response->assertInertia('dashboard');

    }

I'm getting the failure:

The response is not a view.

Seems like I'm missing something obvious?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.