Skip to content

Commit

Permalink
Move new tests into common StimulusAttributesTest class
Browse files Browse the repository at this point in the history
  • Loading branch information
7-zete-7 committed Sep 28, 2024
1 parent d239224 commit f7b6033
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 115 deletions.
115 changes: 0 additions & 115 deletions src/StimulusBundle/tests/Dto/StimulusActionAttributeTest.php

This file was deleted.

87 changes: 87 additions & 0 deletions src/StimulusBundle/tests/Dto/StimulusAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,91 @@ public function testAddAttribute()
$this->assertSame('foo="bar baz"', (string) $this->stimulusAttributes);
$this->assertSame(['foo' => 'bar baz'], $this->stimulusAttributes->toArray());
}

/**
* @dataProvider provideAddComplexActionData
*/
public function testAddComplexAction(string $controllerName, string $actionName, ?string $eventName, string $expectedAction): void
{
$this->stimulusAttributes->addAction($controllerName, $actionName, $eventName);
$attributesHtml = (string) $this->stimulusAttributes;
self::assertSame(\sprintf('data-action="%s"', $expectedAction), $attributesHtml);
}

/**
* @return iterable<array{
* controllerName: string,
* actionName: string,
* eventName: ?string,
* expectedAction: string,
* }>
*/
public static function provideAddComplexActionData(): iterable
{
// basic datasets
yield 'foo#bar' => [
'controllerName' => 'foo',
'actionName' => 'bar',
'eventName' => null,
'expectedAction' => 'foo#bar',
];
yield 'baz->foo#bar' => [
'controllerName' => 'foo',
'actionName' => 'bar',
'eventName' => 'baz',
'expectedAction' => 'baz->foo#bar',
];

// datasets from https://github.com/hotwired/stimulus
yield 'keydown.esc@document->a#log' => [
'controllerName' => 'a',
'actionName' => 'log',
'eventName' => 'keydown.esc@document',
'expectedAction' => 'keydown.esc@document->a#log',
];
yield 'keydown.enter->a#log' => [
'controllerName' => 'a',
'actionName' => 'log',
'eventName' => 'keydown.enter',
'expectedAction' => 'keydown.enter->a#log',
];
yield 'keydown.shift+a->a#log' => [
'controllerName' => 'a',
'actionName' => 'log',
'eventName' => 'keydown.shift+a',
'expectedAction' => 'keydown.shift+a->a#log',
];
yield 'keydown@window->c#log' => [
'controllerName' => 'c',
'actionName' => 'log',
'eventName' => 'keydown@window',
'expectedAction' => 'keydown@window->c#log',
];
yield 'click->c#log:once' => [
'controllerName' => 'c',
'actionName' => 'log:once',
'eventName' => 'click',
'expectedAction' => 'click->c#log:once',
];

// extended datasets
yield 'vue:mount->foo#bar:passive' => [
'controllerName' => 'foo',
'actionName' => 'bar:passive',
'eventName' => 'vue:mount',
'expectedAction' => 'vue:mount->foo#bar:passive',
];
yield 'foo--controller-1:baz->bar--controller-2#log' => [
'controllerName' => '@bar/controller_2',
'actionName' => 'log',
'eventName' => '@foo/controller_1:baz',
'expectedAction' => 'foo--controller-1:baz->bar--controller-2#log',
];
yield 'foo--controller-1:baz@document->bar--controller-2#log:capture' => [
'controllerName' => '@bar/controller_2',
'actionName' => 'log:capture',
'eventName' => '@foo/controller_1:baz@document',
'expectedAction' => 'foo--controller-1:baz@document->bar--controller-2#log:capture',
];
}
}

0 comments on commit f7b6033

Please sign in to comment.