Testing

Livewire offers a powerful set of tools for testing your components.

Here's a Livewire component and a corresponding test to demonstrate the basics.

            
1class CreatePost extends Component
2{
3 public $title;
4 
5 protected $rules = [
6 'title' => 'required',
7 ];
8 
9 public function create()
10 {
11 auth()->user()->posts()->create(
12 $this->validate()
13 );
14 
15 return redirect()->to('/posts');
16 }
17}
            
1<form wire:submit.prevent="create">
2 <input wire:model="title" type="text">
3 
4 <button>Create Post</button>
5</form>
        
1class CreatePostTest extends TestCase
2{
3 /** @test */
4 function can_create_post()
5 {
6 $this->actingAs(User::factory()->create());
7 
8 Livewire::test(CreatePost::class)
9 ->set('title', 'foo')
10 ->call('create');
11 
12 $this->assertTrue(Post::whereTitle('foo')->exists());
13 }
14 
15 /** @test */
16 function can_set_initial_title()
17 {
18 $this->actingAs(User::factory()->create());
19 
20 Livewire::test(CreatePost::class, ['initialTitle' => 'foo'])
21 ->assertSet('title', 'foo');
22 }
23 
24 /** @test */
25 function title_is_required()
26 {
27 $this->actingAs(User::factory()->create());
28 
29 Livewire::test(CreatePost::class)
30 ->set('title', '')
31 ->call('create')
32 ->assertHasErrors(['title' => 'required']);
33 }
34 
35 /** @test */
36 function is_redirected_to_posts_page_after_creation()
37 {
38 $this->actingAs(User::factory()->create());
39 
40 Livewire::test(CreatePost::class)
41 ->set('title', 'foo')
42 ->call('create')
43 ->assertRedirect('/posts');
44 }
45}

Testing Component Presence

Livewire registers handy PHPUnit methods for testing a components presence on a page.

        
1class CreatePostTest extends TestCase
2{
3 /** @test */
4 function post_creation_page_contains_livewire_component()
5 {
6 $this->get('/posts/create')->assertSeeLivewire('create-post');
7 }
8 
9 /** @test */
10 function post_creation_page_doesnt_contain_livewire_component()
11 {
12 $this->get('/posts/create')->assertDontSeeLivewire('edit-post');
13 }
14}

Alternatively, you may pass a component's class name to the assertSeeLivewire and assertDontSeeLivewire methods.

        
1use App\Http\Livewire\CreatePost;
2use App\Http\Livewire\EditPost;
3 
4class CreatePostTest extends TestCase
5{
6 /** @test */
7 function post_creation_page_contains_livewire_component()
8 {
9 $this->get('/posts/create')->assertSeeLivewire(CreatePost::class);
10 }
11 
12 /** @test */
13 function post_creation_page_doesnt_contain_livewire_component()
14 {
15 $this->get('/posts/create')->assertDontSeeLivewire(EditPost::class);
16 }
17}

Testing With Query String Parameters

To test Livewire's $queryString functionality, you can use Livewire's ::withQueryParams testing utility.

        
1class CreatePostTest extends TestCase
2{
3 /** @test */
4 function post_creation_page_contains_livewire_component()
5 {
6 Livewire::withQueryParams(['foo' => 'bar'])
7 ->test(ShowFoo::class)
8 ->assertSet('foo', 'bar')
9 ->assertSee('bar');
10 }
11}

Testing Components With Passed Data

            
1class CreatePostTest extends TestCase
2{
3 /** @test */
4 function has_data_passed_correctly()
5 {
6 Livewire::test(ShowFoo::class, ['foo' => 'bar'])
7 ->assertSet('foo', 'bar')
8 ->assertSee('bar');
9 }
10}
            
1<livewire:show-foo foo="bar">

