Skip to content

Commit cf9c8b3

Browse files
authored
Merge pull request #700 from ashokatheheroo/2.x
[2.x] Introduce inertiaProps Method in TestResponseMacros for Improved Inertia.js Testing
2 parents 657d1e4 + a43d67f commit cf9c8b3

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/Testing/TestResponseMacros.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Inertia\Testing;
44

55
use Closure;
6+
use Illuminate\Support\Arr;
67

78
class TestResponseMacros
89
{
@@ -27,4 +28,11 @@ public function inertiaPage()
2728
return AssertableInertia::fromTestResponse($this)->toArray();
2829
};
2930
}
31+
32+
public function inertiaProps()
33+
{
34+
return function (?string $propName = null) {
35+
return Arr::get($this->inertiaPage()['props'], $propName);
36+
};
37+
}
3038
}

tests/Testing/TestResponseMacrosTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,30 @@ public function test_it_can_retrieve_the_inertia_page(): void
5050
$this->assertFalse($page['clearHistory']);
5151
});
5252
}
53+
54+
public function test_it_can_retrieve_the_inertia_props(): void
55+
{
56+
$props = ['bar' => 'baz'];
57+
$response = $this->makeMockRequest(
58+
Inertia::render('foo', $props)
59+
);
60+
61+
$this->assertSame($props, $response->inertiaProps());
62+
}
63+
64+
public function test_it_can_retrieve_nested_inertia_prop_values_with_dot_notation(): void
65+
{
66+
$response = $this->makeMockRequest(
67+
Inertia::render('foo', [
68+
'bar' => ['baz' => 'qux'],
69+
'users' => [
70+
['name' => 'John'],
71+
['name' => 'Jane'],
72+
],
73+
])
74+
);
75+
76+
$this->assertSame('qux', $response->inertiaProps('bar.baz'));
77+
$this->assertSame('John', $response->inertiaProps('users.0.name'));
78+
}
5379
}

0 commit comments

Comments
 (0)