-
Notifications
You must be signed in to change notification settings - Fork 224
/
testing.blade.php
executable file
·326 lines (249 loc) · 9.43 KB
/
testing.blade.php
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
* [Introduction](#introduction)
* [Testing Component Presence](#testing-component-presence)
* [Testing With Query String Parameters](#testing-querystring)
* [Testing Components With Passed Data](#testing-passed-data)
* [Generating Tests](#generating-tests)
* [All Available Test Methods](#all-testing-methods)
## Introduction {#introduction}
Livewire offers a powerful set of tools for testing your components.
Here's a Livewire component and a corresponding test to demonstrate the basics.
@component('components.code-component')
@slot('class')
@verbatim
class CreatePost extends Component
{
public $title;
protected $rules = [
'title' => 'required',
];
public function create()
{
auth()->user()->posts()->create(
$this->validate()
);
return redirect()->to('/posts');
}
}
@endverbatim
@endslot
@slot('view')
@verbatim
<form wire:submit.prevent="create">
<input wire:model="title" type="text">
<button>Create Post</button>
</form>
@endverbatim
@endslot
@endcomponent
@component('components.code', ['lang' => 'php'])
class CreatePostTest extends TestCase
{
/** @test */
function can_create_post()
{
$this->actingAs(User::factory()->create());
Livewire::test(CreatePost::class)
->set('title', 'foo')
->call('create');
$this->assertTrue(Post::whereTitle('foo')->exists());
}
/** @test */
function can_set_initial_title()
{
$this->actingAs(User::factory()->create());
Livewire::test(CreatePost::class, ['initialTitle' => 'foo'])
->assertSet('title', 'foo');
}
/** @test */
function title_is_required()
{
$this->actingAs(User::factory()->create());
Livewire::test(CreatePost::class)
->set('title', '')
->call('create')
->assertHasErrors(['title' => 'required']);
}
/** @test */
function is_redirected_to_posts_page_after_creation()
{
$this->actingAs(User::factory()->create());
Livewire::test(CreatePost::class)
->set('title', 'foo')
->call('create')
->assertRedirect('/posts');
}
}
@endcomponent
## Testing Component Presence {#testing-component-presence}
Livewire registers handy PHPUnit methods for testing a components presence on a page.
@component('components.code', ['lang' => 'php'])
class CreatePostTest extends TestCase
{
/** @test */
function post_creation_page_contains_livewire_component()
{
$this->get('/posts/create')->assertSeeLivewire('create-post');
}
/** @test */
function post_creation_page_doesnt_contain_livewire_component()
{
$this->get('/posts/create')->assertDontSeeLivewire('edit-post');
}
}
@endcomponent
Alternatively, you may pass a component's class name to the `assertSeeLivewire` and `assertDontSeeLivewire` methods.
@component('components.code', ['lang' => 'php'])
use App\Http\Livewire\CreatePost;
use App\Http\Livewire\EditPost;
class CreatePostTest extends TestCase
{
/** @test */
function post_creation_page_contains_livewire_component()
{
$this->get('/posts/create')->assertSeeLivewire(CreatePost::class);
}
/** @test */
function post_creation_page_doesnt_contain_livewire_component()
{
$this->get('/posts/create')->assertDontSeeLivewire(EditPost::class);
}
}
@endcomponent
## Testing With Query String Parameters {#testing-querystring}
To test Livewire's `$queryString` functionality, you can use Livewire's `::withQueryParams` testing utility.
@component('components.code', ['lang' => 'php'])
class CreatePostTest extends TestCase
{
/** @test */
function post_creation_page_contains_livewire_component()
{
Livewire::withQueryParams(['foo' => 'bar'])
->test(ShowFoo::class)
->assertSet('foo', 'bar')
->assertSee('bar');
}
}
@endcomponent
## Testing Components With Passed Data {#testing-passed-data}
@component('components.code-component')
@slot('view')
@verbatim
<livewire:show-foo foo="bar">
@endverbatim
@endslot
@slot('class')
class CreatePostTest extends TestCase
{
/** @test */
function has_data_passed_correctly()
{
Livewire::test(ShowFoo::class, ['foo' => 'bar'])
->assertSet('foo', 'bar')
->assertSee('bar');
}
}
@endslot
@endcomponent
## Generating Tests {#generating-tests}
When creating a component, you can include the `--test` flag, and a test file will be created for you as well.
@component('components.code', ['lang' => 'shell'])
php artisan make:livewire ShowPosts --test
@endcomponent
@component('components.code-component', ['lang' => 'php', 'className' => 'tests/Feature/Livewire/ShowPostsTest.php'])
@slot('class')
class ShowPostsTest extends TestCase
{
/** @test */
public function the_component_can_render()
{
$component = Livewire::test(ShowPosts::class);
$component->assertStatus(200);
}
}
@endslot
@endcomponent
## All Available Test Methods {#all-testing-methods}
@component('components.code', ['lang' => 'php'])
Livewire::actingAs($user);
// Set the provided user as the session's logged in user for the test
Livewire::withQueryParams(['foo' => 'bar']);
// Set the query param "foo" to "bar" for the Livewire component's `$queryString` property to pick up.
Livewire::test('foo', ['bar' => $bar]);
// Test the "foo" component with "bar" set as a parameter.
->set('foo', 'bar');
// Set the "foo" property (`public $foo`) to the value: "bar"
->toggle('foo');
// Toggle the "foo" property (`public $foo`) between true and false
->call('foo');
// Call the "foo" method
->call('foo', 'bar', 'baz');
// Call the "foo" method, and pass the "bar" and "baz" parameters
->emit('foo');
// Fire the "foo" event
->emit('foo', 'bar', 'baz');
// Fire the "foo" event, and pass the "bar" and "baz" parameters
->assertSet('foo', 'bar');
// Asserts that the "foo" property is set to the value "bar" (Includes computed properties)
->assertNotSet('foo', 'bar');
// Asserts that the "foo" property is NOT set to the value "bar" (Includes computed properties)
->assertCount('foo', 1);
// Asserts that the "foo" property (an array) has a count of 1 (Includes computed properties)
->assertPayloadSet('foo', 'bar');
// Asserts that the "foo" property from the JavaScript payload that Livewire returns is set to the value "bar"
->assertPayloadNotSet('foo', 'bar');
// Asserts that the "foo" property in the JavaScript payload that Livewire returns is NOT set to the value "bar"
->assertViewIs('foo');
// Assert that the view "foo" is the currently rendered view
->assertViewHas('foo', 'bar');
// Assert that the rendered view has a key of "foo" with a value of "bar"
->assertSee('foo');
// Assert that the string "foo" exists in the currently rendered content of the component
->assertDontSee('foo');
// Assert that the string "foo" DOES NOT exist in the currently rendered content of the component
->assertSeeHtml('<h1>foo</h1>');
// Assert that the string "<h1>foo</h1>" exists in the currently rendered HTML of the component
->assertDontSeeHtml('<h1>foo</h1>');
// Assert that the string "<h1>foo</h1>" DOES NOT exist in the currently rendered HTML of the component
->assertSeeInOrder(['foo', 'bar']);
// Assert that the string "foo" exists before "bar" in the currently rendered content of the component
->assertSeeHtmlInOrder(['<h1>foo</h1>', '<h1>bar</h1>']);
// Assert that the string "<h1>foo</h1>" exists before "<h1>bar</h1>" in the currently rendered content of the component
->assertEmitted('foo');
// Assert that the "foo" event was emitted
->assertEmitted('foo', 'bar', 'baz');
// Assert that the "foo" event was emitted with the "bar" and "baz" parameters
->assertNotEmitted('foo');
// Assert that the "foo" event was NOT emitted
->assertEmittedTo('bar','foo');
// Assert that the "foo" event was emitted to "bar" component
->assertHasErrors('foo');
// Assert that the "foo" property has validation errors
->assertHasErrors(['foo', 'bar']);
// Assert that the "foo" AND "bar" properties have validation errors
->assertHasErrors(['foo' => 'required']);
// Assert that the "foo" property has a "required" validation rule error
->assertHasErrors(['foo' => ['required', 'min']]);
// Assert that the "foo" property has a "required" AND "min" validation rule error
->assertHasNoErrors('foo');
// Assert that the "foo" property has no validation errors
->assertHasNoErrors(['foo', 'bar']);
// Assert that the "foo" AND "bar" properties have no validation errors
->assertNotFound();
// Assert that an error within the component caused an error with the status code: 404
->assertRedirect('/some-path');
// Assert that a redirect was triggered from the component
->assertNoRedirect();
// Assert that no redirect was triggered from the component
->assertUnauthorized();
// Assert that an error within the component caused an error with the status code: 401
->assertForbidden();
// Assert that an error within the component caused an error with the status code: 403
->assertStatus(500);
// Assert that an error within the component caused an error with the status code: 500
->assertDispatchedBrowserEvent('event', $data);
// Assert that a browser event was dispatched from the component using (->dispatchBrowserEvent(...))
->assertNotDispatchedBrowserEvent('event');
// Assert that a browser event was not dispatched from the component using (->dispatchBrowserEvent(...))
->assertFileDownloaded($filename)
// Assert that a downloaded file was returned with a specific name
@endcomponent