Skip to content

Commit

Permalink
Test the view with extra attributes
Browse files Browse the repository at this point in the history
Claude is just insanely good
  • Loading branch information
caendesilva committed Jul 10, 2024
1 parent 17a33e0 commit ed672ce
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 2 deletions.
61 changes: 61 additions & 0 deletions packages/framework/tests/Feature/YamlConfigurationFeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,67 @@ public function testCanSetCustomNavigationItemsInTheYamlConfig()
$this->assertSame(300, $navigationItems[3]->getPriority());
}

public function testCanSetAttributesInNavigationItemsInTheYamlConfig()
{
$this->file('hyde.yml', <<<'YAML'
hyde:
navigation:
custom:
- destination: 'https://example.com'
label: 'Example'
priority: 100
attributes:
class: 'example'
- destination: 'about'
label: 'About Us'
priority: 200
attributes:
class: 'about'
id: 'about'
- destination: 'contact'
label: 'Contact'
priority: 300
attributes:
target: '_blank'
rel: 'noopener noreferrer'
foo: 'bar'
YAML);

$this->runBootstrappers();

$configItems = config('hyde.navigation.custom');

$this->assertSame([
[
'destination' => 'https://example.com',
'label' => 'Example',
'priority' => 100,
'attributes' => ['class' => 'example'],
], [
'destination' => 'about',
'label' => 'About Us',
'priority' => 200,
'attributes' => ['class' => 'about', 'id' => 'about'],
], [
'destination' => 'contact',
'label' => 'Contact',
'priority' => 300,
'attributes' => ['target' => '_blank', 'rel' => 'noopener noreferrer', 'foo' => 'bar'],
],
], $configItems);

/** @var NavigationItem[] $navigationItems */
$navigationItems = NavigationMenuGenerator::handle(MainNavigationMenu::class)->getItems()->all();

$this->assertCount(4, $navigationItems);
$this->assertContainsOnlyInstancesOf(NavigationItem::class, $navigationItems);

$this->assertSame([], $navigationItems[0]->getExtraAttributes());
$this->assertSame(['class' => 'example'], $navigationItems[1]->getExtraAttributes());
$this->assertSame(['class' => 'about', 'id' => 'about'], $navigationItems[2]->getExtraAttributes());
$this->assertSame(['target' => '_blank', 'rel' => 'noopener noreferrer', 'foo' => 'bar'], $navigationItems[3]->getExtraAttributes());
}

public function testOnlyNeedToAddDestinationToYamlConfiguredNavigationItems()
{
$this->file('hyde.yml', <<<'YAML'
Expand Down
53 changes: 51 additions & 2 deletions packages/framework/tests/Unit/Views/NavigationLinkViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ protected function setUp(): void
$this->mockPage();
}

protected function testView(): TestView
protected function testView(array $extraAttributes = []): TestView
{
return $this->view(view('hyde::components.navigation.navigation-link', [
'item' => NavigationItem::create(new Route(new InMemoryPage('foo')), 'Foo'),
'item' => NavigationItem::create(new Route(new InMemoryPage('foo')), 'Foo', null, $extraAttributes),
'attributes' => new ComponentAttributeBag(),
]));
}
Expand Down Expand Up @@ -89,6 +89,55 @@ public function testComponentHasActiveClassWhenActive()
->assertHasClass('navigation-link-active');
}

public function testComponentRendersExtraAttributes()
{
$this->testView(['data-test' => 'value'])
->assertHasAttribute('data-test')
->assertAttributeIs('data-test="value"');
}

public function testComponentRendersMultipleExtraAttributes()
{
$this->testView(['data-test' => 'value', 'data-foo' => 'bar'])
->assertHasAttribute('data-test')
->assertAttributeIs('data-test="value"')
->assertHasAttribute('data-foo')
->assertAttributeIs('data-foo="bar"');
}

public function testComponentRendersExtraAttributesWithExistingAttributes()
{
$this->mockCurrentPage('foo');

$view = $this->testView(['data-test' => 'value']);

$expected = <<<'HTML'
<a href="foo.html" aria-current="page" class="navigation-link block my-2 md:my-0 md:inline-block py-1 text-gray-700 hover:text-gray-900 dark:text-gray-100 navigation-link-active border-l-4 border-indigo-500 md:border-none font-medium -ml-6 pl-5 md:ml-0 md:pl-0 bg-gray-100 dark:bg-gray-800 md:bg-transparent dark:md:bg-transparent" data-test="value">Foo</a>
HTML;

$this->assertSame($expected, $view->getRendered());
}

public function testComponentMergesClassAttributeCorrectly()
{
$this->testView(['class' => 'custom-class'])
->assertHasClass('navigation-link')
->assertHasClass('custom-class');
}

public function testComponentOverridesDefaultAttributesWithExtraAttributes()
{
$this->testView(['href' => 'https://example.com'])
->assertAttributeIs('href="https://example.com"');
}

public function testComponentHandlesEmptyExtraAttributes()
{
$this->testView([])
->assertHasElement('<a>')
->assertTextIs('Foo');
}

public function testComponentState()
{
$this->mockCurrentPage('foo');
Expand Down

0 comments on commit ed672ce

Please sign in to comment.