Skip to content

Commit e58cba1

Browse files
committed
chore: remove not empty, add additional tests, update readme
1 parent 880cd90 commit e58cba1

File tree

5 files changed

+32
-45
lines changed

5 files changed

+32
-45
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ $this->assertView('welcome')->in('title')->contains('Laravel');
6767
$this->assertView('welcome')->in('.content')->contains('Nova');
6868
```
6969

70+
### `empty`
71+
72+
Asserts that the view has no text content.
73+
74+
_Note: empty html nodes are not considered in this check._
75+
76+
```php
77+
$this->assertView('empty')->in('.empty-div')->empty();
78+
```
79+
7080
### `first`
7181

7282
Filters the view and returns only the first element matching the selector.

src/ViewAssertion.php

+8-16
Original file line numberDiff line numberDiff line change
@@ -104,26 +104,18 @@ public function contains(string $text): ViewAssertion
104104
}
105105

106106
/**
107-
* Asserts that the view, at given selector, is empty.
107+
* Asserts that the view, at given selector, has no content.
108108
*/
109109
public function empty(): ViewAssertion
110110
{
111-
Assert::assertEmpty(
112-
$this->crawler->html(),
113-
"Failed asserting that the text `{$this->crawler->html()}` is empty."
114-
);
115-
116-
return $this;
117-
}
111+
$content = "";
112+
foreach ($this->crawler->getIterator() as $node) {
113+
$content .= trim($node->textContent);
114+
}
118115

119-
/**
120-
* Asserts that the view, at given selector, is not empty.
121-
*/
122-
public function notEmpty(): ViewAssertion
123-
{
124-
Assert::assertNotEmpty(
125-
$this->crawler->html(),
126-
"Failed asserting that the text `{$this->crawler->html()}` is not empty."
116+
Assert::assertEmpty(
117+
$content,
118+
"Failed asserting that the text `{$content}` is empty."
127119
);
128120

129121
return $this;

tests/AssertEmpty.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ final class AssertEmpty extends TestCase
1717
public function testEmpty(): void
1818
{
1919
$this->assertView('empty')->in('.empty-div')->empty();
20+
$this->assertView('empty')->in('.empty-with-empty-nodes')->empty();
21+
$this->assertView('empty')->in('.empty-with-space')->empty();
2022
}
2123

2224
public function testNotEmpty(): void
2325
{
2426
$this->expectException(AssertionFailedError::class);
2527

26-
$this->assertView('button')->in('.btn')->empty();
28+
$this->assertView('empty')->in('.not-empty')->empty();
2729
}
2830
}

tests/AssertNotEmpty.php

-28
This file was deleted.

tests/fixtures/empty.html

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
<div>
22
<div class="empty-div"></div>
3+
<div class="empty-with-space">
4+
5+
</div>
6+
<div class="empty-with-empty-nodes">
7+
<span></span>
8+
</div>
9+
10+
<div class="not-empty-text">
11+
<span></span>
12+
Hello
13+
</div>
314
</div>

0 commit comments

Comments
 (0)