Skip to content

Commit c6cceda

Browse files
authored
Add assertDoesntExist method (#42)
1 parent 38bbbd1 commit c6cceda

File tree

8 files changed

+129
-6
lines changed

8 files changed

+129
-6
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,15 @@ $this->get('/some-route')
193193
```
194194

195195

196-
For simple and quick checks, you can use `assertContainsElement`.
197-
This method allows you to verify that a specific element exists on the page you can optionally include an array of expected attributes.
196+
For simple and quick checks, you can use `assertContainsElement` or `assertDoesntExist`
197+
These methods allow you to verify that a specific element exists on the page.
198+
199+
`assertContainsElement` optionally allows an array of expected attributes
198200
```
199201
$this->get('/some-route')
200202
->assertContainsElement('#content')
201-
->assertContainsElement('div.banner', ['text' => 'Successfully deleted', 'data-status' => 'success']);
203+
->assertContainsElement('div.banner', ['text' => 'Successfully deleted', 'data-status' => 'success'])
204+
->assertDoesntExist('div.not-here');
202205
```
203206

204207
### Testing forms

ide-helper.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ public function assertContainsElement($selector, array $attributes = [])
5050
/** @var \Illuminate\Testing\TestResponse $instance */
5151
return $instance;
5252
}
53+
54+
public function assertDoesntExist($selector)
55+
{
56+
/** @var \Illuminate\Testing\TestResponse $instance */
57+
return $instance;
58+
}
5359
}
5460

5561
class TestView
@@ -101,6 +107,12 @@ public function assertContainsElement($selector, array $attributes = [])
101107
/** @var \Illuminate\Testing\TestView $instance */
102108
return $instance;
103109
}
110+
111+
public function assertDoesntExist($selector)
112+
{
113+
/** @var \Illuminate\Testing\TestView $instance */
114+
return $instance;
115+
}
104116
}
105117

106118
class TestComponent
@@ -152,5 +164,11 @@ public function assertContainsElement($selector, array $attributes = [])
152164
/** @var \Illuminate\Testing\TestComponent $instance */
153165
return $instance;
154166
}
167+
168+
public function assertDoesntExist($selector)
169+
{
170+
/** @var \Illuminate\Testing\TestComponent $instance */
171+
return $instance;
172+
}
155173
}
156174
}

src/TestComponentMacros.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,34 @@ public function assertContainsElement(): Closure
157157
};
158158
}
159159

160+
public function assertDoesntExist(): Closure
161+
{
162+
return function (string $selector): TestComponent {
163+
/** @var TestComponent $this */
164+
Assert::assertNotEmpty(
165+
(string) $this,
166+
'The component is empty!'
167+
);
168+
169+
try {
170+
if (! app()->has('dom-assertions.parser')) {
171+
app()->instance('dom-assertions.parser', DomParser::new((string) $this));
172+
}
173+
} catch (DOMException $exception) {
174+
Assert::fail($exception->getMessage());
175+
}
176+
177+
$element = app()->make('dom-assertions.parser')->query($selector);
178+
179+
Assert::assertNull(
180+
$element,
181+
sprintf('Expected no element with selector: %s, but one was found.', $selector)
182+
);
183+
184+
return $this;
185+
};
186+
}
187+
160188
public function assertForm(): Closure
161189
{
162190
return $this->assertFormExists();

src/TestResponseMacros.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,34 @@ public function assertContainsElement(): Closure
157157
};
158158
}
159159

160+
public function assertDoesntExist(): Closure
161+
{
162+
return function (string $selector): TestResponse {
163+
/** @var TestResponse $this */
164+
Assert::assertNotEmpty(
165+
(string) $this->getContent(),
166+
'The view is empty!'
167+
);
168+
169+
try {
170+
if (! app()->has('dom-assertions.parser')) {
171+
app()->instance('dom-assertions.parser', DomParser::new($this->getContent()));
172+
}
173+
} catch (DOMException $exception) {
174+
Assert::fail($exception->getMessage());
175+
}
176+
177+
$element = app()->make('dom-assertions.parser')->query($selector);
178+
179+
Assert::assertNull(
180+
$element,
181+
sprintf('Expected no element with selector: %s, but one was found.', $selector)
182+
);
183+
184+
return $this;
185+
};
186+
}
187+
160188
public function assertForm(): Closure
161189
{
162190
return $this->assertFormExists();

src/TestViewMacros.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,34 @@ public function assertContainsElement(): Closure
158158
};
159159
}
160160

161+
public function assertDoesntExist(): Closure
162+
{
163+
return function (string $selector): TestView {
164+
/** @var TestView $this */
165+
Assert::assertNotEmpty(
166+
(string) $this,
167+
'The view is empty!'
168+
);
169+
170+
try {
171+
if (! app()->has('dom-assertions.parser')) {
172+
app()->instance('dom-assertions.parser', DomParser::new((string) $this));
173+
}
174+
} catch (DOMException $exception) {
175+
Assert::fail($exception->getMessage());
176+
}
177+
178+
$element = app()->make('dom-assertions.parser')->query($selector);
179+
180+
Assert::assertNull(
181+
$element,
182+
sprintf('Expected no element with selector: %s, but one was found.', $selector)
183+
);
184+
185+
return $this;
186+
};
187+
}
188+
161189
public function assertForm(): Closure
162190
{
163191
return $this->assertFormExists();

tests/ComponentTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@
2626
});
2727
});
2828

29-
it('assertContainsElement works as expects', function () {
29+
it('assertDoesntExist works as expected', function () {
30+
$this->component(NestedComponent::class)
31+
->assertDoesntExist('span.fake')
32+
->assertDoesntExist('nav.fake');
33+
});
34+
35+
it('assertContainsElement works as expected', function () {
3036
$this->component(NestedComponent::class)
3137
->assertContainsElement('span.foo', ['text' => 'Foo', 'class' => 'bar foo'])
3238
->assertContainsElement('nav');

tests/DomTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@
1010
});
1111
});
1212

13-
it('assertContainsElement works as expects', function () {
13+
it('assertDoesntExist works as expected', function () {
14+
$this->get('nesting')
15+
->assertDoesntExist('span.fake')
16+
->assertDoesntExist('nav.fake');
17+
});
18+
19+
it('assertContainsElement works as expected', function () {
1420
$this->get('nesting')
1521
->assertContainsElement('span.foo', ['text' => 'Foo', 'class' => 'bar foo'])
1622
->assertContainsElement('nav');

tests/ViewTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
use PHPUnit\Framework\AssertionFailedError;
44
use Sinnbeck\DomAssertions\Asserts\AssertElement;
55

6-
it('assertContainsElement works as expects', function () {
6+
it('assertDoesntExist works as expected', function () {
7+
$this->get('nesting')
8+
->assertDoesntExist('span.fake')
9+
->assertDoesntExist('nav.fake');
10+
});
11+
12+
it('assertContainsElement works as expected', function () {
713
$this->view('nesting')
814
->assertContainsElement('span.foo', ['text' => 'Foo'])
915
->assertContainsElement('nav');

0 commit comments

Comments
 (0)