Skip to content
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

[2.x] Clean up navigation view markup #1606

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
894a865
Include navigation link as Blade component instead of include
caendesilva Mar 13, 2024
52f2d0c
Fix component name
caendesilva Mar 13, 2024
6ba9eaf
Add CSS class hooks for the navigation link states
caendesilva Mar 13, 2024
c61eef9
Include dropdown navigation item as Blade component
caendesilva Mar 13, 2024
759c63a
Refactor component to use attribute merging
caendesilva Mar 13, 2024
5710b1a
Test inverse attribute state
caendesilva Mar 13, 2024
5ffeac4
Remove unnecessary parentheses
caendesilva Mar 13, 2024
ac2e0ba
Merge branch 'improved-navigation-internals' into clean-up-navigation…
caendesilva Mar 13, 2024
6a47409
Add testing helper to make NavItem for page
caendesilva Mar 14, 2024
2cd3da2
Add improved assertion output
caendesilva Mar 14, 2024
d5f33f6
Test component resolves relative links for routes
caendesilva Mar 14, 2024
a4c0f9e
Simplify test data handling
caendesilva Mar 14, 2024
c042cf7
Add more attribute assertion helpers
caendesilva Mar 14, 2024
b7fb433
Refactor test to use fluent assertions
caendesilva Mar 14, 2024
7b1868e
Add testing helper to dump and die with HTML state
caendesilva Mar 14, 2024
c3d756e
Merge branch 'improved-navigation-internals' into clean-up-navigation…
caendesilva Mar 14, 2024
282d1fe
Refactor test to use fluent assertions
caendesilva Mar 14, 2024
e15daca
Merge branch 'improved-navigation-internals' into clean-up-navigation…
caendesilva Mar 14, 2024
add2390
Use helper method fluently
caendesilva Mar 14, 2024
68d46ff
Update testing helper to automatically parse attributes
caendesilva Mar 15, 2024
cd1e567
Use shorthand attribute testing syntax
caendesilva Mar 15, 2024
b4bb5f4
Merge branch 'improved-navigation-internals' into clean-up-navigation…
caendesilva Mar 15, 2024
2a1c9cc
Add assertion
caendesilva Mar 15, 2024
322862b
Remove duplicated test case
caendesilva Mar 15, 2024
cc071eb
Refactor tests to use fluent assertions
caendesilva Mar 15, 2024
c66c5c8
Remove unused testing helper
caendesilva Mar 15, 2024
b451eff
Remove unused parameter from testing helper method
caendesilva Mar 15, 2024
fbb5bfd
Add testing helpers to assert CSS class states
caendesilva Mar 15, 2024
d5411eb
Use new class state helpers
caendesilva Mar 15, 2024
fd1c68a
Clean up and normalize test semantics
caendesilva Mar 15, 2024
b0d4331
Add testing helper to assert element state
caendesilva Mar 15, 2024
8449237
Test component renders
caendesilva Mar 15, 2024
7bce5eb
Merge branch 'improved-navigation-internals' into clean-up-navigation…
caendesilva Mar 15, 2024
ed1ae9c
Merge branch 'improved-navigation-internals' into clean-up-navigation…
caendesilva Mar 15, 2024
2439b72
Create SidebarItemsViewTest.php
caendesilva Mar 15, 2024
4f7dd3b
Add testing helper to assert if an HTML ID is present
caendesilva Mar 15, 2024
1d5e1f7
Update testing helper to fluently direct assertions
caendesilva Mar 15, 2024
4fa8dea
Set up testing helper
caendesilva Mar 15, 2024
2d9ae2a
Test component renders
caendesilva Mar 15, 2024
3ebd20f
Mock route
caendesilva Mar 15, 2024
2eeed5f
Add a route to populate testing sidebar
caendesilva Mar 15, 2024
a0c0726
Add type annotation to view
caendesilva Mar 15, 2024
12dfbc4
Add more mock pages to test
caendesilva Mar 15, 2024
361325f
Move mock route call to setup
caendesilva Mar 15, 2024
5d45940
Test high level active states
caendesilva Mar 16, 2024
627a3f2
Assert see list item class
caendesilva Mar 16, 2024
668961a
Add testing helper to assert string occurrence count
caendesilva Mar 16, 2024
f04a46f
Assert see times
caendesilva Mar 16, 2024
ce51a65
Use clearer assertion string
caendesilva Mar 16, 2024
7a3ca45
Add fluent assertion shorthand method
caendesilva Mar 16, 2024
be2bc01
Remove redundant argument
caendesilva Mar 16, 2024
9c27a98
Assert see once
caendesilva Mar 16, 2024
5061326
Merge branch 'improved-navigation-internals' into clean-up-navigation…
caendesilva Mar 17, 2024
18c3442
Merge branch 'improved-navigation-internals' into clean-up-navigation…
caendesilva Mar 17, 2024
fc3a63c
Merge branch '2.x-dev' into improved-navigation-internals
caendesilva Mar 18, 2024
f6257f9
Merge branch 'improved-navigation-internals' into clean-up-navigation…
caendesilva Mar 18, 2024
b84cdf2
Merge branch 'improved-navigation-internals' into clean-up-navigation…
caendesilva Mar 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</ul>
@else
<ul id="sidebar-items" role="list">
@php /** @var \Hyde\Framework\Features\Navigation\NavGroupItem $group */ @endphp
@foreach ($sidebar->getItems() as $group)
<li class="sidebar-group" role="listitem" @if($sidebar->isCollapsible()) x-data="{ groupOpen: {{ $sidebar->isGroupActive($group->getGroupKey()) ? 'true' : 'false' }} }" @endif>
<header @class(['sidebar-group-header p-2 px-4 -ml-2 flex justify-between items-center', 'group hover:bg-black/10' => $sidebar->isCollapsible()]) @if($sidebar->isCollapsible()) @click="groupOpen = ! groupOpen" @endif>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@isset($items)
@foreach ($items as $item)
<li class="whitespace-nowrap">
@include('hyde::components.navigation.navigation-link')
<x-hyde::navigation.navigation-link :item="$item"/>
</li>
@endforeach
@else
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<a href="{{ $item }}" {!! $item->isActive() ? 'aria-current="page"' : '' !!} @class([
'block my-2 md:my-0 md:inline-block py-1 text-gray-700 hover:text-gray-900 dark:text-gray-100',
'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' => $item->isActive()
])>{{ $item->getLabel() }}</a>
<a href="{{ $item }}" {{ $attributes->except('item')->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' => $item->isActive()
])->merge([
'aria-current' => $item->isActive() ? 'page' : false,
]) }}>{{ $item->getLabel() }}</a>
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
@if($item instanceof \Hyde\Framework\Features\Navigation\NavGroupItem)
<x-hyde::navigation.dropdown :label="$item->getLabel()" :items="$item->getItems()"/>
@else
@include('hyde::components.navigation.navigation-link')
<x-hyde::navigation.navigation-link :item="$item"/>
@endif
</li>
@endforeach
Expand Down
59 changes: 44 additions & 15 deletions packages/framework/tests/Unit/Views/NavigationLinkViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Hyde\Framework\Testing\Unit\Views;

use Hyde\Pages\InMemoryPage;
use Hyde\Support\Models\Route;
use Hyde\Testing\TestsBladeViews;
use Hyde\Testing\Support\TestView;
use Hyde\Foundation\Facades\Routes;
use Illuminate\View\ComponentAttributeBag;
use Hyde\Framework\Features\Navigation\NavItem;
use Hyde\Testing\TestCase;

Expand All @@ -24,39 +26,66 @@ protected function setUp(): void
$this->mockPage();
}

protected function render(?NavItem $item = null): string
protected function testView(): TestView
{
return view('hyde::components.navigation.navigation-link', [
'item' => $item ?? NavItem::forLink('foo.html', 'Foo'),
])->render();
return $this->view(view('hyde::components.navigation.navigation-link', [
'item' => NavItem::forRoute(new Route(new InMemoryPage('foo')), 'Foo'),
'attributes' => new ComponentAttributeBag(),
]));
}

protected function testView(?NavItem $item = null): TestView
public function testComponentRenders()
{
return $this->view(view('hyde::components.navigation.navigation-link', [
'item' => $item ?? NavItem::forLink('foo.html', 'Foo'),
]));
$this->testView()->assertHasElement('<a>');
}

public function testComponentLinksToRouteDestination()
{
$this->testView()->assertAttributeIs('href', 'foo.html');
$this->testView()->assertAttributeIs('href="foo.html"');
}

