Skip to content

Conversation

@jackbayliss
Copy link
Contributor

@jackbayliss jackbayliss commented Oct 4, 2025

This PR introduces a new assertion method: assertContainsElement.

It acts as a shorthand for assertElementExists, making it easier to chain assertions directly off a component/view or response keeping it fluent and great for quick, simple checks without sacrificing expressiveness.

So, all you do is the selector and the attributes.

$this->view('testing')
    ->assertContainsElement('nav');
    ->assertContainsElement('span.foo', ['text' => 'Foo', 'class' => 'bar foo'])

The idea being, for simple assertions such as alert banners etc, we can just do the below, as sometimes using assertElementExists feels long:

$this->view('testing')
    ->assertContainsElement('div.banner', ['text' => 'Your item was created successfully', 'class' => 'success']);

I will probably add an assertDoesntContainElement if this gets accepted

I've added it to the ide-helper, and improved the component dom to match the other nesting items.

@jackbayliss jackbayliss marked this pull request as ready for review October 4, 2025 15:04
@sinnbeck
Copy link
Owner

sinnbeck commented Oct 6, 2025

Thanks for all of these PR's. Its great to see some interest in expanding on the package.
Sorry that it takes me a while to review them, but I have a bit hung up in my personal life currently :)

Any reason why the ide-helper.php has been changed from TestResponse to TestComponent ?

@jackbayliss
Copy link
Contributor Author

jackbayliss commented Oct 6, 2025

@sinnbeck no problem!

The reason I changed the stan return is the TestComponent macro returns $this - which is actually TestComponent not TestResponse.

I think I copy pasted when I first added them 😬 - so fixing my own mistake. ( I guess there might be a few others wrong too, but will sort in a separate PR)

@sinnbeck
Copy link
Owner

sinnbeck commented Oct 6, 2025

@jackbayliss Ok I might need to test it out. I think I originally set it to TestResponse as that allows phpstorm to autocomplete after it. For instance

$this->get('/foo')
    ->assertContainsElement('nav')
    ->assertStatus(200); //this can be autocompleted

@jackbayliss
Copy link
Contributor Author

@sinnbeck Sure, we can revert those changes for now if you want - I'm not too precious about it :)

@sinnbeck
Copy link
Owner

sinnbeck commented Oct 6, 2025

@jackbayliss I think it might be a matter of whether we are asserting on a view or a response. I wonder if it is possible to make both work. Perhaps a union value is what is needed.

        public function assertElement($selector = 'body', $callback = null)
        {
            /** @var \Illuminate\Testing\TestComponent|\Illuminate\Testing\TestResponse $instance */
            return $instance;
        }

I am thinking it might be best to revert it in this PR, and we can look into a version that works for both in a seperate?

@sinnbeck
Copy link
Owner

sinnbeck commented Oct 6, 2025

Sorry. Just read the code again. I think yours is 100% correct. But a separate PR would be nice if you dont mind? :)

@sinnbeck sinnbeck merged commit ffcc045 into sinnbeck:main Oct 6, 2025
@jackbayliss jackbayliss deleted the assertContainsElement branch October 6, 2025 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants