generated from ibexa/bundle-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Added tests for Checkbox list twig component #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
252 changes: 252 additions & 0 deletions
252
tests/integration/Twig/Components/Checkbox/ListFieldTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,252 @@ | ||
| <?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\Checkbox; | ||
|
|
||
| use Ibexa\DesignSystemTwig\Twig\Components\Checkbox\ListField; | ||
| 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 ListFieldTest extends KernelTestCase | ||
| { | ||
| use InteractsWithTwigComponents; | ||
|
|
||
| public function testMount(): void | ||
| { | ||
| $component = $this->mountTwigComponent(ListField::class, $this->baseProps()); | ||
| self::assertInstanceOf( | ||
| ListField::class, | ||
| $component, | ||
| 'Component should mount as Checkbox\\ListField.' | ||
| ); | ||
| } | ||
|
|
||
| public function testDefaultRenderProducesWrapperAndRendersItems(): void | ||
| { | ||
| $crawler = $this->renderTwigComponent(ListField::class, $this->baseProps())->crawler(); | ||
|
|
||
| $wrapper = $this->getWrapper($crawler); | ||
| $classes = $this->getClassAttr($wrapper); | ||
| self::assertStringContainsString( | ||
| 'ids-field', | ||
| $classes, | ||
| 'Wrapper should include "ids-field".' | ||
| ); | ||
| self::assertStringContainsString( | ||
| 'ids-field--list', | ||
| $classes, | ||
| 'Wrapper should include "ids-field--list".' | ||
| ); | ||
| self::assertStringContainsString( | ||
| 'ids-choice-inputs-list', | ||
| $classes, | ||
| 'Wrapper should include "ids-choice-inputs-list".' | ||
| ); | ||
| self::assertStringContainsString( | ||
| 'ids-checkboxes-list-field', | ||
| $classes, | ||
| 'Wrapper should include "ids-checkboxes-list-field".' | ||
| ); | ||
|
|
||
| $items = $crawler->filter('.ids-choice-inputs-list__items .ids-checkbox-field'); | ||
| self::assertSame( | ||
| 2, | ||
| $items->count(), | ||
| 'Should render exactly two checkbox field items.' | ||
| ); | ||
|
|
||
| $firstInput = $this->getCheckboxInput($items->eq(0)); | ||
| $secondInput = $this->getCheckboxInput($items->eq(1)); | ||
|
|
||
| self::assertSame( | ||
| 'checkbox', | ||
| $firstInput->attr('type'), | ||
| 'First item should be a checkbox input.' | ||
| ); | ||
| self::assertSame( | ||
| 'group', | ||
| $firstInput->attr('name'), | ||
| 'First item "name" should be taken from the top-level group.' | ||
| ); | ||
| self::assertSame( | ||
| 'checkbox', | ||
| $secondInput->attr('type'), | ||
| 'Second item should be a checkbox input.' | ||
| ); | ||
| self::assertSame( | ||
| 'group', | ||
| $secondInput->attr('name'), | ||
| 'Second item "name" should be taken from the top-level group.' | ||
| ); | ||
|
|
||
| self::assertStringContainsString( | ||
| 'Pick A', | ||
| $this->getText($items->eq(0)), | ||
| 'First item should render its label.' | ||
| ); | ||
| self::assertStringContainsString( | ||
| 'Pick B', | ||
| $this->getText($items->eq(1)), | ||
| 'Second item should render its label.' | ||
| ); | ||
| } | ||
|
|
||
| public function testWrapperAttributesMergeClassAndData(): void | ||
| { | ||
| $crawler = $this->renderTwigComponent( | ||
| ListField::class, | ||
| $this->baseProps([ | ||
| 'attributes' => ['class' => 'extra-class', 'data-custom' => 'custom'], | ||
| ]) | ||
| )->crawler(); | ||
|
|
||
| $wrapper = $this->getWrapper($crawler); | ||
| self::assertStringContainsString( | ||
| 'extra-class', | ||
| $this->getClassAttr($wrapper), | ||
| 'Custom wrapper class should be merged.' | ||
| ); | ||
| self::assertSame( | ||
| 'custom', | ||
| $wrapper->attr('data-custom'), | ||
| 'Custom data attribute should render on the wrapper.' | ||
| ); | ||
| } | ||
|
|
||
| public function testDirectionVariantAddsExpectedClass(): void | ||
| { | ||
| $crawler = $this->renderTwigComponent( | ||
| ListField::class, | ||
| $this->baseProps(['direction' => 'horizontal']) | ||
| )->crawler(); | ||
|
|
||
| $wrapper = $this->getWrapper($crawler); | ||
|
|
||
| self::assertStringContainsString( | ||
| 'ids-choice-inputs-list--horizontal', | ||
| $this->getClassAttr($wrapper), | ||
| 'Direction "horizontal" should add the corresponding class.' | ||
| ); | ||
| } | ||
|
|
||
| public function testPerItemPropsAreForwardedToNestedField(): void | ||
| { | ||
| $props = $this->baseProps(); | ||
| $props['items'][0]['disabled'] = true; | ||
| $props['items'][1]['required'] = true; | ||
|
|
||
| $crawler = $this->renderTwigComponent( | ||
| ListField::class, | ||
| $props | ||
| )->crawler(); | ||
|
|
||
| $items = $crawler->filter('.ids-choice-inputs-list__items .ids-checkbox-field'); | ||
| $first = $this->getCheckboxInput($items->eq(0)); | ||
| $second = $this->getCheckboxInput($items->eq(1)); | ||
|
|
||
| self::assertNotNull( | ||
| $first->attr('disabled'), | ||
| 'Disabled=true on first item should render native "disabled".' | ||
| ); | ||
| self::assertNull( | ||
| $first->attr('required'), | ||
| 'First item should not be required.' | ||
| ); | ||
|
|
||
| self::assertNull( | ||
| $second->attr('disabled'), | ||
| 'Second item should not be disabled.' | ||
| ); | ||
| self::assertNotNull( | ||
| $second->attr('required'), | ||
| 'Required=true on second item should render native "required".' | ||
| ); | ||
| } | ||
|
|
||
| public function testInvalidItemsTypeCausesResolverErrorOnMount(): void | ||
| { | ||
| $this->expectException(InvalidOptionsException::class); | ||
|
|
||
| $this->mountTwigComponent(ListField::class, [ | ||
| 'name' => 'group', | ||
| 'items' => 'oops', | ||
| ]); | ||
| } | ||
|
|
||
| public function testMissingRequiredOptionsCauseResolverErrorOnMount(): void | ||
| { | ||
| $this->expectException(MissingOptionsException::class); | ||
|
|
||
| $this->mountTwigComponent(ListField::class, [ | ||
| 'items' => [ | ||
| ['id' => 'opt-a', 'label' => 'Pick A'], | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, mixed> $overrides | ||
| * | ||
| * @return array<string, mixed> | ||
| */ | ||
| private function baseProps(array $overrides = []): array | ||
| { | ||
| return array_replace([ | ||
| 'name' => 'group', | ||
| 'items' => [ | ||
| [ | ||
| 'id' => 'opt-a', | ||
| 'label' => 'Pick A', | ||
| 'value' => 'A', | ||
| ], | ||
| [ | ||
| 'id' => 'opt-b', | ||
| 'label' => 'Pick B', | ||
| 'value' => 'B', | ||
| ], | ||
| ], | ||
| ], $overrides); | ||
| } | ||
|
|
||
| private function getWrapper(Crawler $crawler): Crawler | ||
| { | ||
| $node = $crawler->filter('.ids-field')->first(); | ||
| self::assertGreaterThan( | ||
| 0, | ||
| $node->count(), | ||
| 'Wrapper ".ids-field" should be present.' | ||
| ); | ||
|
|
||
| return $node; | ||
| } | ||
|
|
||
| private function getCheckboxInput(Crawler $scope): Crawler | ||
| { | ||
| $node = $scope->filter('input[type="checkbox"]')->first(); | ||
| self::assertGreaterThan( | ||
| 0, | ||
| $node->count(), | ||
| 'Checkbox input should be present.' | ||
| ); | ||
|
|
||
| return $node; | ||
| } | ||
|
|
||
| private function getClassAttr(Crawler $node): string | ||
| { | ||
| return (string) $node->attr('class'); | ||
| } | ||
|
|
||
| private function getText(Crawler $node): string | ||
| { | ||
| return trim($node->text('')); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| <?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; | ||
|
|
||
| use Ibexa\Tests\Integration\DesignSystemTwig\Twig\Stub\DummyListFieldComponent; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; | ||
|
|
||
| final class ListFieldTraitTest extends TestCase | ||
| { | ||
| public function testResolveWithValidItemsAndHorizontalDirectionSucceeds(): void | ||
| { | ||
| $resolved = $this->getComponent()->resolve([ | ||
| 'items' => [ | ||
| ['id' => 'a', 'label' => 'Alpha', 'value' => 'A'], | ||
| ['id' => 'b', 'label' => 'Beta', 'value' => 'B'], | ||
| ], | ||
| 'direction' => 'horizontal', | ||
| ]); | ||
|
|
||
| self::assertArrayHasKey( | ||
| 'items', | ||
| $resolved, | ||
| 'Resolved options should contain "items".' | ||
| ); | ||
| self::assertCount( | ||
| 2, | ||
| $resolved['items'], | ||
| '"items" should contain two entries.' | ||
| ); | ||
| self::assertSame( | ||
| 'horizontal', | ||
| $resolved['direction'] ?? null, | ||
| '"direction" should resolve to HORIZONTAL.' | ||
| ); | ||
| } | ||
|
|
||
| public function testDefaultsWhenNoOptionsProvided(): void | ||
| { | ||
| $resolved = $this->getComponent()->resolve([]); | ||
|
|
||
| self::assertSame( | ||
| [], | ||
| $resolved['items'], | ||
| '"items" should default to an empty array.' | ||
| ); | ||
| self::assertSame( | ||
| 'vertical', | ||
| $resolved['direction'], | ||
| '"direction" should default to VERTICAL.' | ||
| ); | ||
| } | ||
|
|
||
| public function testInvalidItemsTypeCausesResolverError(): void | ||
| { | ||
| $this->expectException(InvalidOptionsException::class); | ||
|
|
||
| $this->getComponent()->resolve([ | ||
| 'items' => 'not-an-array', | ||
| ]); | ||
| } | ||
|
|
||
| public function testInvalidDirectionValueCausesResolverError(): void | ||
| { | ||
| $this->expectException(InvalidOptionsException::class); | ||
|
|
||
| $this->getComponent()->resolve([ | ||
| 'items' => [['id' => 'a', 'label' => 'Alpha', 'value' => 'A']], | ||
| 'direction' => 'diagonal', | ||
| ]); | ||
| } | ||
|
|
||
| private function getComponent(): DummyListFieldComponent | ||
| { | ||
| return new DummyListFieldComponent(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?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\Stub; | ||
|
|
||
| use Ibexa\DesignSystemTwig\Twig\Components\ListFieldTrait; | ||
| use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
|
||
| final class DummyListFieldComponent | ||
| { | ||
| use ListFieldTrait; | ||
|
|
||
| public string $name = 'group'; | ||
|
|
||
| public bool $required = false; | ||
|
|
||
| /** | ||
| * @param array<string, mixed> $options | ||
| * | ||
| * @return array<string, mixed> | ||
| */ | ||
| public function resolve(array $options): array | ||
| { | ||
| $resolver = new OptionsResolver(); | ||
| $this->validateListFieldProps($resolver); | ||
|
|
||
| return $resolver->resolve($options); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.