All Available Test Methods

        
1Livewire::actingAs($user);
2// Set the provided user as the session's logged in user for the test
3 
4Livewire::withQueryParams(['foo' => 'bar']);
5// Set the query param "foo" to "bar" for the Livewire component's `$queryString` property to pick up.
6 
7Livewire::test('foo', ['bar' => $bar]);
8// Test the "foo" component with "bar" set as a parameter.
9 
10->set('foo', 'bar');
11// Set the "foo" property (`public $foo`) to the value: "bar"
12 
13->toggle('foo');
14// Toggle the "foo" property (`public $foo`) between true and false
15 
16->call('foo');
17// Call the "foo" method
18 
19->call('foo', 'bar', 'baz');
20// Call the "foo" method, and pass the "bar" and "baz" parameters
21 
22->emit('foo');
23// Fire the "foo" event
24 
25->emit('foo', 'bar', 'baz');
26// Fire the "foo" event, and pass the "bar" and "baz" parameters
27 
28->assertSet('foo', 'bar');
29// Asserts that the "foo" property is set to the value "bar" (Includes computed properties)
30 
31->assertNotSet('foo', 'bar');
32// Asserts that the "foo" property is NOT set to the value "bar" (Includes computed properties)
33 
34->assertCount('foo', 1);
35// Asserts that the "foo" property (an array) has a count of 1 (Includes computed properties)
36 
37->assertPayloadSet('foo', 'bar');
38// Asserts that the "foo" property from the JavaScript payload that Livewire returns is set to the value "bar"
39 
40->assertPayloadNotSet('foo', 'bar');
41// Asserts that the "foo" property in the JavaScript payload that Livewire returns is NOT set to the value "bar"
42 
43->assertViewIs('foo');
44// Assert that the view "foo" is the currently rendered view
45 
46->assertViewHas('foo', 'bar');
47// Assert that the rendered view has a key of "foo" with a value of "bar"
48 
49->assertSee('foo');
50// Assert that the string "foo" exists in the currently rendered content of the component
51 
52->assertDontSee('foo');
53// Assert that the string "foo" DOES NOT exist in the currently rendered content of the component
54 
55->assertSeeHtml('<h1>foo</h1>');
56// Assert that the string "<h1>foo</h1>" exists in the currently rendered HTML of the component
57 
58->assertDontSeeHtml('<h1>foo</h1>');
59// Assert that the string "<h1>foo</h1>" DOES NOT exist in the currently rendered HTML of the component
60 
61->assertSeeInOrder(['foo', 'bar']);
62// Assert that the string "foo" exists before "bar" in the currently rendered content of the component
63 
64->assertSeeHtmlInOrder(['<h1>foo</h1>', '<h1>bar</h1>']);
65// Assert that the string "<h1>foo</h1>" exists before "<h1>bar</h1>" in the currently rendered content of the component
66 
67->assertEmitted('foo');
68// Assert that the "foo" event was emitted
69 
70->assertEmitted('foo', 'bar', 'baz');
71// Assert that the "foo" event was emitted with the "bar" and "baz" parameters
72 
73->assertNotEmitted('foo');
74// Assert that the "foo" event was NOT emitted
75 
76->assertHasErrors('foo');
77// Assert that the "foo" property has validation errors
78 
79->assertHasErrors(['foo', 'bar']);
80// Assert that the "foo" AND "bar" properties have validation errors
81 
82->assertHasErrors(['foo' => 'required']);
83// Assert that the "foo" property has a "required" validation rule error
84 
85->assertHasErrors(['foo' => ['required', 'min']]);
86// Assert that the "foo" property has a "required" AND "min" validation rule error
87 
88->assertHasNoErrors('foo');
89// Assert that the "foo" property has no validation errors
90 
91->assertHasNoErrors(['foo', 'bar']);
92// Assert that the "foo" AND "bar" properties have no validation errors
93 
94->assertNotFound();
95// Assert that an error within the component caused an error with the status code: 404
96 
97->assertRedirect('/some-path');
98// Assert that a redirect was triggered from the component
99 
100->assertNoRedirect();
101// Assert that no redirect was triggered from the component
102 
103->assertUnauthorized();
104// Assert that an error within the component caused an error with the status code: 401
105 
106->assertForbidden();
107// Assert that an error within the component caused an error with the status code: 403
108 
109->assertStatus(500);
110// Assert that an error within the component caused an error with the status code: 500
111 
112->assertDispatchedBrowserEvent('event', $data);
113// Assert that a browser event was dispatched from the component using (->dispatchBrowserEvent(...))
← Previous Topic

Inline Scripts

Next Topic →

Security