Here's an example of a Pest test to test the correct creation of a post using Livewire and Pest:
use App\Http\Livewire\CreatePost;
use App\Models\Post;
use Livewire\Livewire;
use function Pest\Livewire\livewire;
it('can create a post', function () {
// Arrange
$postData = [ 'title' => 'Test Post', 'content' => 'This is a test post', ];
// Act
livewire(CreatePost::class) ->set('title', $postData['title']) ->set('content', $postData['content']) ->call('createPost'); // Assert $this->assertTrue(Post::where($postData)->exists());
});
In this test, we're using the livewire
function provided by Pest to simulate a Livewire component interaction. We first define the data that we want to submit for creating a post. Then, we use the livewire
function to instantiate the CreatePost
Livewire component. We set the title
and content
properties of the component using the set
method, and then we call the createPost
method on the component using the call
method. Finally, we assert that a post with the given data exists in the database using the assertTrue
assertion. Make sure to adjust the namespaces and class names according to your application's structure. Remember to install Pest and Livewire dependencies before running the test. You can find more information about Pest and Livewire in their official documentation: - Pest: https://pestphp.com/ - Livewire: https://laravel-livewire.com/