Summer Sale! All accounts are 50% off this week.

Tiskiel's avatar

How assertEqual to Inertia with Pest ?

Hi everyone,

I've searched every were but I didn't found informations about my problem.

<?php

use App\Http\Controllers\PreparationMarkerController;
use App\Models\Marker;
use App\Models\Operation;
use App\Models\Preparation;
use Database\Seeders\DatabaseSeeder;
use Database\Seeders\OperationSeeder;
use Illuminate\Testing\Fluent\AssertableJson;
use Inertia\Testing\AssertableInertia as Assert;

beforeEach(function () {
    app(DatabaseSeeder::class)->call(OperationSeeder::class);
});

it('ensure markers are sorted by code', function (array $markers) {
    $preparation = Preparation::create();

    foreach ($markers as $marker) {
        Marker::factory(1)->create([
            'preparation_id' => $preparation->id,
            'operation_id' => Operation::all()->random()->id,
            'code' => $marker['code'],
        ]);
    }
    $this->withoutExceptionHandling();
    asAdmin()->get(action([PreparationMarkerController::class, 'index'], $preparation->id))
        ->assertInertia(fn (Assert $page) => $page
            ->component('Admin/Workflow/Preparations/Markers/Index')
            ->has('markers.data', function (AssertableJson $json) {
                $json
                    ->has('0', function (AssertableJson $json) {
                        $json
                            ->where('code', 'A011')
                            ->etc();
                    })
                    ->has('1', function (AssertableJson $json) {
                        $json
                            ->where('code', 'A0111')
                            ->etc();
                    })
                    ->has('2', function (AssertableJson $json) {
                        $json
                            ->where('code', 'A1')
                            ->etc();
                    })
                    ->has('3', function (AssertableJson $json) {
                        $json
                            ->where('code', 'AA011')
                            ->etc();
                    })
                    ->has('4', function (AssertableJson $json) {
                        $json
                            ->where('code', 'AA1')
                            ->etc();
                    })
                    ->has('5', function (AssertableJson $json) {
                        $json
                            ->where('code', 'AAA1')
                            ->etc();
                    })
                    ->has('6', function (AssertableJson $json) {
                        $json
                            ->where('code', 'AAA11')
                            ->etc();
                    });
            }));
})->with([
    'markers' => [
        [
            ['code' => 'A1'],
            ['code' => 'AA1'],
            ['code' => 'AAA11'],
            ['code' => 'AAA1'],
            ['code' => 'A0111'],
            ['code' => 'A011'],
            ['code' => 'AA011'],
        ],
    ],
]);

I have this test that work but I would d'like use one methode like " assertEqual " to assert my array are same value to ' markers.data.*.code '. But I read that don't work with Inertia.

My array that I want assertEqual is :


['A011', 'A0111', 'A1', 'AA011', 'AA1', 'AAA1', 'AAA11']

One of you already meet this problem ?

Thanks in advance

0 likes
2 replies
Tiskiel's avatar

We've improved our test with :


    $response = asAdmin()
        ->json('GET', action([PreparationMarkerController::class, 'index'], $preparation->id));

    $responseCodes = Arr::map(Arr::get($response, 'page.props.markers.data'), fn ($marker) => Arr::get($marker, 'code'));

    $this->assertEquals($responseCodes, ['A011', 'A0111', 'A1', 'AA011', 'AA1', 'AAA1', 'AAA11']);

1 like
n8udd's avatar

@Tiskiel Appreciate the reply to your own post here. Helpful, thank you!

1 like

Please or to participate in this conversation.