Skip to content

Commit a08aea5

Browse files
authored
Added loadDeferredProps() method to AssertableInertia class (#779)
* Added `loadDeferredProps()` method * Update AssertableInertia.php * Fix new PHPStan error
1 parent 5a9c4e5 commit a08aea5

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

src/ProvidesInertiaProperties.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface ProvidesInertiaProperties
99
* to dynamically provide properties that will be serialized and sent
1010
* to the frontend.
1111
*
12-
* @return array<string, mixed>
12+
* @return iterable<string, mixed>
1313
*/
1414
public function toInertiaProperties(RenderContext $context): iterable;
1515
}

src/Testing/AssertableInertia.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Closure;
66
use Illuminate\Http\Response;
7+
use Illuminate\Support\Arr;
78
use Illuminate\Testing\Fluent\AssertableJson;
89
use Illuminate\Testing\TestResponse;
910
use InvalidArgumentException;
@@ -47,6 +48,13 @@ class AssertableInertia extends AssertableJson
4748
*/
4849
private $clearHistory;
4950

51+
/**
52+
* The deferred props (if any).
53+
*
54+
* @var array<string, array<int, string>>
55+
*/
56+
private $deferredProps;
57+
5058
/**
5159
* Create an AssertableInertia instance from a test response.
5260
*
@@ -75,6 +83,7 @@ public static function fromTestResponse(TestResponse $response): self
7583
$instance->version = $page['version'];
7684
$instance->encryptHistory = $page['encryptHistory'];
7785
$instance->clearHistory = $page['clearHistory'];
86+
$instance->deferredProps = $page['deferredProps'] ?? [];
7887

7988
return $instance;
8089
}
@@ -119,6 +128,24 @@ public function version(string $value): self
119128
return $this;
120129
}
121130

131+
/**
132+
* Load the deferred props for the given groups and perform assertions on the response.
133+
*
134+
* @param Closure|array<int, string>|string $groupsOrCallback
135+
*/
136+
public function loadDeferredProps(Closure|array|string $groupsOrCallback, ?Closure $callback = null): self
137+
{
138+
$callback = is_callable($groupsOrCallback) ? $groupsOrCallback : $callback;
139+
140+
$groups = is_callable($groupsOrCallback) ? array_keys($this->deferredProps) : Arr::wrap($groupsOrCallback);
141+
142+
$props = collect($groups)->flatMap(function ($group) {
143+
return $this->deferredProps[$group] ?? [];
144+
})->implode(',');
145+
146+
return $this->reloadOnly($props, $callback);
147+
}
148+
122149
/**
123150
* Reload the Inertia page and perform assertions on the response.
124151
*

tests/Stubs/ExampleInertiaPropsProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function __construct(
1515
) {}
1616

1717
/**
18-
* @return array<string, mixed>
18+
* @return iterable<string, mixed>
1919
*/
2020
public function toInertiaProperties(RenderContext $context): iterable
2121
{

tests/Testing/AssertableInertiaTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,55 @@ public function test_lazy_props_can_be_evaluated_with_except(): void
276276

277277
$this->assertTrue($called);
278278
}
279+
280+
public function test_assert_against_deferred_props(): void
281+
{
282+
$response = $this->makeMockRequest(
283+
Inertia::render('foo', [
284+
'foo' => 'bar',
285+
'deferred1' => Inertia::defer(fn () => 'baz'),
286+
'deferred2' => Inertia::defer(fn () => 'qux', 'custom'),
287+
'deferred3' => Inertia::defer(fn () => 'quux', 'custom'),
288+
])
289+
);
290+
291+
$called = 0;
292+
293+
$response->assertInertia(function (AssertableInertia $inertia) use (&$called) {
294+
$inertia->where('foo', 'bar');
295+
$inertia->missing('deferred1');
296+
$inertia->missing('deferred2');
297+
$inertia->missing('deferred3');
298+
299+
$inertia->loadDeferredProps(function (AssertableInertia $inertia) use (&$called) {
300+
$inertia->where('deferred1', 'baz');
301+
$inertia->where('deferred2', 'qux');
302+
$inertia->where('deferred3', 'quux');
303+
$called++;
304+
});
305+
306+
$inertia->loadDeferredProps('default', function (AssertableInertia $inertia) use (&$called) {
307+
$inertia->where('deferred1', 'baz');
308+
$inertia->missing('deferred2');
309+
$inertia->missing('deferred3');
310+
$called++;
311+
});
312+
313+
$inertia->loadDeferredProps('custom', function (AssertableInertia $inertia) use (&$called) {
314+
$inertia->missing('deferred1');
315+
$inertia->where('deferred2', 'qux');
316+
$inertia->where('deferred3', 'quux');
317+
$called++;
318+
});
319+
320+
$inertia->loadDeferredProps(['default', 'custom'], function (AssertableInertia $inertia) use (&$called) {
321+
$inertia->where('deferred1', 'baz');
322+
$inertia->where('deferred2', 'qux');
323+
$inertia->where('deferred3', 'quux');
324+
$called++;
325+
});
326+
});
327+
328+
$this->assertSame(4, $called);
329+
}
279330
}

0 commit comments

Comments
 (0)