Skip to content

Commit d38a788

Browse files
authored
Make clearHistory and encryptHistory optional in page object (#834)
1 parent 85c2bad commit d38a788

8 files changed

Lines changed: 80 additions & 68 deletions

File tree

src/Response.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,10 @@ public function toResponse($request)
183183
'props' => $resolvedProps,
184184
'url' => $this->getUrl($request),
185185
'version' => $this->version,
186-
'clearHistory' => $this->clearHistory,
187-
'encryptHistory' => $this->encryptHistory,
188186
],
189187
$resolvedMetadata,
188+
$this->resolveClearHistory($request),
189+
$this->resolveEncryptHistory($request),
190190
$this->resolveFlashData($request),
191191
$this->resolvePreserveFragment($request),
192192
);
@@ -198,6 +198,26 @@ public function toResponse($request)
198198
return ResponseFactory::view($this->rootView, $this->viewData + ['page' => $page]);
199199
}
200200

201+
/**
202+
* Resolve the clear history flag.
203+
*
204+
* @return array<string, mixed>
205+
*/
206+
protected function resolveClearHistory(Request $request): array
207+
{
208+
return $this->clearHistory ? ['clearHistory' => true] : [];
209+
}
210+
211+
/**
212+
* Resolve the encrypt history flag.
213+
*
214+
* @return array<string, mixed>
215+
*/
216+
protected function resolveEncryptHistory(Request $request): array
217+
{
218+
return $this->encryptHistory ? ['encryptHistory' => true] : [];
219+
}
220+
201221
/**
202222
* Resolve flash data from the session.
203223
*

src/Testing/AssertableInertia.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ public static function fromTestResponse(TestResponse $response): self
7878
PHPUnit::assertArrayHasKey('props', $page);
7979
PHPUnit::assertArrayHasKey('url', $page);
8080
PHPUnit::assertArrayHasKey('version', $page);
81-
PHPUnit::assertArrayHasKey('encryptHistory', $page);
82-
PHPUnit::assertArrayHasKey('clearHistory', $page);
8381
} catch (AssertionFailedError $e) {
8482
PHPUnit::fail('Not a valid Inertia response.');
8583
}
@@ -88,8 +86,8 @@ public static function fromTestResponse(TestResponse $response): self
8886
$instance->component = $page['component'];
8987
$instance->url = $page['url'];
9088
$instance->version = $page['version'];
91-
$instance->encryptHistory = $page['encryptHistory'];
92-
$instance->clearHistory = $page['clearHistory'];
89+
$instance->encryptHistory = isset($page['encryptHistory']);
90+
$instance->clearHistory = isset($page['clearHistory']);
9391
$instance->deferredProps = $page['deferredProps'] ?? [];
9492
$instance->flash = $page['flash'] ?? [];
9593

@@ -291,14 +289,16 @@ public static function assertFlashMissing(array $flash, string $key): void
291289
*/
292290
public function toArray()
293291
{
294-
return [
295-
'component' => $this->component,
296-
'props' => $this->prop(),
297-
'url' => $this->url,
298-
'version' => $this->version,
299-
'encryptHistory' => $this->encryptHistory,
300-
'clearHistory' => $this->clearHistory,
301-
'flash' => $this->flash,
302-
];
292+
return array_merge(
293+
[
294+
'component' => $this->component,
295+
'props' => $this->prop(),
296+
'url' => $this->url,
297+
'version' => $this->version,
298+
'flash' => $this->flash,
299+
],
300+
$this->encryptHistory ? ['encryptHistory' => true] : [],
301+
$this->clearHistory ? ['clearHistory' => true] : [],
302+
);
303303
}
304304
}

tests/ControllerTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ public function test_controller_returns_an_inertia_response(): void
2828
],
2929
'url' => '/',
3030
'version' => '',
31-
'encryptHistory' => false,
32-
'clearHistory' => false,
3331
]);
3432
}
3533
}

tests/DirectiveTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function test_inertia_directive_renders_the_root_element(): void
5555
{
5656
Config::set(['inertia.ssr.enabled' => false]);
5757

58-
$html = '<script data-page="app" type="application/json">{"component":"Foo\/Bar","props":{"foo":"bar"},"url":"\/test","version":"","encryptHistory":false,"clearHistory":false}</script><div id="app"></div>';
58+
$html = '<script data-page="app" type="application/json">{"component":"Foo\/Bar","props":{"foo":"bar"},"url":"\/test","version":""}</script><div id="app"></div>';
5959

6060
$this->assertSame($html, $this->renderView('@inertia', ['page' => self::EXAMPLE_PAGE_OBJECT]));
6161
$this->assertSame($html, $this->renderView('@inertia()', ['page' => self::EXAMPLE_PAGE_OBJECT]));
@@ -77,7 +77,7 @@ public function test_inertia_directive_can_use_a_different_root_element_id(): vo
7777
{
7878
Config::set(['inertia.ssr.enabled' => false]);
7979

80-
$html = '<script data-page="foo" type="application/json">{"component":"Foo\/Bar","props":{"foo":"bar"},"url":"\/test","version":"","encryptHistory":false,"clearHistory":false}</script><div id="foo"></div>';
80+
$html = '<script data-page="foo" type="application/json">{"component":"Foo\/Bar","props":{"foo":"bar"},"url":"\/test","version":""}</script><div id="foo"></div>';
8181

8282
$this->assertSame($html, $this->renderView('@inertia(foo)', ['page' => self::EXAMPLE_PAGE_OBJECT]));
8383
$this->assertSame($html, $this->renderView("@inertia('foo')", ['page' => self::EXAMPLE_PAGE_OBJECT]));

tests/HistoryTest.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@ public function test_the_history_is_not_encrypted_or_cleared_by_default(): void
2222
]);
2323

2424
$response->assertSuccessful();
25-
$response->assertJson([
26-
'component' => 'User/Edit',
27-
'encryptHistory' => false,
28-
'clearHistory' => false,
29-
]);
25+
$response->assertJsonMissing(['encryptHistory' => true]);
26+
$response->assertJsonMissing(['clearHistory' => true]);
3027
}
3128

3229
public function test_the_history_can_be_encrypted(): void
@@ -116,10 +113,7 @@ public function test_the_history_can_be_encrypted_globally_and_overridden(): voi
116113
]);
117114

118115
$response->assertSuccessful();
119-
$response->assertJson([
120-
'component' => 'User/Edit',
121-
'encryptHistory' => false,
122-
]);
116+
$response->assertJsonMissing(['encryptHistory' => true]);
123117
}
124118

125119
public function test_the_history_can_be_cleared(): void
@@ -160,7 +154,7 @@ public function test_the_history_can_be_cleared_when_redirecting(): void
160154
]);
161155

162156
$response->assertSuccessful();
163-
$response->assertContent('<script data-page="app" type="application/json">{"component":"User\/Edit","props":{"errors":{}},"url":"\/users","version":"","clearHistory":true,"encryptHistory":false}</script><div id="app"></div>');
157+
$response->assertContent('<script data-page="app" type="application/json">{"component":"User\/Edit","props":{"errors":{}},"url":"\/users","version":"","clearHistory":true}</script><div id="app"></div>');
164158
}
165159

166160
public function test_the_fragment_is_not_preserved_by_default(): void

0 commit comments

Comments
 (0)