madprabh's avatar

PEST test to verify component is rendered on UI

Hey Folks,

Is there a way for me to write a PEST test to confirm if the data from the server is actually being shown in the UI? Basically I am dynamically displaying vue component dynamically based on response from db. How do I make sure that the component is getting displayed on the UI?

Best

0 likes
2 replies
LaryAI's avatar
Level 58

To verify that a Vue component is rendered on the UI using PEST, you can use Laravel's built-in testing capabilities along with the Laravel Dusk package for browser automation and testing. Here’s a step-by-step solution:

  1. Install Laravel Dusk: If you haven't already, you need to install Laravel Dusk.

    composer require --dev laravel/dusk
    php artisan dusk:install
    
  2. Create a Dusk Test: Create a new Dusk test using the Artisan command.

    php artisan dusk:make ComponentRenderTest
    
  3. Write the Test: Open the newly created test file tests/Browser/ComponentRenderTest.php and write your test to check if the Vue component is rendered based on the server response.

    <?php
    
    namespace Tests\Browser;
    
    use Laravel\Dusk\Browser;
    use Tests\DuskTestCase;
    
    class ComponentRenderTest extends DuskTestCase
    {
        /**
         * A basic browser test example.
         *
         * @return void
         */
        public function testComponentIsRendered()
        {
            $this->browse(function (Browser $browser) {
                $browser->visit('/your-page-url')
                        ->waitFor('.your-component-selector') // Wait for the component to be rendered
                        ->assertVisible('.your-component-selector'); // Assert that the component is visible
            });
        }
    }
    

    In this example:

    • Replace /your-page-url with the URL of the page where the component should be rendered.
    • Replace .your-component-selector with the CSS selector of the Vue component you want to check.
  4. Run the Test: Finally, run your Dusk test to ensure everything is working as expected.

    php artisan dusk
    

This approach uses Laravel Dusk to simulate a browser and interact with your application just like a real user would. It waits for the Vue component to be rendered and then asserts that it is visible on the page.

If you need to mock the server response, you might need to set up a mock server or use a tool like moxios in your Vue component tests to simulate the server response.

This should help you confirm that the data from the server is being shown in the UI by dynamically displaying the Vue component based on the response from the database.

Tray2's avatar

Like Lary says, you need to use a client test framework like Dusk or Cypress to test anything that is rendered with JavaScript, while Pest is a php testing framework which tests your code server side, you can test the DOM generated with php, and even Livewire with Pest, but not vue components.

https://packagist.org/packages/sinnbeck/laravel-dom-assertions

1 like

Please or to participate in this conversation.