Skip to content

Commit 2e8b093

Browse files
authored
Merge pull request #11 from balping/main
Match string within text content
2 parents b3bc3ed + dc9270e commit 2e8b093

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,10 @@ Livewire::test(UserForm::class)
371371
| `->isDiv()` | Magic method. Same as `->is('div')` |
372372
| `->contains($selector, $attributes, $count)` | Checks for any children of the current element |
373373
| `->containsDiv, ['class' => 'foo'], 3)` | Magic method. Same as `->contains('div', ['class' => 'foo'], 3)` |
374+
| `->containsText($needle, $ignoreCase)` | Checks if the element's text content contains a specified string |
374375
| `->doesntContain($selector, $attributes)` | Ensures that there are no matching children |
375376
| `->doesntContainDiv, ['class' => 'foo'])` | Magic method. Same as `->doesntContain('div', ['class' => 'foo'])` |
377+
| `->doesntContainText($needle, $ignoreCase)` | Checks if the element's text content doesn't contain a specified string |
376378
| `->find($selector, $callback)` | Find a specific child element and get a new AssertElement. Returns the first match. |
377379
| `->findDiv(fn (AssertElement $element) => {})` | Magic method. Same as `->find('div', fn (AssertElement $element) => {})` |
378380

src/Asserts/Traits/UsesElementAsserts.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,42 @@ public function doesntContain(string $elementName, array $attributes = []): self
147147
return $this;
148148
}
149149

150+
public function containsText(string $needle, bool $ignoreCase = false): self
151+
{
152+
$text = $this->getAttribute('text');
153+
154+
$assertFunction = $ignoreCase ?
155+
'assertStringContainsStringIgnoringCase' :
156+
'assertStringContainsString';
157+
158+
call_user_func(
159+
[PHPUnit::class, $assertFunction],
160+
$needle,
161+
$text,
162+
sprintf('Could not find text content "%s" containing %s', $text, $needle)
163+
);
164+
165+
return $this;
166+
}
167+
168+
public function doesntContainText(string $needle, bool $ignoreCase = false): self
169+
{
170+
$text = $this->getAttribute('text');
171+
172+
$assertFunction = $ignoreCase ?
173+
'assertStringNotContainsStringIgnoringCase' :
174+
'assertStringNotContainsString';
175+
176+
call_user_func(
177+
[PHPUnit::class, $assertFunction],
178+
$needle,
179+
$text,
180+
sprintf('Found text content "%s" containing %s', $text, $needle)
181+
);
182+
183+
return $this;
184+
}
185+
150186
public function is(string $type): self
151187
{
152188
PHPUnit::assertEquals(

tests/DomTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,27 @@
182182
});
183183
});
184184

185+
it('can match text content containing a string', function () {
186+
$this->get('nesting')
187+
->assertElementExists('p.foo.bar', function (AssertElement $element) {
188+
$element->containsText('Bar');
189+
});
190+
});
191+
192+
it('can match text content containing a string ignoring case', function () {
193+
$this->get('nesting')
194+
->assertElementExists('p.foo.bar', function (AssertElement $element) {
195+
$element->containsText('bar', true);
196+
});
197+
});
198+
199+
it('can match text content not containing a string', function () {
200+
$this->get('nesting')
201+
->assertElementExists('p.foo.bar', function (AssertElement $element) {
202+
$element->doesntContainText('bar');
203+
});
204+
});
205+
185206
it('can match a class no matter the order', function () {
186207
$this->get('nesting')
187208
->assertElementExists(function (AssertElement $element) {

0 commit comments

Comments
 (0)