forked from livewire/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting.blade.php
executable file
·225 lines (167 loc) · 6.53 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
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 a handy PHPUnit method to test for a component's 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');
}
}
@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
## 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"
->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)
->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
->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
->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
->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(...))
@endcomponent