-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInputTest.php
More file actions
251 lines (213 loc) · 8.08 KB
/
InputTest.php
File metadata and controls
251 lines (213 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\Tests\Integration\DesignSystemTwig\Twig\Components\DropdownSingle;
use Ibexa\DesignSystemTwig\Twig\Components\DropdownSingle\Input;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
use Symfony\UX\TwigComponent\Test\InteractsWithTwigComponents;
final class InputTest extends KernelTestCase
{
use InteractsWithTwigComponents;
public function testMount(): void
{
$component = $this->mountTwigComponent(Input::class, $this->baseProps(['value' => 'foo']));
self::assertInstanceOf(
Input::class,
$component,
'Component should mount as DropdownSingle\\Input.'
);
}
public function testEmptyStateShowsPlaceholderAndHidesSelectedItems(): void
{
$crawler = $this->renderTwigComponent(
Input::class,
$this->baseProps()
)->crawler();
$select = $this->getSelect($crawler);
$selectedOption = $select->filter('option[selected]')->first();
self::assertSame(
'a',
$selectedOption->attr('value'),
'Without a value, the first option should be selected by default.'
);
self::assertSame(
'Alpha',
trim($selectedOption->text('')),
'The first option label should be shown as selected by default.'
);
$placeholder = $crawler->filter('.ids-dropdown__placeholder')->first();
$selectedBox = $crawler->filter('.ids-dropdown__selection-info-items')->first();
self::assertNotNull(
$placeholder->attr('hidden'),
'Placeholder should be hidden when no explicit value is selected.'
);
self::assertNull(
$selectedBox->attr('hidden'),
'Selection box should be visible when no explicit value is selected.'
);
self::assertStringContainsString(
'Alpha',
trim($selectedBox->text('')),
'Selection box should show the first item label by default.'
);
}
public function testSelectedValueHidesPlaceholderAndShowsSelectedLabel(): void
{
$crawler = $this->renderTwigComponent(Input::class, $this->baseProps(['value' => 'b']))->crawler();
$select = $this->getSelect($crawler);
$selectedOption = $select->filter('option[selected]')->first();
self::assertSame(
'b',
$selectedOption->attr('value'),
'Selected <option> should match provided value.'
);
self::assertSame(
'Beta',
trim($selectedOption->text('')),
'Selected <option> text should be the item label.'
);
$placeholder = $crawler->filter('.ids-dropdown__placeholder')->first();
$selectedBox = $crawler->filter('.ids-dropdown__selection-info-items')->first();
self::assertNotNull(
$placeholder->attr('hidden'),
'Placeholder should be hidden when a value is selected.'
);
self::assertNull(
$selectedBox->attr('hidden'),
'Selection box should be visible when a value is selected.'
);
self::assertStringContainsString(
'Beta',
trim($selectedBox->text('')),
'Selection box should render selected label.'
);
}
public function testOptionsAreRenderedAndOneIsMarkedSelected(): void
{
$crawler = $this->renderTwigComponent(Input::class, $this->baseProps(['value' => 'a']))->crawler();
$select = $this->getSelect($crawler);
$options = $select->filter('option');
self::assertSame(
3,
$options->count(),
'Should render three <option> elements.'
);
self::assertSame(
'a',
$select->filter('option[selected]')->attr('value'),
'The <option> with selected attribute should match provided value.'
);
$labels = $options->each(static fn (Crawler $o): string => trim($o->text('')));
self::assertSame(
['Alpha', 'Beta', 'Gamma'],
$labels,
'Options should render provided labels in order.'
);
}
public function testDisabledAndErrorAddClassesAndSelectDisabled(): void
{
$crawler = $this->renderTwigComponent(
Input::class,
$this->baseProps(['disabled' => true, 'error' => true])
)->crawler();
$wrapper = $this->getWrapper($crawler);
$widget = $this->getWidget($crawler);
$select = $this->getSelect($crawler);
self::assertStringContainsString(
'ids-dropdown--disabled',
$this->getClassAttr($wrapper),
'Wrapper should include disabled modifier.'
);
self::assertStringContainsString(
'ids-dropdown--error',
$this->getClassAttr($wrapper),
'Wrapper should include error modifier.'
);
self::assertStringContainsString(
'ids-input--disabled',
$this->getClassAttr($widget),
'Widget should include disabled modifier.'
);
self::assertStringContainsString(
'ids-input--error',
$this->getClassAttr($widget),
'Widget should include error modifier.'
);
self::assertNotNull(
$select->attr('disabled'),
'Native "disabled" attribute should be present on <select> when disabled=true.'
);
}
public function testWrapperClassMergesFromAttributes(): void
{
$crawler = $this->renderTwigComponent(
Input::class,
$this->baseProps([
'attributes' => ['class' => 'extra-class'],
])
)->crawler();
$wrapper = $this->getWrapper($crawler);
self::assertStringContainsString(
'extra-class',
$this->getClassAttr($wrapper),
'Custom class should be merged into wrapper classes.'
);
}
public function testInvalidItemsTypeCausesResolverErrorOnMount(): void
{
$this->expectException(InvalidOptionsException::class);
$this->mountTwigComponent(Input::class, [
'name' => 'group',
'items' => 'not-an-array',
]);
}
public function testMissingRequiredOptionsCauseResolverErrorOnMount(): void
{
$this->expectException(MissingOptionsException::class);
$this->mountTwigComponent(Input::class);
}
/**
* @param array<string, mixed> $overrides
*
* @return array<string, mixed>
*/
private function baseProps(array $overrides = []): array
{
return array_replace([
'name' => 'group',
'items' => [
['id' => 'a', 'label' => 'Alpha'],
['id' => 'b', 'label' => 'Beta'],
['id' => 'c', 'label' => 'Gamma'],
],
], $overrides);
}
private function getWrapper(Crawler $crawler): Crawler
{
$node = $crawler->filter('.ids-dropdown')->first();
self::assertGreaterThan(0, $node->count(), 'Wrapper ".ids-dropdown" should be present.');
return $node;
}
private function getWidget(Crawler $crawler): Crawler
{
$node = $crawler->filter('.ids-dropdown__widget')->first();
self::assertGreaterThan(0, $node->count(), 'Widget ".ids-dropdown__widget" should be present.');
return $node;
}
private function getSelect(Crawler $crawler): Crawler
{
$node = $crawler->filter('.ids-dropdown__source > select')->first();
self::assertGreaterThan(0, $node->count(), 'Source <select> should be present.');
return $node;
}
private function getClassAttr(Crawler $node): string
{
return (string) $node->attr('class');
}
}