-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathComponentTest.php
More file actions
197 lines (137 loc) · 6.67 KB
/
ComponentTest.php
File metadata and controls
197 lines (137 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace Inertia\Tests;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Config;
use Inertia\Ssr\Gateway;
use Inertia\Ssr\SsrState;
use Inertia\Tests\Stubs\FakeGateway;
class ComponentTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$this->app->bind(Gateway::class, FakeGateway::class);
}
/**
* @param array<string, mixed> $data
*/
protected function renderView(string $contents, array $data = []): string
{
app(SsrState::class)->setPage($data['page'] ?? []);
return Blade::render($contents, $data, true);
}
public function test_head_component_renders_fallback_slot_when_ssr_is_disabled(): void
{
Config::set(['inertia.ssr.enabled' => false]);
$view = '<x-inertia::head><title>Fallback Title</title></x-inertia::head>';
$this->assertStringContainsString(
'<title>Fallback Title</title>',
$this->renderView($view, ['page' => self::EXAMPLE_PAGE_OBJECT])
);
}
public function test_head_component_renders_ssr_head_when_ssr_is_enabled(): void
{
Config::set(['inertia.ssr.enabled' => true]);
$view = '<x-inertia::head><title>Fallback Title</title></x-inertia::head>';
$rendered = $this->renderView($view, ['page' => self::EXAMPLE_PAGE_OBJECT]);
$this->assertStringContainsString('<title inertia>Example SSR Title</title>', $rendered);
$this->assertStringNotContainsString('<title>Fallback Title</title>', $rendered);
}
public function test_app_component_renders_client_side_div_when_ssr_is_disabled(): void
{
Config::set(['inertia.ssr.enabled' => false]);
$view = '<x-inertia::app />';
$rendered = $this->renderView($view, ['page' => self::EXAMPLE_PAGE_OBJECT]);
$this->assertStringContainsString('<div id="app"></div>', $rendered);
$this->assertStringContainsString('data-page="app"', $rendered);
}
public function test_app_component_renders_ssr_body_when_ssr_is_enabled(): void
{
Config::set(['inertia.ssr.enabled' => true]);
$view = '<x-inertia::app />';
$this->assertSame(
'<p>This is some example SSR content</p>',
trim($this->renderView($view, ['page' => self::EXAMPLE_PAGE_OBJECT]))
);
}
public function test_app_component_accepts_custom_id(): void
{
Config::set(['inertia.ssr.enabled' => false]);
$view = '<x-inertia::app id="custom" />';
$rendered = $this->renderView($view, ['page' => self::EXAMPLE_PAGE_OBJECT]);
$this->assertStringContainsString('<div id="custom"></div>', $rendered);
$this->assertStringContainsString('data-page="custom"', $rendered);
}
public function test_ssr_is_only_dispatched_once_with_components(): void
{
Config::set(['inertia.ssr.enabled' => true]);
$this->app->instance(Gateway::class, $gateway = new FakeGateway);
$view = '<x-inertia::head><title>Fallback</title></x-inertia::head><x-inertia::app />';
$this->renderView($view, ['page' => self::EXAMPLE_PAGE_OBJECT]);
$this->assertSame(1, $gateway->times);
}
public function test_app_component_matches_directive_output_when_ssr_is_disabled(): void
{
Config::set(['inertia.ssr.enabled' => false]);
$directive = $this->renderView('@inertia', ['page' => self::EXAMPLE_PAGE_OBJECT]);
$this->app->forgetScopedInstances();
$component = trim($this->renderView('<x-inertia::app />', ['page' => self::EXAMPLE_PAGE_OBJECT]));
$this->assertSame($directive, $component);
}
public function test_app_component_matches_directive_output_when_ssr_is_enabled(): void
{
Config::set(['inertia.ssr.enabled' => true]);
$directive = $this->renderView('@inertia', ['page' => self::EXAMPLE_PAGE_OBJECT]);
$this->app->forgetScopedInstances();
$component = trim($this->renderView('<x-inertia::app />', ['page' => self::EXAMPLE_PAGE_OBJECT]));
$this->assertSame($directive, $component);
}
public function test_app_component_with_custom_id_matches_directive_output(): void
{
Config::set(['inertia.ssr.enabled' => false]);
$directive = $this->renderView('@inertia("foo")', ['page' => self::EXAMPLE_PAGE_OBJECT]);
$this->app->forgetScopedInstances();
$component = trim($this->renderView('<x-inertia::app id="foo" />', ['page' => self::EXAMPLE_PAGE_OBJECT]));
$this->assertSame($directive, $component);
}
public function test_head_component_without_slot_matches_directive_output_when_ssr_is_disabled(): void
{
Config::set(['inertia.ssr.enabled' => false]);
$directive = $this->renderView('@inertiaHead', ['page' => self::EXAMPLE_PAGE_OBJECT]);
$this->app->forgetScopedInstances();
$component = trim($this->renderView('<x-inertia::head />', ['page' => self::EXAMPLE_PAGE_OBJECT]));
$this->assertSame($directive, $component);
}
public function test_head_component_without_slot_matches_directive_output_when_ssr_is_enabled(): void
{
Config::set(['inertia.ssr.enabled' => true]);
$directive = trim($this->renderView('@inertiaHead', ['page' => self::EXAMPLE_PAGE_OBJECT]));
$this->app->forgetScopedInstances();
$component = trim($this->renderView('<x-inertia::head />', ['page' => self::EXAMPLE_PAGE_OBJECT]));
$this->assertSame($directive, $component);
}
public function test_components_do_not_create_cached_view_files_per_request(): void
{
Config::set(['inertia.ssr.enabled' => true]);
$viewCachePath = $this->app['config']['view.compiled'];
$view = '<x-inertia::head><title>Fallback</title></x-inertia::head><x-inertia::app />';
$this->renderView($view, ['page' => self::EXAMPLE_PAGE_OBJECT]);
$cachedViews = glob($viewCachePath.'/*.php');
$this->app->forgetScopedInstances();
$this->renderView($view, ['page' => ['component' => 'Different', 'props' => ['foo' => 'bar']]]);
$this->assertSame($cachedViews, glob($viewCachePath.'/*.php'));
}
public function test_ssr_state_is_scoped_and_does_not_leak_between_requests(): void
{
Config::set(['inertia.ssr.enabled' => true]);
$state1 = app(SsrState::class);
$state1->setPage(self::EXAMPLE_PAGE_OBJECT);
$state1->dispatch();
$this->assertNotNull($state1->response);
// Simulate Octane request boundary by flushing scoped instances
$this->app->forgetScopedInstances();
$state2 = app(SsrState::class);
$this->assertNotSame($state1, $state2);
$this->assertNull($state2->response);
}
}