Skip to content
Merged
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
8 changes: 8 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,11 @@ public function inertiaPage()
return AssertableInertia::fromTestResponse($this)->toArray();
};
}

public function inertiaProps()
{
return function (?string $propName = null) {
return Arr::get($this->inertiaPage()['props'], $propName);
};
}
}
26 changes: 26 additions & 0 deletions tests/Testing/TestResponseMacrosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}