laravel / ideas

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

Asserting a view has a sub view #2490

Open amberlampsio opened 3 years ago

amberlampsio commented 3 years ago

We currently have,

->assertViewIs()

would be nice to have another method on testresponse or somewhere to be able to assert a "subview" has been rendered

such as

@include('layouts.app')

->assertViewHasSubview('layouts.app')

ahinkle commented 3 years ago

What would be the benefit from using assertViewHas() to check if the sub view has been injected?

$response->assertViewHas('layouts.app');
amberlampsio commented 3 years ago

What would be the benefit from using assertViewHas() to check if the sub view has been injected?

$response->assertViewHas('layouts.app');

Hey,

I'm not sure if it is not working for me or this function only check the bound data which is going to the view, but I just tried this approach and it does not assert that that view has been injected.

Are you able to replicate this?

I have a "working" version if this

    public function assertViewHasSubview($value)
    {
        $this->ensureResponseHasView();

        $views = Arr::wrap($value);

        foreach($views as $view) {
            PHPUnit::assertArrayHasKey(
                $view, 
                $this->original->getFactory()->getFinder()->getViews(),
                "View [{$view}] was not in the response"
            );
        }

        return $this;
    }

But it isn't perfect, because if you do something like View::exists() this method will assert that a view has been rendered.