Skip to content

Commit 620dcea

Browse files
committed
Test the view with extra attributes
Claude is just insanely good
1 parent 17a33e0 commit 620dcea

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

packages/framework/tests/Unit/Views/NavigationLinkViewTest.php

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ protected function setUp(): void
2626
$this->mockPage();
2727
}
2828

29-
protected function testView(): TestView
29+
protected function testView(array $extraAttributes = []): TestView
3030
{
3131
return $this->view(view('hyde::components.navigation.navigation-link', [
32-
'item' => NavigationItem::create(new Route(new InMemoryPage('foo')), 'Foo'),
32+
'item' => NavigationItem::create(new Route(new InMemoryPage('foo')), 'Foo', null, $extraAttributes),
3333
'attributes' => new ComponentAttributeBag(),
3434
]));
3535
}
@@ -89,6 +89,56 @@ public function testComponentHasActiveClassWhenActive()
8989
->assertHasClass('navigation-link-active');
9090
}
9191

92+
93+
public function testComponentRendersExtraAttributes()
94+
{
95+
$this->testView(['data-test' => 'value'])
96+
->assertHasAttribute('data-test')
97+
->assertAttributeIs('data-test="value"');
98+
}
99+
100+
public function testComponentRendersMultipleExtraAttributes()
101+
{
102+
$this->testView(['data-test' => 'value', 'data-foo' => 'bar'])
103+
->assertHasAttribute('data-test')
104+
->assertAttributeIs('data-test="value"')
105+
->assertHasAttribute('data-foo')
106+
->assertAttributeIs('data-foo="bar"');
107+
}
108+
109+
public function testComponentRendersExtraAttributesWithExistingAttributes()
110+
{
111+
$this->mockCurrentPage('foo');
112+
113+
$view = $this->testView(['data-test' => 'value']);
114+
115+
$expected = <<<'HTML'
116+
<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>
117+
HTML;
118+
119+
$this->assertSame($expected, $view->getRendered());
120+
}
121+
122+
public function testComponentMergesClassAttributeCorrectly()
123+
{
124+
$this->testView(['class' => 'custom-class'])
125+
->assertHasClass('navigation-link')
126+
->assertHasClass('custom-class');
127+
}
128+
129+
public function testComponentOverridesDefaultAttributesWithExtraAttributes()
130+
{
131+
$this->testView(['href' => 'https://example.com'])
132+
->assertAttributeIs('href="https://example.com"');
133+
}
134+
135+
public function testComponentHandlesEmptyExtraAttributes()
136+
{
137+
$this->testView([])
138+
->assertHasElement('<a>')
139+
->assertTextIs('Foo');
140+
}
141+
92142
public function testComponentState()
93143
{
94144
$this->mockCurrentPage('foo');

0 commit comments

Comments
 (0)