diff --git a/src/Testing/TestResponseMacros.php b/src/Testing/TestResponseMacros.php index 428dc2a6..cf4e5e29 100644 --- a/src/Testing/TestResponseMacros.php +++ b/src/Testing/TestResponseMacros.php @@ -3,6 +3,7 @@ namespace Inertia\Testing; use Closure; +use Illuminate\Support\Arr; class TestResponseMacros { @@ -27,4 +28,11 @@ public function inertiaPage() return AssertableInertia::fromTestResponse($this)->toArray(); }; } + + public function inertiaProps() + { + return function (?string $propName = null) { + return Arr::get($this->inertiaPage()['props'], $propName); + }; + } } diff --git a/tests/Testing/TestResponseMacrosTest.php b/tests/Testing/TestResponseMacrosTest.php index 312e8480..837177af 100644 --- a/tests/Testing/TestResponseMacrosTest.php +++ b/tests/Testing/TestResponseMacrosTest.php @@ -50,4 +50,30 @@ 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) + ); + + $this->assertSame($props, $response->inertiaProps()); + } + + public function test_it_can_retrieve_nested_inertia_prop_values_with_dot_notation(): void + { + $response = $this->makeMockRequest( + Inertia::render('foo', [ + 'bar' => ['baz' => 'qux'], + 'users' => [ + ['name' => 'John'], + ['name' => 'Jane'], + ], + ]) + ); + + $this->assertSame('qux', $response->inertiaProps('bar.baz')); + $this->assertSame('John', $response->inertiaProps('users.0.name')); + } }