okorpheus's avatar

Nested statements in test using laravel-dom-assertion

Thanks to some help here, I've started using the package laravel-dom-assertion, but I must not be understanding how assertions can be nested. I have this test:

it('each audition has a checkbox with its name', function () {
    // Arrange
    $events = Event::factory()->count(2)->create();
    foreach ($events as $event) {
        Audition::factory()->count(5)->create(['event_id' => $event->id]);
    }
    actAsAdmin();
    // Act
    $response = $this->get(route('admin.draw.index'));
    // Assert
    foreach ($events as $event) {
        $response->assertElementExists('#event-section-'.$event->id, function (AssertElement $element) use ($event) {
            foreach ($event->auditions as $audition) {
                $element->contains('#auditiongroup-'.$audition->id, function (AssertElement $element) use ($audition) {
                    $element->contains('#auditionCheckbox-'.$audition->id);
                    $element->containsText($audition->name);
                });
            }
        });
    }
});

testing a route that outputs this blade template

<x-layout.app>
    @foreach($events as $event)
        <x-card.card class="mb-5 mx-auto max-w-3xl" id="event-section-{{$event->id}}">
            <x-card.heading>{{ $event->name }}</x-card.heading>
            <div class="grid gap-y-2 md:grid-cols-2 lg:grid-cols-3 mx-5 my-3">
                @foreach($event->auditions as $audition)
                    <div id="auditiongroup-{{$audition->id}}">
                        {{$audition->name}}
                    </div>
                @endforeach
            </div>
        </x-card.card>
    @endforeach
</x-layout.app>

I'm expecting it to fail as the checkbox isn't added yet, but it passes. The following test fails, but doesn't require the checkbox to be in the div with its name.

it('each audition has a checkbox with its name', function () {
    // Arrange
    $events = Event::factory()->count(2)->create();
    foreach ($events as $event) {
        Audition::factory()->count(5)->create(['event_id' => $event->id]);
    }
    actAsAdmin();
    // Act
    $response = $this->get(route('admin.draw.index'));
    // Assert
    foreach ($events as $event) {
        $response->assertElementExists('#event-section-'.$event->id, function (AssertElement $element) use ($event) {
            foreach ($event->auditions as $audition) {
                $element->contains('#auditiongroup-'.$audition->id, function (AssertElement $element) use ($audition) {
                    
                    $element->containsText($audition->name);
                });
                $element->contains('#auditionCheckbox-'.$audition->id);
            }
        });
    }
});

Is there something about nesting the assertions I'm missing?

0 likes
1 reply

Please or to participate in this conversation.