Skip to content

Commit 82be2ef

Browse files
hosmelqcndrsdrmn
andauthored
handle array inputs in reloadOnly and reloadExcept (#792)
Co-authored-by: Candra Sudirman <[email protected]>
1 parent 02bdec1 commit 82be2ef

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

src/Testing/AssertableInertia.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@ public function reload(?Closure $callback = null, array|string|null $only = null
192192
public function reloadOnly(array|string $only, ?Closure $callback = null): self
193193
{
194194
return $this->reload(only: $only, callback: function (AssertableInertia $assertable) use ($only, $callback) {
195-
$assertable->hasAll(explode(',', $only));
195+
$props = is_array($only) ? $only : explode(',', $only);
196+
197+
$assertable->hasAll($props);
196198

197199
if ($callback) {
198200
$callback($assertable);
@@ -208,7 +210,9 @@ public function reloadOnly(array|string $only, ?Closure $callback = null): self
208210
public function reloadExcept(array|string $except, ?Closure $callback = null): self
209211
{
210212
return $this->reload(except: $except, callback: function (AssertableInertia $assertable) use ($except, $callback) {
211-
$assertable->missingAll(explode(',', $except));
213+
$props = is_array($except) ? $except : explode(',', $except);
214+
215+
$assertable->missingAll($props);
212216

213217
if ($callback) {
214218
$callback($assertable);

tests/Testing/AssertableInertiaTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,36 @@ public function test_lazy_props_can_be_evaluated(): void
249249
$this->assertTrue($called);
250250
}
251251

252+
public function test_lazy_props_can_be_evaluated_when_only_is_array(): void
253+
{
254+
$response = $this->makeMockRequest(
255+
Inertia::render('foo', [
256+
'foo' => 'bar',
257+
'lazy1' => Inertia::optional(fn () => 'baz'),
258+
'lazy2' => Inertia::optional(fn () => 'qux'),
259+
])
260+
);
261+
262+
$called = false;
263+
264+
$response->assertInertia(function ($inertia) use (&$called) {
265+
$inertia->where('foo', 'bar');
266+
$inertia->missing('lazy1');
267+
$inertia->missing('lazy2');
268+
269+
$result = $inertia->reloadOnly(['lazy1'], function ($inertia) use (&$called) {
270+
$inertia->missing('foo');
271+
$inertia->where('lazy1', 'baz');
272+
$inertia->missing('lazy2');
273+
$called = true;
274+
});
275+
276+
$this->assertSame($result, $inertia);
277+
});
278+
279+
$this->assertTrue($called);
280+
}
281+
252282
public function test_lazy_props_can_be_evaluated_with_except(): void
253283
{
254284
$response = $this->makeMockRequest(
@@ -277,6 +307,34 @@ public function test_lazy_props_can_be_evaluated_with_except(): void
277307
$this->assertTrue($called);
278308
}
279309

310+
public function test_lazy_props_can_be_evaluated_with_except_when_except_is_array(): void
311+
{
312+
$response = $this->makeMockRequest(
313+
Inertia::render('foo', [
314+
'foo' => 'bar',
315+
'lazy1' => Inertia::lazy(fn () => 'baz'),
316+
'lazy2' => Inertia::lazy(fn () => 'qux'),
317+
])
318+
);
319+
320+
$called = false;
321+
322+
$response->assertInertia(function ($inertia) use (&$called) {
323+
$inertia->where('foo', 'bar');
324+
$inertia->missing('lazy1');
325+
$inertia->missing('lazy2');
326+
327+
$inertia->reloadExcept(['lazy1'], function ($inertia) use (&$called) {
328+
$inertia->where('foo', 'bar');
329+
$inertia->missing('lazy1');
330+
$inertia->where('lazy2', 'qux');
331+
$called = true;
332+
});
333+
});
334+
335+
$this->assertTrue($called);
336+
}
337+
280338
public function test_assert_against_deferred_props(): void
281339
{
282340
$response = $this->makeMockRequest(

0 commit comments

Comments
 (0)