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

[2.x] Introduce inertiaProps Method in TestResponseMacros for Improved Inertia.js Testing #700

Open
wants to merge 3 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Testing/TestResponseMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Inertia\Testing;

use Closure;
use Illuminate\Support\Arr;

class TestResponseMacros
{
Expand All @@ -27,4 +28,14 @@ public function inertiaPage()
return AssertableInertia::fromTestResponse($this)->toArray();
};
}

public function inertiaProps()
{
return function (?string $propName = null) {
return Arr::get(
$this->inertiaPage()['props'] ?? [],
$propName
);
};
}
}
19 changes: 19 additions & 0 deletions tests/Testing/TestResponseMacrosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,23 @@ public function test_it_can_retrieve_the_inertia_page(): void
$this->assertFalse($page['clearHistory']);
});
}

public function test_it_can_retrieve_the_inertia_props(): void
{
$props = ['bar' => 'baz'];
$response = $this->makeMockRequest(
Inertia::render('foo', $props)
);

tap($response->inertiaProps(), fn (array $pageProps) => $this->assertSame($props, $pageProps));
}

public function test_it_can_retrieve_nested_inertia_prop_values_with_dot_notation(): void
{
$response = $this->makeMockRequest(
Inertia::render('foo', ['bar' => ['baz' => 'qux']])
);

tap($response->inertiaProps('bar.baz'), fn (mixed $value) => $this->assertSame('qux', $value));
}
}