Skip to content

Commit 38bbbd1

Browse files
authored
Add missing ide-helper methods and component (#41)
1 parent 10ef4d1 commit 38bbbd1

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

ide-helper.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ public function assertFormExists($selector = 'form', $callback = null)
8484
return $instance;
8585
}
8686

87+
public function assertSelect($selector = 'select', $callback = null)
88+
{
89+
/** @var \Illuminate\Testing\TestView $instance */
90+
return $instance;
91+
}
92+
93+
public function assertSelectExists($selector = 'select', $callback = null)
94+
{
95+
/** @var \Illuminate\Testing\TestView $instance */
96+
return $instance;
97+
}
98+
8799
public function assertContainsElement($selector, array $attributes = [])
88100
{
89101
/** @var \Illuminate\Testing\TestView $instance */
@@ -123,6 +135,18 @@ public function assertFormExists($selector = 'form', $callback = null)
123135
return $instance;
124136
}
125137

138+
public function assertSelect($selector = 'select', $callback = null)
139+
{
140+
/** @var \Illuminate\Testing\TestComponent $instance */
141+
return $instance;
142+
}
143+
144+
public function assertSelectExists($selector = 'select', $callback = null)
145+
{
146+
/** @var \Illuminate\Testing\TestComponent $instance */
147+
return $instance;
148+
}
149+
126150
public function assertContainsElement($selector, array $attributes = [])
127151
{
128152
/** @var \Illuminate\Testing\TestComponent $instance */

src/TestComponentMacros.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPUnit\Framework\Assert;
1212
use Sinnbeck\DomAssertions\Asserts\AssertElement;
1313
use Sinnbeck\DomAssertions\Asserts\AssertForm;
14+
use Sinnbeck\DomAssertions\Asserts\AssertSelect;
1415
use Sinnbeck\DomAssertions\Support\DomParser;
1516

1617
/**
@@ -203,4 +204,52 @@ public function assertFormExists(): Closure
203204
return $this;
204205
};
205206
}
207+
208+
public function assertSelect(): Closure
209+
{
210+
return $this->assertSelectExists();
211+
}
212+
213+
public function assertSelectExists(): Closure
214+
{
215+
return function ($selector = 'select', $callback = null): TestComponent {
216+
/** @var TestComponent $this */
217+
Assert::assertNotEmpty(
218+
(string) $this,
219+
'The component is empty!'
220+
);
221+
222+
try {
223+
$parser = DomParser::new((string) $this);
224+
} catch (DOMException $exception) {
225+
Assert::fail($exception->getMessage());
226+
}
227+
228+
if ($selector instanceof Closure) {
229+
$callback = $selector;
230+
$selector = 'select';
231+
}
232+
233+
if (is_string($selector)) {
234+
$select = $parser->query($selector);
235+
} else {
236+
Assert::fail('Invalid selector!');
237+
}
238+
239+
Assert::assertNotNull(
240+
$select,
241+
sprintf('No select was found with selector "%s"', $selector)
242+
);
243+
Assert::assertEquals(
244+
'select',
245+
$select->nodeName,
246+
'Element is not of type select!');
247+
248+
if ($callback) {
249+
$callback(new AssertSelect((string) $this, $select));
250+
}
251+
252+
return $this;
253+
};
254+
}
206255
}

tests/ComponentTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
use PHPUnit\Framework\AssertionFailedError;
44
use PHPUnit\Framework\TestCase;
55
use Sinnbeck\DomAssertions\Asserts\AssertElement;
6+
use Sinnbeck\DomAssertions\Asserts\AssertForm;
7+
use Sinnbeck\DomAssertions\Asserts\AssertSelect;
68
use Tests\Views\Components\BrokenComponent;
79
use Tests\Views\Components\EmptyBodyComponent;
810
use Tests\Views\Components\EmptyComponent;
11+
use Tests\Views\Components\FormComponent;
912
use Tests\Views\Components\Html5Component;
1013
use Tests\Views\Components\LivewireComponent;
1114
use Tests\Views\Components\NestedComponent;
@@ -44,6 +47,29 @@
4447
->assertContainsElement('span.foo', ['non-existing-attribute' => 'non-existing']);
4548
})->throws(AssertionFailedError::class, 'Attribute [non-existing-attribute] not found in element [span.foo]');
4649

50+
it('assertFormExists works as expects', function () {
51+
$this->component(FormComponent::class)
52+
->assertFormExists('#form1', function (AssertForm $form) {
53+
$form->hasAction('store-comment');
54+
});
55+
});
56+
57+
it('assertSelectExists works as expects', function () {
58+
$this->component(FormComponent::class)
59+
->assertSelectExists('[name="things"]', function (AssertSelect $select) {
60+
$select->containsOptgroups(
61+
['label' => 'Animals'],
62+
['label' => 'Vegetables'],
63+
['label' => 'Minerals'],
64+
);
65+
$select->containsOptions(
66+
['value' => 'dog'],
67+
['value' => 'carrot'],
68+
['value' => 'calcium'],
69+
);
70+
});
71+
});
72+
4773
it('can handle an empty component', function () {
4874
$this->component(EmptyComponent::class)
4975
->assertElementExists();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Tests\Views\Components;
4+
5+
use Illuminate\View\Component;
6+
7+
class FormComponent extends Component
8+
{
9+
public function __construct() {}
10+
11+
public function render(): string
12+
{
13+
return <<<'HTML'
14+
<form id="form1" x-data="foo" action="store-comment" enctype="multipart/form-data">
15+
<label for="comment">Comment</label>
16+
<textarea name="comment" id="comment" required>
17+
foo
18+
</textarea>
19+
20+
<input type="checkbox" />
21+
22+
<select name="things">
23+
<optgroup label="Animals">
24+
<option value="dog">Dog</option>
25+
<option value="cat">Cat</option>
26+
</optgroup>
27+
<optgroup label="Vegetables" x-data="none">
28+
<option value="carrot">Carrot</option>
29+
<option value="onion">Onion</option>
30+
</optgroup>
31+
<optgroup label="Minerals">
32+
<option value="calcium">Calcium</option>
33+
<option value="zinc">Zinc</option>
34+
</optgroup>
35+
</select>
36+
</form>
37+
HTML;
38+
}
39+
}

0 commit comments

Comments
 (0)