laravel / ideas

Issues board used for Laravel internals discussions.
938 stars 31 forks source link

Assert that a component has rendered on a page #2650

Open ahinkle opened 3 years ago

ahinkle commented 3 years ago

I'm finding a need to check if a component has processed on the page.

I can always test if the HTML/text shows within the page but sometimes I want to test if a component was rendered within the page and assert that the variables are defined correctly from the passed variables.

If anyone would find this useful I would be happy to work on it and open a pull request.

Important to note: We already have testing to evaluate and render a single Blade component via $this->component(). This is outside the scope of the single component render and coming from the application get() request.

    // use App\View\Components\SomeComponent;

    public function it_does_something_really_cool_and_shows_the_component()
    {
        $this->get('/my-page')
            ->assertOk()
            ->assertSeeComponent(SomeComponent::class);

            // Assert that a component was not rendered on the page:
            ->assertMissingComponent(SomeComponent::class);

            // Access the component properties which must evaluate to true.
            ->assertSeeComponent(SomeComponent::class, function ($component) {
                return $component->title === 'My Page Title';
            });
    }