Skip to content

Commit 15b7329

Browse files
test: add coverage for resolving array helper props in Inertia responses
1 parent 656661c commit 15b7329

2 files changed

Lines changed: 41 additions & 32 deletions

File tree

tests/Fixtures/TestController.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use function NeoIsRecursive\Inertia\inertia;
2121
use function Tempest\Router\uri;
22+
use function Tempest\Support\arr;
2223

2324
final readonly class TestController
2425
{
@@ -55,14 +56,14 @@ public function testCanSharePropsFromAnyWhere(Inertia $inertia): InertiaResponse
5556
public function testAllSortsOfProps(Inertia $inertia): InertiaResponse
5657
{
5758
return $inertia->share(
58-
key: 'foo',
59-
value: 'bar',
59+
key: 'shared',
60+
value: Inertia::always(arr([1, 2, 3])),
6061
)->render(
6162
component: 'User/Edit',
6263
props: [
63-
'always' => Inertia::always(fn() => 'always'),
64-
'optional' => Inertia::optional(fn() => 'optional'),
65-
'defer' => Inertia::defer(fn() => 'defer'),
64+
'always' => Inertia::always(fn() => arr(['always-1', 'always-2'])),
65+
'optional' => Inertia::optional(fn() => arr(['optional-1', 'optional-2'])),
66+
'defer' => Inertia::defer(fn() => arr(['defer-1', 'defer-2'])),
6667
],
6768
);
6869
}

tests/Integration/InertiaTest.php

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,10 @@ public function test_shared_data_can_be_shared_from_anywhere(): void
193193

194194
public function test_can_flush_shared_data(): void
195195
{
196-
get(Inertia::class)
197-
->share(
198-
key: 'foo',
199-
value: 'bar',
200-
);
196+
get(Inertia::class)->share(
197+
key: 'foo',
198+
value: 'bar',
199+
);
201200

202201
static::assertArrayHasKey(
203202
key: 'foo',
@@ -245,26 +244,35 @@ public function test_mergeable_props_can_be_merged(): void
245244
);
246245
}
247246

248-
// public function test_will_accept_arrayabe_props()
249-
// {
250-
// Route::middleware([StartSession::class, ExampleMiddleware::class])->get('/', function () {
251-
// Inertia::share('foo', 'bar');
252-
// return Inertia::render('User/Edit', new class() implements Arrayable {
253-
// public function toArray()
254-
// {
255-
// return [
256-
// 'foo' => 'bar',
257-
// ];
258-
// }
259-
// });
260-
// });
261-
// $response = $this->http->get('/', ['X-Inertia' => 'true']);
262-
// $response->assertSuccessful();
263-
// $response->assertJson([
264-
// 'component' => 'User/Edit',
265-
// 'props' => [
266-
// 'foo' => 'bar',
267-
// ],
268-
// ]);
269-
// }
247+
public function test_array_helper_props_can_be_resolved(): void
248+
{
249+
$version = get(Inertia::class)->version;
250+
251+
$response = $this->http->get(uri([TestController::class, 'testAllSortsOfProps']), headers: [
252+
Header::INERTIA => 'true',
253+
Header::VERSION => $version,
254+
Header::PARTIAL_COMPONENT => 'User/Edit',
255+
Header::PARTIAL_ONLY => 'optional,defer',
256+
]);
257+
258+
$response->assertOk();
259+
static::assertSame(
260+
expected: [
261+
'component' => 'User/Edit',
262+
'props' => [
263+
'user' => null,
264+
'errors' => [],
265+
'shared' => [1, 2, 3],
266+
'always' => ['always-1', 'always-2'],
267+
'optional' => ['optional-1', 'optional-2'],
268+
'defer' => ['defer-1', 'defer-2'],
269+
],
270+
'url' => uri([TestController::class, 'testAllSortsOfProps']),
271+
'version' => $version,
272+
'clearHistory' => false,
273+
'encryptHistory' => false,
274+
],
275+
actual: $response->body->jsonSerialize(),
276+
);
277+
}
270278
}

0 commit comments

Comments
 (0)