You're browsing the documentation for an old version of Livewire. Consider upgrading your project to Livewire 3.x.
Testing
- Introduction
- Testing Component Presence
- Testing With Query String Parameters
- Testing Components With Passed Data
- Generating Tests
- All Available Test Methods
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.
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">
Generating Tests
When creating a component, you can include the --test
flag, and a test file will be created for you as well.
1php artisan make:livewire ShowPosts --test
1class ShowPostsTest extends TestCase 2{ 3 /** @test */ 4 public function the_component_can_render() 5 { 6 $component = Livewire::test(ShowPosts::class); 7 8 $component->assertStatus(200); 9 }10}
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->assertEmittedTo('bar','foo'); 77// Assert that the "foo" event was emitted to "bar" component 78 79->assertHasErrors('foo'); 80// Assert that the "foo" property has validation errors 81 82->assertHasErrors(['foo', 'bar']); 83// Assert that the "foo" AND "bar" properties have validation errors 84 85->assertHasErrors(['foo' => 'required']); 86// Assert that the "foo" property has a "required" validation rule error 87 88->assertHasErrors(['foo' => ['required', 'min']]); 89// Assert that the "foo" property has a "required" AND "min" validation rule error 90 91->assertHasNoErrors('foo'); 92// Assert that the "foo" property has no validation errors 93 94->assertHasNoErrors(['foo', 'bar']); 95// Assert that the "foo" AND "bar" properties have no validation errors 96 97->assertNotFound(); 98// Assert that an error within the component caused an error with the status code: 404 99 100->assertRedirect('/some-path');101// Assert that a redirect was triggered from the component102 103->assertNoRedirect();104// Assert that no redirect was triggered from the component105 106->assertUnauthorized();107// Assert that an error within the component caused an error with the status code: 401108 109->assertForbidden();110// Assert that an error within the component caused an error with the status code: 403111 112->assertStatus(500);113// Assert that an error within the component caused an error with the status code: 500114 115->assertDispatchedBrowserEvent('event', $data);116// Assert that a browser event was dispatched from the component using (->dispatchBrowserEvent(...))117 118->assertNotDispatchedBrowserEvent('event');119// Assert that a browser event was not dispatched from the component using (->dispatchBrowserEvent(...))120 121->assertFileDownloaded($filename)122// Assert that a downloaded file was returned with a specific name
← Previous Topic
Inline Scripts
Next Topic →
Deployment