How to test in Laravel view component @section rendered content?

413 Views Asked by At

In my Laravel 9.x project I have this code in a component's blade template:

@section('scripts')
    <script type="text/javascript">
        // ...
    </script>
@endsection

@section('content')
    @foreach($popups as $popup)
        <div id="{{ $popup->modal_id }}" class="popup">
            ...
        </div>
    @endforeach
@endsection

In my test:

public function testComponentsYieldedSectionContents
{
    $component = new PopupComponent()

    // ...what's coming here?
}

UPDATE:

I tried to create a fake blade template, like this:

$template = <<<'BLADE'
    <div id="content">
    @yield('content')
    </div>

    <div id="scripts">
    @yield('scripts')
    </div>

    <x-popup />
    BLADE;

$result = Blade::render($template, ['id' => $popup->id]);

In this case the result is this:

<div id="content">
</div>

<div id="scripts">
</div>

So the component's @section is not arrived into the @yield.

How can I test what is the result in @section('content') and @section('scripts')?

1

There are 1 best solutions below

1
On

Laravel ships with an extensive testing framework. You might want to check out the method assertSeeText($string) which verifies whether a certain string is contained in the HTTP response.

See the documentation here. There is also a list of all available assertions if the one mentioned does not suit your exact purpose.