diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index ef692c02..d5d953d8 100644 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -11,6 +11,7 @@ use Illuminate\Support\Fluent; use Illuminate\Http\JsonResponse; use Illuminate\Support\Collection; +use Inertia\Tests\Stubs\AsArrayable; use Inertia\Tests\Stubs\FakeResource; use Illuminate\Http\Response as BaseResponse; use Illuminate\Pagination\LengthAwarePaginator; @@ -210,6 +211,38 @@ public function test_arrayable_prop_response(): void $this->assertSame('123', $page->version); } + public function test_nested_arrayable_prop_response(): void + { + $request = Request::create('/user/123', 'GET'); + $request->headers->add(['X-Inertia' => 'true']); + + $data = AsArrayable::make([ + 'user' => function () { + return [ + 'full_name' => 'Victor Gutt', + 'organizations' => [ + function () { + return AsArrayable::make([ + 'name' => 'Transl.me', + ]); + }, + ], + ]; + }, + ]); + + $response = new Response('User/Edit', ['data' => $data], 'app', '123'); + $response = $response->toResponse($request); + $page = $response->getData(); + + $this->assertInstanceOf(JsonResponse::class, $response); + $this->assertSame('User/Edit', $page->component); + $this->assertSame('Victor Gutt', $page->props->data->user->full_name); + $this->assertSame('Transl.me', $page->props->data->user->organizations[0]->name); + $this->assertSame('/user/123', $page->url); + $this->assertSame('123', $page->version); + } + public function test_promise_props_are_resolved(): void { $request = Request::create('/user/123', 'GET'); diff --git a/tests/Stubs/AsArrayable.php b/tests/Stubs/AsArrayable.php new file mode 100644 index 00000000..18f2517e --- /dev/null +++ b/tests/Stubs/AsArrayable.php @@ -0,0 +1,31 @@ + + */ +class AsArrayable implements Arrayable { + /** + * @var array + */ + protected $data = []; + + public function __construct(array $data) + { + $this->data = $data; + } + + public static function make(array $data): self { + return new self($data); + } + + /** + * Get the instance as an array. + */ + public function toArray(): array { + return $this->data; + } +}