diff --git a/src/ProvidesInertiaProperties.php b/src/ProvidesInertiaProperties.php index 9ad59673..f3fac00a 100644 --- a/src/ProvidesInertiaProperties.php +++ b/src/ProvidesInertiaProperties.php @@ -9,7 +9,7 @@ interface ProvidesInertiaProperties * to dynamically provide properties that will be serialized and sent * to the frontend. * - * @return array + * @return iterable */ public function toInertiaProperties(RenderContext $context): iterable; } diff --git a/src/Testing/AssertableInertia.php b/src/Testing/AssertableInertia.php index 87e05e2f..4f455e85 100644 --- a/src/Testing/AssertableInertia.php +++ b/src/Testing/AssertableInertia.php @@ -4,6 +4,7 @@ use Closure; use Illuminate\Http\Response; +use Illuminate\Support\Arr; use Illuminate\Testing\Fluent\AssertableJson; use Illuminate\Testing\TestResponse; use InvalidArgumentException; @@ -47,6 +48,13 @@ class AssertableInertia extends AssertableJson */ private $clearHistory; + /** + * The deferred props (if any). + * + * @var array> + */ + private $deferredProps; + /** * Create an AssertableInertia instance from a test response. * @@ -75,6 +83,7 @@ public static function fromTestResponse(TestResponse $response): self $instance->version = $page['version']; $instance->encryptHistory = $page['encryptHistory']; $instance->clearHistory = $page['clearHistory']; + $instance->deferredProps = $page['deferredProps'] ?? []; return $instance; } @@ -119,6 +128,24 @@ public function version(string $value): self return $this; } + /** + * Load the deferred props for the given groups and perform assertions on the response. + * + * @param Closure|array|string $groupsOrCallback + */ + public function loadDeferredProps(Closure|array|string $groupsOrCallback, ?Closure $callback = null): self + { + $callback = is_callable($groupsOrCallback) ? $groupsOrCallback : $callback; + + $groups = is_callable($groupsOrCallback) ? array_keys($this->deferredProps) : Arr::wrap($groupsOrCallback); + + $props = collect($groups)->flatMap(function ($group) { + return $this->deferredProps[$group] ?? []; + })->implode(','); + + return $this->reloadOnly($props, $callback); + } + /** * Reload the Inertia page and perform assertions on the response. * diff --git a/tests/Stubs/ExampleInertiaPropsProvider.php b/tests/Stubs/ExampleInertiaPropsProvider.php index abd1854a..453da325 100644 --- a/tests/Stubs/ExampleInertiaPropsProvider.php +++ b/tests/Stubs/ExampleInertiaPropsProvider.php @@ -15,7 +15,7 @@ public function __construct( ) {} /** - * @return array + * @return iterable */ public function toInertiaProperties(RenderContext $context): iterable { diff --git a/tests/Testing/AssertableInertiaTest.php b/tests/Testing/AssertableInertiaTest.php index 2d385034..c4e6644c 100644 --- a/tests/Testing/AssertableInertiaTest.php +++ b/tests/Testing/AssertableInertiaTest.php @@ -276,4 +276,55 @@ public function test_lazy_props_can_be_evaluated_with_except(): void $this->assertTrue($called); } + + public function test_assert_against_deferred_props(): void + { + $response = $this->makeMockRequest( + Inertia::render('foo', [ + 'foo' => 'bar', + 'deferred1' => Inertia::defer(fn () => 'baz'), + 'deferred2' => Inertia::defer(fn () => 'qux', 'custom'), + 'deferred3' => Inertia::defer(fn () => 'quux', 'custom'), + ]) + ); + + $called = 0; + + $response->assertInertia(function (AssertableInertia $inertia) use (&$called) { + $inertia->where('foo', 'bar'); + $inertia->missing('deferred1'); + $inertia->missing('deferred2'); + $inertia->missing('deferred3'); + + $inertia->loadDeferredProps(function (AssertableInertia $inertia) use (&$called) { + $inertia->where('deferred1', 'baz'); + $inertia->where('deferred2', 'qux'); + $inertia->where('deferred3', 'quux'); + $called++; + }); + + $inertia->loadDeferredProps('default', function (AssertableInertia $inertia) use (&$called) { + $inertia->where('deferred1', 'baz'); + $inertia->missing('deferred2'); + $inertia->missing('deferred3'); + $called++; + }); + + $inertia->loadDeferredProps('custom', function (AssertableInertia $inertia) use (&$called) { + $inertia->missing('deferred1'); + $inertia->where('deferred2', 'qux'); + $inertia->where('deferred3', 'quux'); + $called++; + }); + + $inertia->loadDeferredProps(['default', 'custom'], function (AssertableInertia $inertia) use (&$called) { + $inertia->where('deferred1', 'baz'); + $inertia->where('deferred2', 'qux'); + $inertia->where('deferred3', 'quux'); + $called++; + }); + }); + + $this->assertSame(4, $called); + } }