|
4 | 4 |
|
5 | 5 | namespace Hyde\Framework\Testing\Unit\Views;
|
6 | 6 |
|
7 |
| -use Hyde\Foundation\Facades\Routes; |
| 7 | +use Hyde\Pages\InMemoryPage; |
| 8 | +use Hyde\Support\Models\Route; |
| 9 | +use Hyde\Testing\TestsBladeViews; |
| 10 | +use Hyde\Testing\Support\TestView; |
| 11 | +use Illuminate\View\ComponentAttributeBag; |
8 | 12 | use Hyde\Framework\Features\Navigation\NavItem;
|
9 | 13 | use Hyde\Testing\TestCase;
|
10 | 14 |
|
|
13 | 17 | */
|
14 | 18 | class NavigationLinkViewTest extends TestCase
|
15 | 19 | {
|
| 20 | + use TestsBladeViews; |
| 21 | + |
16 | 22 | protected function setUp(): void
|
17 | 23 | {
|
18 | 24 | parent::setUp();
|
19 | 25 | $this->mockRoute();
|
20 | 26 | $this->mockPage();
|
21 | 27 | }
|
22 | 28 |
|
23 |
| - protected function render(?NavItem $item = null): string |
| 29 | + protected function test(): TestView |
24 | 30 | {
|
25 |
| - return view('hyde::components.navigation.navigation-link', [ |
26 |
| - 'item' => $item ?? NavItem::forLink('foo.html', 'Foo'), |
27 |
| - ])->render(); |
| 31 | + return $this->test(view('hyde::components.navigation.navigation-link', [ |
| 32 | + 'item' => NavItem::forRoute(new Route(new InMemoryPage('foo')), 'Foo'), |
| 33 | + 'attributes' => new ComponentAttributeBag(), |
| 34 | + ])); |
28 | 35 | }
|
29 | 36 |
|
30 | 37 | public function testComponentLinksToRouteDestination()
|
31 | 38 | {
|
32 |
| - $this->assertStringContainsString('href="foo.html"', $this->render()); |
| 39 | + $this->test()->assertAttributeIs('href', 'foo.html'); |
| 40 | + } |
| 41 | + |
| 42 | + public function testComponentResolvesRelativeLinksForRoutes() |
| 43 | + { |
| 44 | + $this->mockCurrentPage('foo/bar'); |
| 45 | + $this->test()->assertAttributeIs('href', '../foo.html'); |
33 | 46 | }
|
34 | 47 |
|
35 | 48 | public function testComponentUsesTitle()
|
36 | 49 | {
|
37 |
| - $this->assertStringContainsString('Foo', $this->render()); |
| 50 | + $this->test()->assertTextIs('Foo'); |
| 51 | + } |
| 52 | + |
| 53 | + public function testComponentDoesNotHaveCurrentAttributesWhenCurrentRouteDoesNotMatch() |
| 54 | + { |
| 55 | + $this->test() |
| 56 | + ->assertDontSee('current') |
| 57 | + ->assertDoesNotHaveAttribute('aria-current'); |
38 | 58 | }
|
39 | 59 |
|
40 | 60 | public function testComponentIsCurrentWhenCurrentRouteMatches()
|
41 | 61 | {
|
42 |
| - $this->mockRoute(Routes::get('index')); |
43 |
| - $this->assertStringContainsString('current', $this->render(NavItem::forRoute(Routes::get('index'), 'Home'))); |
| 62 | + $this->mockCurrentPage('foo') |
| 63 | + ->test() |
| 64 | + ->assertSee('current') |
| 65 | + ->assertHasAttribute('aria-current') |
| 66 | + ->assertAttributeIs('aria-current="page"'); |
| 67 | + } |
| 68 | + |
| 69 | + public function testComponentDoesNotHaveActiveClassWhenNotActive() |
| 70 | + { |
| 71 | + $this->test() |
| 72 | + ->assertSee('navigation-link ') |
| 73 | + ->assertDontSee('navigation-link-active'); |
44 | 74 | }
|
45 | 75 |
|
46 |
| - public function testComponentHasAriaCurrentWhenCurrentRouteMatches() |
| 76 | + public function testComponentHasActiveClassWhenActive() |
47 | 77 | {
|
48 |
| - $this->mockRoute(Routes::get('index')); |
49 |
| - $this->assertStringContainsString('aria-current="page"', $this->render(NavItem::forRoute(Routes::get('index'), 'Home'))); |
| 78 | + $this->mockCurrentPage('foo') |
| 79 | + ->test() |
| 80 | + ->assertSee('navigation-link ') |
| 81 | + ->assertSee('navigation-link-active'); |
50 | 82 | }
|
51 | 83 | }
|
0 commit comments