netdjw's avatar
Level 15

How to test @section result in View Component

I have this code in my awesome.blade.php what belongs to AwesomeComponent.php:

@section('scripts')
    <script type="text/javascript">
        var myCarousel = document.querySelector('#carousel-{{ $banner->id }}')
        var carousel = new bootstrap.Carousel(myCarousel)
    </script>
@endsection

// here is some other HTML codes

How should I test the result of @section('script') ?

0 likes
4 replies
Sinnbeck's avatar

Do you mean with phpunit? What exactly are you trying to test? That there is a script tag ?

netdjw's avatar
Level 15

@Sinnbeck yes, with phpunit.

I have no idea what should I test. I want to proof the code is there in the component and it's emitting to the "scripts" section. Sorry, emitting is maybe not the right term, but you've got the point.

Sinnbeck's avatar

@netdjw phpunit won't run Javascript so there is no way it will know if the code is being run. Sounds to me like an integration/e2e test for which I would use cypress or laravel dusk (I prefer cypress)

netdjw's avatar
Level 15

@Sinnbeck I don't care about JavaScript running, I just want to test this section is not deleted or modified in the file by other developers. (I use other tests to be sure JS code is good.)

Please or to participate in this conversation.