diff --git a/README.md b/README.md index 9ce0a32..2c7dbd5 100644 --- a/README.md +++ b/README.md @@ -193,12 +193,15 @@ $this->get('/some-route') ``` -For simple and quick checks, you can use `assertContainsElement`. -This method allows you to verify that a specific element exists on the page you can optionally include an array of expected attributes. +For simple and quick checks, you can use `assertContainsElement` or `assertDoesntExist` +These methods allow you to verify that a specific element exists on the page. + +`assertContainsElement` optionally allows an array of expected attributes ``` $this->get('/some-route') ->assertContainsElement('#content') - ->assertContainsElement('div.banner', ['text' => 'Successfully deleted', 'data-status' => 'success']); + ->assertContainsElement('div.banner', ['text' => 'Successfully deleted', 'data-status' => 'success']) + ->assertDoesntExist('div.not-here'); ``` ### Testing forms diff --git a/ide-helper.php b/ide-helper.php index 7f7d1eb..4a6f24e 100644 --- a/ide-helper.php +++ b/ide-helper.php @@ -50,6 +50,12 @@ public function assertContainsElement($selector, array $attributes = []) /** @var \Illuminate\Testing\TestResponse $instance */ return $instance; } + + public function assertDoesntExist($selector) + { + /** @var \Illuminate\Testing\TestResponse $instance */ + return $instance; + } } class TestView @@ -89,6 +95,12 @@ public function assertContainsElement($selector, array $attributes = []) /** @var \Illuminate\Testing\TestView $instance */ return $instance; } + + public function assertDoesntExist($selector) + { + /** @var \Illuminate\Testing\TestView $instance */ + return $instance; + } } class TestComponent @@ -128,5 +140,11 @@ public function assertContainsElement($selector, array $attributes = []) /** @var \Illuminate\Testing\TestComponent $instance */ return $instance; } + + public function assertDoesntExist($selector) + { + /** @var \Illuminate\Testing\TestComponent $instance */ + return $instance; + } } } diff --git a/src/TestComponentMacros.php b/src/TestComponentMacros.php index 608875c..561043f 100644 --- a/src/TestComponentMacros.php +++ b/src/TestComponentMacros.php @@ -156,6 +156,34 @@ public function assertContainsElement(): Closure }; } + public function assertDoesntExist(): Closure + { + return function (string $selector): TestComponent { + /** @var TestComponent $this */ + Assert::assertNotEmpty( + (string) $this, + 'The component is empty!' + ); + + try { + if (! app()->has('dom-assertions.parser')) { + app()->instance('dom-assertions.parser', DomParser::new((string) $this)); + } + } catch (DOMException $exception) { + Assert::fail($exception->getMessage()); + } + + $element = app()->make('dom-assertions.parser')->query($selector); + + Assert::assertNull( + $element, + sprintf('Expected no element with selector: %s, but one was found.', $selector) + ); + + return $this; + }; + } + public function assertForm(): Closure { return $this->assertFormExists(); diff --git a/src/TestResponseMacros.php b/src/TestResponseMacros.php index 46f06ff..f66cead 100644 --- a/src/TestResponseMacros.php +++ b/src/TestResponseMacros.php @@ -157,6 +157,34 @@ public function assertContainsElement(): Closure }; } + public function assertDoesntExist(): Closure + { + return function (string $selector): TestResponse { + /** @var TestResponse $this */ + Assert::assertNotEmpty( + (string) $this->getContent(), + 'The view is empty!' + ); + + try { + if (! app()->has('dom-assertions.parser')) { + app()->instance('dom-assertions.parser', DomParser::new($this->getContent())); + } + } catch (DOMException $exception) { + Assert::fail($exception->getMessage()); + } + + $element = app()->make('dom-assertions.parser')->query($selector); + + Assert::assertNull( + $element, + sprintf('Expected no element with selector: %s, but one was found.', $selector) + ); + + return $this; + }; + } + public function assertForm(): Closure { return $this->assertFormExists(); diff --git a/src/TestViewMacros.php b/src/TestViewMacros.php index b77da2d..a7d3881 100644 --- a/src/TestViewMacros.php +++ b/src/TestViewMacros.php @@ -158,6 +158,34 @@ public function assertContainsElement(): Closure }; } + public function assertDoesntExist(): Closure + { + return function (string $selector): TestView { + /** @var TestView $this */ + Assert::assertNotEmpty( + (string) $this, + 'The view is empty!' + ); + + try { + if (! app()->has('dom-assertions.parser')) { + app()->instance('dom-assertions.parser', DomParser::new((string) $this)); + } + } catch (DOMException $exception) { + Assert::fail($exception->getMessage()); + } + + $element = app()->make('dom-assertions.parser')->query($selector); + + Assert::assertNull( + $element, + sprintf('Expected no element with selector: %s, but one was found.', $selector) + ); + + return $this; + }; + } + public function assertForm(): Closure { return $this->assertFormExists(); diff --git a/tests/ComponentTest.php b/tests/ComponentTest.php index af54800..44879dc 100644 --- a/tests/ComponentTest.php +++ b/tests/ComponentTest.php @@ -23,7 +23,13 @@ }); }); -it('assertContainsElement works as expects', function () { +it('assertDoesntExist works as expected', function () { + $this->component(NestedComponent::class) + ->assertDoesntExist('span.fake') + ->assertDoesntExist('nav.fake'); +}); + +it('assertContainsElement works as expected', function () { $this->component(NestedComponent::class) ->assertContainsElement('span.foo', ['text' => 'Foo', 'class' => 'bar foo']) ->assertContainsElement('nav'); diff --git a/tests/DomTest.php b/tests/DomTest.php index 3eceb3f..faaad21 100644 --- a/tests/DomTest.php +++ b/tests/DomTest.php @@ -10,7 +10,13 @@ }); }); -it('assertContainsElement works as expects', function () { +it('assertDoesntExist works as expected', function () { + $this->get('nesting') + ->assertDoesntExist('span.fake') + ->assertDoesntExist('nav.fake'); +}); + +it('assertContainsElement works as expected', function () { $this->get('nesting') ->assertContainsElement('span.foo', ['text' => 'Foo', 'class' => 'bar foo']) ->assertContainsElement('nav'); diff --git a/tests/ViewTest.php b/tests/ViewTest.php index eb1906a..1485d7b 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -3,7 +3,13 @@ use PHPUnit\Framework\AssertionFailedError; use Sinnbeck\DomAssertions\Asserts\AssertElement; -it('assertContainsElement works as expects', function () { +it('assertDoesntExist works as expected', function () { + $this->get('nesting') + ->assertDoesntExist('span.fake') + ->assertDoesntExist('nav.fake'); +}); + +it('assertContainsElement works as expected', function () { $this->view('nesting') ->assertContainsElement('span.foo', ['text' => 'Foo']) ->assertContainsElement('nav');