Skip to content

Commit cd785d2

Browse files
committed
feat:expectation add for ToHaveValue
1 parent d3b3f34 commit cd785d2

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PHPUnit\Framework\ExpectationFailedException;
6+
7+
it('passes when element has the expected value', function (): void {
8+
$page = page()->goto('/test/form-inputs');
9+
expect($page->locator('input[name="email"]'))->toHaveValue('john.doe@pestphp.com');
10+
});
11+
12+
it('fails when element does not have the expected value', function (): void {
13+
$page = page()->goto('/test/form-inputs');
14+
expect($page->locator('input[name="email"]'))->toHaveValue('wrong@email.com');
15+
})->throws(ExpectationFailedException::class);
16+
17+
it('passes when element does not have the value and we expect it not to', function (): void {
18+
$page = page()->goto('/test/form-inputs');
19+
expect($page->locator('input[name="email"]'))->not->toHaveValue('wrong@email.com');
20+
});
21+
22+
it('fails when element has the value but we expect it not to', function (): void {
23+
$page = page()->goto('/test/form-inputs');
24+
expect($page->locator('input[name="email"]'))->not->toHaveValue('john.doe@pestphp.com');
25+
})->throws(ExpectationFailedException::class);

tests/Pest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,23 @@
116116
return $this;
117117
});
118118

119-
expect()->intercept('toBeEmpty', Locator::class,
119+
expect()->intercept(
120+
'toBeEmpty',
121+
Locator::class,
120122
function (): ExpectationMixin {
121123
expect($this->value->textContent())->toBe('');
122124

123125
return $this;
124126
}
125127
);
126128

129+
expect()->extend('toHaveValue', function (string $id): Expectation {
130+
if (! $this->value instanceof Locator) {
131+
throw new InvalidArgumentException('Expected value to be an Locator instance');
132+
}
133+
134+
expect($this->value->inputValue())->toBe($id);
135+
136+
return $this;
137+
});
127138
// todo: move this to Pest core end

0 commit comments

Comments
 (0)