Skip to content

Commit 96c9502

Browse files
committed
Added AssertableInertia::partial() test helper
1 parent 657d1e4 commit 96c9502

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

src/Testing/AssertableInertia.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Inertia\Testing;
44

5+
use Closure;
6+
use Illuminate\Foundation\Application;
57
use Illuminate\Testing\Fluent\AssertableJson;
68
use Illuminate\Testing\TestResponse;
79
use InvalidArgumentException;
@@ -81,6 +83,29 @@ public function version(string $value): self
8183
return $this;
8284
}
8385

86+
public function partial(array|string $key, ?Closure $callback = null, ?Application $app = null): self
87+
{
88+
$partialRequest = new PartialRequest(
89+
is_array($key) ? implode(',', $key) : $key,
90+
$this->url,
91+
$this->component,
92+
$this->version,
93+
$app ?? app()
94+
);
95+
96+
$assertable = static::fromTestResponse($partialRequest());
97+
$assertable->component($this->component);
98+
$assertable->url($this->url);
99+
$assertable->version($this->version);
100+
$assertable->hasAll(is_array($key) ? $key : explode(',', $key));
101+
102+
if ($callback) {
103+
$callback($assertable);
104+
}
105+
106+
return $this;
107+
}
108+
84109
public function toArray()
85110
{
86111
return [

src/Testing/PartialRequest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
9+
class PartialRequest
10+
{
11+
use MakesHttpRequests;
12+
13+
public function __construct(
14+
protected string $props,
15+
protected string $url,
16+
protected string $component,
17+
protected string $version,
18+
protected Application $app
19+
) {
20+
}
21+
22+
public function __invoke(): TestResponse
23+
{
24+
return $this->get($this->url, [
25+
'X-Inertia-Partial-Data' => $this->props,
26+
'X-Inertia-Partial-Component' => $this->component,
27+
'X-Inertia-Version' => $this->version,
28+
]);
29+
}
30+
}

tests/Testing/AssertableInertiaTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,24 @@ public function test_the_asset_version_does_not_match(): void
193193
$inertia->version('different-version');
194194
});
195195
}
196+
197+
public function test_lazy_props_can_be_evaluated(): void
198+
{
199+
$response = $this->makeMockRequest(
200+
Inertia::render('foo', [
201+
'foo' => 'bar',
202+
'lazy' => Inertia::lazy(fn () => 'baz'),
203+
])
204+
);
205+
206+
$response->assertInertia(function ($inertia) {
207+
$inertia->where('foo', 'bar');
208+
$inertia->missing('lazy');
209+
210+
$inertia->partial('lazy', function ($inertia) {
211+
$inertia->where('lazy', 'baz');
212+
$inertia->missing('foo');
213+
});
214+
});
215+
}
196216
}

0 commit comments

Comments
 (0)