public function testComponentResolvesRelativeLinksForRoutes()
{
$this->mockCurrentPage('foo/bar');

$this->testView()->assertAttributeIs('href="../foo.html"');
}

public function testComponentUsesTitle()
{
$this->testView()->assertTextIs('Foo');
}

public function testComponentDoesNotHaveCurrentAttributesWhenCurrentRouteDoesNotMatch()
{
$this->testView()
->assertDontSee('current')
->assertDoesNotHaveAttribute('aria-current');
}

public function testComponentIsCurrentWhenCurrentRouteMatches()
{
$this->mockRoute(Routes::get('index'));
$this->assertStringContainsString('current', $this->render(NavItem::forRoute(Routes::get('index'), 'Home')));
$this->mockCurrentPage('foo');

$this->testView()
->assertSee('current')
->assertHasAttribute('aria-current')
->assertAttributeIs('aria-current="page"');
}

public function testComponentDoesNotHaveActiveClassWhenNotActive()
{
$this->testView()
->assertHasClass('navigation-link')
->assertDoesNotHaveClass('navigation-link-active');
}

public function testComponentHasAriaCurrentWhenCurrentRouteMatches()
public function testComponentHasActiveClassWhenActive()
{
$this->mockRoute(Routes::get('index'));
$this->assertStringContainsString('aria-current="page"', $this->render(NavItem::forRoute(Routes::get('index'), 'Home')));
$this->mockCurrentPage('foo');

$this->testView()
->assertHasClass('navigation-link')
->assertHasClass('navigation-link-active');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function testNavigationMenuWithRootPages()
$contents = $foo->compile();

$this->assertStringContainsString('<a href="foo.html" aria-current="page" class="', $contents);
$this->assertStringContainsString('<a href="bar.html" class="', $contents);
$this->assertStringContainsString('<a href="bar.html" class="', $contents);
}

public function testNavigationMenuWithDropdownPages()
Expand Down
70 changes: 70 additions & 0 deletions packages/framework/tests/Unit/Views/SidebarItemsViewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Testing\Unit\Views;

use Hyde\Hyde;
use Hyde\Testing\TestCase;
use Hyde\Support\Models\Route;
use Hyde\Testing\TestsBladeViews;
use Hyde\Pages\DocumentationPage;
use Hyde\Testing\Support\TestView;
use Hyde\Framework\Features\Navigation\DocumentationSidebar;
use Hyde\Framework\Features\Navigation\NavigationMenuGenerator;

/**
* @see resources/views/components/docs/sidebar-items.blade.php
*/
class SidebarItemsViewTest extends TestCase
{
use TestsBladeViews;

protected function setUp(): void
{
parent::setUp();

$this->mockRoute();
}

protected function testView(): TestView
{
Hyde::routes()->addRoute(new Route(new DocumentationPage('foo')));
Hyde::routes()->addRoute(new Route(new DocumentationPage('bar')));
Hyde::routes()->addRoute(new Route(new DocumentationPage('baz')));

return $this->view(view('hyde::components.docs.sidebar-items', [
'sidebar' => NavigationMenuGenerator::handle(DocumentationSidebar::class),
]));
}

public function testComponentRenders()
{
$this->testView()->assertHasElement('#sidebar-items')->assertSeeTimes('listitem', 3);
}

public function testViewDoesNotContainActiveStateWhenNoPageIsActive()
{
$this->testView()
->assertDontSee('active')
->assertDontSee('Table of contents')
->assertDoesNotHaveAttribute('aria-current');
}

public function testViewContainsActiveStateWhenPageIsActive()
{
$this->mockCurrentPage('docs/foo');
$this->mockPage(new DocumentationPage('foo'));

$this->testView()
->assertSeeOnce('active')
->assertSeeOnce('Table of contents')
->assertHasAttribute('aria-current')
->assertAttributeIs('aria-current="true"');
}

public function testTypeAnnotationIsNotPresentInHtml()
{
$this->testView()->assertDontSee('@var')->assertDontSee('$group');
}
}
Loading