Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ProvidesInertiaProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface ProvidesInertiaProperties
* to dynamically provide properties that will be serialized and sent
* to the frontend.
*
* @return array<string, mixed>
* @return iterable<string, mixed>
*/
public function toInertiaProperties(RenderContext $context): iterable;
}
27 changes: 27 additions & 0 deletions src/Testing/AssertableInertia.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,6 +48,13 @@ class AssertableInertia extends AssertableJson
*/
private $clearHistory;

/**
* The deferred props (if any).
*
* @var array<string, array<int, string>>
*/
private $deferredProps;

/**
* Create an AssertableInertia instance from a test response.
*
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<int, string>|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.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/Stubs/ExampleInertiaPropsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct(
) {}

/**
* @return array<string, mixed>
* @return iterable<string, mixed>
*/
public function toInertiaProperties(RenderContext $context): iterable
{
Expand Down
51 changes: 51 additions & 0 deletions tests/Testing/AssertableInertiaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}