Skip to content

Commit 18a47d8

Browse files
committed
Add support for reload, reloadOnly and reloadExcept
1 parent 2006e2a commit 18a47d8

File tree

5 files changed

+147
-43
lines changed

5 files changed

+147
-43
lines changed

src/Testing/AssertableInertia.php

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

55
use Closure;
6-
use Illuminate\Foundation\Application;
76
use Illuminate\Testing\Fluent\AssertableJson;
87
use Illuminate\Testing\TestResponse;
98
use InvalidArgumentException;
@@ -83,21 +82,30 @@ public function version(string $value): self
8382
return $this;
8483
}
8584

86-
public function partial(array|string $key, ?Closure $callback = null, ?Application $app = null): self
85+
public function reload(?Closure $callback = null, array|string|null $only = null, array|string|null $except = null): self
8786
{
88-
$partialRequest = new PartialRequest(
89-
is_array($key) ? implode(',', $key) : $key,
87+
if (is_array($only)) {
88+
$only = implode(',', $only);
89+
}
90+
91+
if (is_array($except)) {
92+
$except = implode(',', $except);
93+
}
94+
95+
$reloadRequest = new ReloadRequest(
9096
$this->url,
9197
$this->component,
9298
$this->version,
93-
$app ?? app()
99+
$only,
100+
$except,
94101
);
95102

96-
$assertable = static::fromTestResponse($partialRequest());
103+
$assertable = AssertableInertia::fromTestResponse($reloadRequest());
104+
105+
// Make sure we get the same data as the original request.
97106
$assertable->component($this->component);
98107
$assertable->url($this->url);
99108
$assertable->version($this->version);
100-
$assertable->hasAll(is_array($key) ? $key : explode(',', $key));
101109

102110
if ($callback) {
103111
$callback($assertable);
@@ -106,6 +114,28 @@ public function partial(array|string $key, ?Closure $callback = null, ?Applicati
106114
return $this;
107115
}
108116

117+
public function reloadOnly(array|string $only, ?Closure $callback = null): self
118+
{
119+
return $this->reload(only: $only, callback: function (AssertableInertia $assertable) use ($only, $callback) {
120+
$assertable->hasAll(explode(',', $only));
121+
122+
if ($callback) {
123+
$callback($assertable);
124+
}
125+
});
126+
}
127+
128+
public function reloadExcept(array|string $except, ?Closure $callback = null): self
129+
{
130+
return $this->reload(except: $except, callback: function (AssertableInertia $assertable) use ($except, $callback) {
131+
$assertable->missingAll(explode(',', $except));
132+
133+
if ($callback) {
134+
$callback($assertable);
135+
}
136+
});
137+
}
138+
109139
public function toArray()
110140
{
111141
return [

src/Testing/PartialRequest.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/Testing/ReloadRequest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Inertia\Testing;
4+
5+
use Illuminate\Foundation\Application;
6+
use Illuminate\Foundation\Testing\Concerns\MakesHttpRequests;
7+
use Illuminate\Testing\TestResponse;
8+
use Inertia\Support\Header;
9+
10+
class ReloadRequest
11+
{
12+
use MakesHttpRequests;
13+
14+
public function __construct(
15+
protected string $url,
16+
protected string $component,
17+
protected string $version,
18+
protected ?string $only = null,
19+
protected ?string $except = null,
20+
protected ?Application $app = null
21+
) {
22+
$this->app ??= app();
23+
}
24+
25+
public function __invoke(): TestResponse
26+
{
27+
$headers = [Header::VERSION => $this->version];
28+
29+
if (! blank($this->only)) {
30+
$headers[Header::PARTIAL_COMPONENT] = $this->component;
31+
$headers[Header::PARTIAL_ONLY] = $this->only;
32+
}
33+
34+
if (! blank($this->except)) {
35+
$headers[Header::PARTIAL_COMPONENT] = $this->component;
36+
$headers[Header::PARTIAL_EXCEPT] = $this->except;
37+
}
38+
39+
return $this->get($this->url, $headers);
40+
}
41+
}

tests/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected function getTestResponseClass(): string
5252
protected function makeMockRequest($view)
5353
{
5454
app('router')->get('/example-url', function () use ($view) {
55-
return $view;
55+
return is_callable($view) ? $view() : $view;
5656
});
5757

5858
return $this->get('/example-url');

tests/Testing/AssertableInertiaTest.php

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

55
use Inertia\Inertia;
6+
use Inertia\Testing\AssertableInertia;
67
use Inertia\Tests\TestCase;
78
use PHPUnit\Framework\AssertionFailedError;
89

@@ -194,23 +195,85 @@ public function test_the_asset_version_does_not_match(): void
194195
});
195196
}
196197

198+
public function test_reloading_a_visit(): void
199+
{
200+
$foo = 0;
201+
202+
$response = $this->makeMockRequest(function () use (&$foo) {
203+
return Inertia::render('foo', [
204+
'foo' => $foo++,
205+
]);
206+
});
207+
208+
$called = false;
209+
210+
$response->assertInertia(function ($inertia) use (&$called) {
211+
$inertia->where('foo', 0);
212+
213+
$inertia->reload(function ($inertia) use (&$called) {
214+
$inertia->where('foo', 1);
215+
$called = true;
216+
});
217+
});
218+
219+
$this->assertTrue($called);
220+
}
221+
197222
public function test_lazy_props_can_be_evaluated(): void
198223
{
199224
$response = $this->makeMockRequest(
200225
Inertia::render('foo', [
201226
'foo' => 'bar',
202-
'lazy' => Inertia::lazy(fn () => 'baz'),
227+
'lazy1' => Inertia::lazy(fn () => 'baz'),
228+
'lazy2' => Inertia::lazy(fn () => 'qux'),
203229
])
204230
);
205231

206-
$response->assertInertia(function ($inertia) {
232+
$called = false;
233+
234+
$response->assertInertia(function ($inertia) use (&$called) {
207235
$inertia->where('foo', 'bar');
208-
$inertia->missing('lazy');
236+
$inertia->missing('lazy1');
237+
$inertia->missing('lazy2');
209238

210-
$inertia->partial('lazy', function ($inertia) {
211-
$inertia->where('lazy', 'baz');
239+
$result = $inertia->reloadOnly('lazy1', function ($inertia) use (&$called) {
212240
$inertia->missing('foo');
241+
$inertia->where('lazy1', 'baz');
242+
$inertia->missing('lazy2');
243+
$called = true;
213244
});
245+
246+
$this->assertSame($result, $inertia);
214247
});
248+
249+
$this->assertTrue($called);
250+
}
251+
252+
public function test_lazy_props_can_be_evaluated_with_except(): void
253+
{
254+
$response = $this->makeMockRequest(
255+
Inertia::render('foo', [
256+
'foo' => 'bar',
257+
'lazy1' => Inertia::lazy(fn () => 'baz'),
258+
'lazy2' => Inertia::lazy(fn () => 'qux'),
259+
])
260+
);
261+
262+
$called = false;
263+
264+
$response->assertInertia(function (AssertableInertia $inertia) use (&$called) {
265+
$inertia->where('foo', 'bar');
266+
$inertia->missing('lazy1');
267+
$inertia->missing('lazy2');
268+
269+
$inertia->reloadExcept('lazy1', function ($inertia) use (&$called) {
270+
$inertia->where('foo', 'bar');
271+
$inertia->missing('lazy1');
272+
$inertia->where('lazy2', 'qux');
273+
$called = true;
274+
});
275+
});
276+
277+
$this->assertTrue($called);
215278
}
216279
}

0 commit comments

Comments
 (0)