|
17 | 17 | use Inertia\LazyProp;
|
18 | 18 | use Inertia\MergeProp;
|
19 | 19 | use Inertia\ProvidesInertiaProperties;
|
| 20 | +use Inertia\ProvidesScrollMetadata; |
20 | 21 | use Inertia\RenderContext;
|
21 | 22 | use Inertia\Response;
|
| 23 | +use Inertia\ScrollProp; |
22 | 24 | use Inertia\Tests\Stubs\FakeResource;
|
23 | 25 | use Inertia\Tests\Stubs\MergeWithSharedProp;
|
24 | 26 | use Mockery;
|
| 27 | +use PHPUnit\Framework\Attributes\DataProvider; |
25 | 28 |
|
26 | 29 | class ResponseTest extends TestCase
|
27 | 30 | {
|
@@ -138,6 +141,75 @@ public function test_server_response_with_deferred_prop_and_multiple_groups(): v
|
138 | 141 | $this->assertSame('<div id="app" data-page="{"component":"User\/Edit","props":{"user":{"name":"Jonathan"}},"url":"\/user\/123","version":"123","clearHistory":false,"encryptHistory":false,"deferredProps":{"default":["foo","bar"],"custom":["baz"]}}"></div>', $view->render());
|
139 | 142 | }
|
140 | 143 |
|
| 144 | + public static function resetUsersProp(): array |
| 145 | + { |
| 146 | + return [ |
| 147 | + 'no reset' => [false], |
| 148 | + 'with reset' => [true], |
| 149 | + ]; |
| 150 | + } |
| 151 | + |
| 152 | + #[DataProvider('resetUsersProp')] |
| 153 | + public function test_server_response_with_scroll_props(bool $resetUsersProp): void |
| 154 | + { |
| 155 | + $request = Request::create('/user/123', 'GET'); |
| 156 | + |
| 157 | + if ($resetUsersProp) { |
| 158 | + $request->headers->add(['X-Inertia-Reset' => 'users']); |
| 159 | + } |
| 160 | + |
| 161 | + $response = new Response( |
| 162 | + 'User/Index', |
| 163 | + [ |
| 164 | + 'users' => new ScrollProp(['data' => [['id' => 1]]], 'data', new class implements ProvidesScrollMetadata |
| 165 | + { |
| 166 | + public function getPageName(): string |
| 167 | + { |
| 168 | + return 'page'; |
| 169 | + } |
| 170 | + |
| 171 | + public function getPreviousPage(): null |
| 172 | + { |
| 173 | + return null; |
| 174 | + } |
| 175 | + |
| 176 | + public function getNextPage(): int |
| 177 | + { |
| 178 | + return 2; |
| 179 | + } |
| 180 | + |
| 181 | + public function getCurrentPage(): int |
| 182 | + { |
| 183 | + return 1; |
| 184 | + } |
| 185 | + }), |
| 186 | + ], |
| 187 | + 'app', |
| 188 | + '123' |
| 189 | + ); |
| 190 | + $response = $response->toResponse($request); |
| 191 | + /** @var BaseResponse $response */ |
| 192 | + $view = $response->getOriginalContent(); |
| 193 | + $page = $view->getData()['page']; |
| 194 | + |
| 195 | + $this->assertInstanceOf(BaseResponse::class, $response); |
| 196 | + $this->assertInstanceOf(View::class, $view); |
| 197 | + |
| 198 | + $this->assertSame('User/Index', $page['component']); |
| 199 | + $this->assertSame(['data' => [['id' => 1]]], $page['props']['users']); |
| 200 | + $this->assertSame('/user/123', $page['url']); |
| 201 | + $this->assertSame('123', $page['version']); |
| 202 | + $this->assertSame([ |
| 203 | + 'users' => [ |
| 204 | + 'pageName' => 'page', |
| 205 | + 'previousPage' => null, |
| 206 | + 'nextPage' => 2, |
| 207 | + 'currentPage' => 1, |
| 208 | + 'reset' => $resetUsersProp, |
| 209 | + ], |
| 210 | + ], $page['scrollProps']); |
| 211 | + } |
| 212 | + |
141 | 213 | public function test_server_response_with_merge_props(): void
|
142 | 214 | {
|
143 | 215 | $request = Request::create('/user/123', 'GET');
|
@@ -501,6 +573,38 @@ public function test_exclude_merge_props_from_partial_except_response(): void
|
501 | 573 | $this->assertSame(['bar'], $page->mergeProps);
|
502 | 574 | }
|
503 | 575 |
|
| 576 | + public function test_exclude_merge_props_when_passed_in_reset_header(): void |
| 577 | + { |
| 578 | + $request = Request::create('/user/123', 'GET'); |
| 579 | + $request->headers->add(['X-Inertia' => 'true']); |
| 580 | + $request->headers->add(['X-Inertia-Partial-Component' => 'User/Edit']); |
| 581 | + $request->headers->add(['X-Inertia-Partial-Data' => 'foo']); |
| 582 | + $request->headers->add(['X-Inertia-Reset' => 'foo']); |
| 583 | + |
| 584 | + $user = ['name' => 'Jonathan']; |
| 585 | + $response = new Response( |
| 586 | + 'User/Edit', |
| 587 | + [ |
| 588 | + 'user' => $user, |
| 589 | + 'foo' => new MergeProp('foo value'), |
| 590 | + 'bar' => new MergeProp('bar value'), |
| 591 | + ], |
| 592 | + 'app', |
| 593 | + '123' |
| 594 | + ); |
| 595 | + |
| 596 | + /** @var JsonResponse $response */ |
| 597 | + $response = $response->toResponse($request); |
| 598 | + $page = $response->getData(); |
| 599 | + |
| 600 | + $props = get_object_vars($page->props); |
| 601 | + |
| 602 | + $this->assertInstanceOf(JsonResponse::class, $response); |
| 603 | + $this->assertSame($props['foo'], 'foo value'); |
| 604 | + $this->assertArrayNotHasKey('bar', $props); |
| 605 | + $this->assertFalse(isset($page->mergeProps)); |
| 606 | + } |
| 607 | + |
504 | 608 | public function test_xhr_response(): void
|
505 | 609 | {
|
506 | 610 | $request = Request::create('/user/123', 'GET');
|
|
0 commit comments