Skip to content

Commit 04d84a3

Browse files
Add test
1 parent 95898e5 commit 04d84a3

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ public function resolveOnceProps(Request $request): array
388388
? App::call($this->oncePropsResolver, ['request' => $request])
389389
: [];
390390

391-
return ['onceProps' => $onceProps];
391+
return empty($onceProps) ? [] : ['onceProps' => $onceProps];
392392
}
393393

394394
/**

tests/MiddlewareTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Inertia\AlwaysProp;
1313
use Inertia\Inertia;
1414
use Inertia\Middleware;
15+
use Inertia\Tests\Stubs\CustomOncePropsResolverMiddleware;
1516
use Inertia\Tests\Stubs\CustomUrlResolverMiddleware;
1617
use Inertia\Tests\Stubs\ExampleMiddleware;
1718
use LogicException;
@@ -146,6 +147,19 @@ public function test_the_url_can_be_resolved_with_a_custom_resolver()
146147
]);
147148
}
148149

150+
public function test_the_once_props_can_be_resolved_with_a_custom_resolver()
151+
{
152+
$this->prepareMockEndpoint(middleware: new CustomOncePropsResolverMiddleware);
153+
154+
$response = $this->withoutExceptionHandling()->get('/');
155+
156+
$response->assertSuccessful();
157+
$this->assertSame(
158+
'<div id="app" data-page="{&quot;component&quot;:&quot;User\/Edit&quot;,&quot;props&quot;:{&quot;errors&quot;:{},&quot;user&quot;:{&quot;name&quot;:&quot;Jonathan&quot;}},&quot;url&quot;:&quot;\/&quot;,&quot;version&quot;:&quot;&quot;,&quot;clearHistory&quot;:false,&quot;encryptHistory&quot;:false,&quot;onceProps&quot;:{&quot;once&quot;:true,&quot;appName&quot;:&quot;test&quot;}}"></div>',
159+
$response->content(),
160+
);
161+
}
162+
149163
public function test_validation_errors_are_registered_as_of_default(): void
150164
{
151165
Route::middleware([StartSession::class, ExampleMiddleware::class])->get('/', function () {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Inertia\Tests\Stubs;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Routing\ResponseFactory;
7+
use Inertia\Middleware;
8+
use PHPUnit\Framework\Assert;
9+
10+
class CustomOncePropsResolverMiddleware extends Middleware
11+
{
12+
public function oncePropsResolver()
13+
{
14+
return function ($request, ResponseFactory $otherDependency) {
15+
Assert::assertInstanceOf(Request::class, $request);
16+
Assert::assertInstanceOf(ResponseFactory::class, $otherDependency);
17+
18+
return [
19+
'once' => true,
20+
'appName' => 'test',
21+
];
22+
};
23+
}
24+
}

0 commit comments

Comments
 (0)