Skip to content

Commit

Permalink
Move group specific tests to new unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Mar 2, 2024
1 parent 02e836e commit 737734d
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 137 deletions.
141 changes: 141 additions & 0 deletions packages/framework/tests/Unit/NavGroupItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

namespace Hyde\Framework\Testing\Unit;

use Hyde\Pages\MarkdownPage;
use Hyde\Pages\InMemoryPage;
use Hyde\Testing\UnitTestCase;
use Hyde\Support\Models\Route;
use Hyde\Support\Facades\Render;
use Hyde\Support\Models\RenderData;
use Hyde\Framework\Features\Navigation\NavItem;

/**
* @covers \Hyde\Framework\Features\Navigation\NavGroupItem
Expand All @@ -25,4 +29,141 @@ protected function setUp(): void
{
Render::swap(new RenderData());
}

public function testCanConstructWithChildren()
{
$route = new Route(new MarkdownPage());
$children = [
new NavItem(new Route(new InMemoryPage('foo')), 'Foo', 500),
new NavItem(new Route(new InMemoryPage('bar')), 'Bar', 500),
];
$item = new NavItem($route, 'Test', 500, null, $children);

$this->assertSame('Test', $item->getLabel());
$this->assertNull($item->getRoute());
$this->assertSame(500, $item->getPriority());

$this->assertCount(2, $item->getChildren());
$this->assertSame($children, $item->getChildren());

$this->assertSame('Foo', $item->getChildren()[0]->getLabel());
$this->assertSame('Bar', $item->getChildren()[1]->getLabel());

$this->assertSame('foo.html', $item->getChildren()[0]->getUrl());
$this->assertSame('bar.html', $item->getChildren()[1]->getUrl());

$this->assertSame(500, $item->getChildren()[0]->getPriority());
$this->assertSame(500, $item->getChildren()[1]->getPriority());
}

public function testCanConstructWithChildrenWithoutRoute()
{
$children = [
new NavItem(new Route(new InMemoryPage('foo')), 'Foo', 500),
new NavItem(new Route(new InMemoryPage('bar')), 'Bar', 500),
];
$item = new NavItem('', 'Test', 500, null, $children);

$this->assertSame('Test', $item->getLabel());
$this->assertNull($item->getRoute());

$this->assertCount(2, $item->getChildren());
$this->assertSame($children, $item->getChildren());
}

public function testGetChildren()
{
$children = [
new NavItem(new Route(new InMemoryPage('foo')), 'Foo', 500),
new NavItem(new Route(new InMemoryPage('bar')), 'Bar', 500),
];

$navItem = new NavItem(new Route(new InMemoryPage('foo')), 'Page', 500, null, $children);
$this->assertSame($children, $navItem->getChildren());
}

public function testGetChildrenWithNoChildren()
{
$navItem = new NavItem(new Route(new InMemoryPage('foo')), 'Page', 500);
$this->assertEmpty($navItem->getChildren());
}

public function testHasChildren()
{
$item = new NavItem(new Route(new MarkdownPage()), 'Test', 500);
$this->assertFalse($item->hasChildren());
}

public function testHasChildrenWithChildren()
{
$item = new NavItem(new Route(new MarkdownPage()), 'Test', 500, null, [
new NavItem(new Route(new InMemoryPage('foo')), 'Foo', 500),
new NavItem(new Route(new InMemoryPage('bar')), 'Bar', 500),
]);
$this->assertTrue($item->hasChildren());
}

public function testCanAddMultipleItemsToDropdown()
{
$parent = new NavItem(new Route(new MarkdownPage()), 'Parent', 500, 'foo');
$child1 = new NavItem(new Route(new MarkdownPage()), 'Child 1', 500, 'foo');
$child2 = new NavItem(new Route(new MarkdownPage()), 'Child 2', 500, 'foo');

$this->assertSame([], $parent->getChildren());

$parent->addChildren([$child1, $child2]);

$this->assertSame([$child1, $child2], $parent->getChildren());
}

public function testAddChildrenMethodReturnsSelf()
{
$parent = new NavItem(new Route(new MarkdownPage()), 'Parent', 500, 'foo');
$child1 = new NavItem(new Route(new MarkdownPage()), 'Child 1', 500, 'foo');
$child2 = new NavItem(new Route(new MarkdownPage()), 'Child 2', 500, 'foo');

$this->assertSame($parent, $parent->addChildren([$child1, $child2]));
}

public function testAddingAnItemWithAGroupKeyKeepsTheSetGroupKey()
{
$parent = new NavItem(new Route(new MarkdownPage()), 'Parent', 500, 'foo');
$child = new NavItem(new Route(new MarkdownPage()), 'Child', 500, 'bar');

$parent->addChild($child);

$this->assertSame('foo', $parent->getGroupIdentifier());
$this->assertSame('bar', $child->getGroupIdentifier());
}

public function testAddingAnItemWithNoGroupKeyUsesParentIdentifier()
{
$parent = new NavItem(new Route(new MarkdownPage()), 'Parent', 500, 'foo');
$child = new NavItem(new Route(new MarkdownPage()), 'Child', 500);

$parent->addChild($child);

$this->assertSame('foo', $parent->getGroupIdentifier());
$this->assertSame('foo', $child->getGroupIdentifier());
}

public function testCanAddItemToDropdown()
{
$parent = new NavItem(new Route(new MarkdownPage()), 'Parent', 500, 'foo');
$child = new NavItem(new Route(new MarkdownPage()), 'Child', 500, 'foo');

$this->assertSame([], $parent->getChildren());

$parent->addChild($child);

$this->assertSame([$child], $parent->getChildren());
}

public function testAddChildMethodReturnsSelf()
{
$parent = new NavItem(new Route(new MarkdownPage()), 'Parent', 500, 'foo');
$child = new NavItem(new Route(new MarkdownPage()), 'Child', 500, 'foo');

$this->assertSame($parent, $parent->addChild($child));
}
}
137 changes: 0 additions & 137 deletions packages/framework/tests/Unit/NavItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,47 +76,6 @@ public function testPassingUnknownRouteKeyToConstructorUsesExternalRoute()
$this->assertSame('foo', (string) $item->getRoute());
}

public function testCanConstructWithChildren()
{
$route = new Route(new MarkdownPage());
$children = [
new NavItem(new Route(new InMemoryPage('foo')), 'Foo', 500),
new NavItem(new Route(new InMemoryPage('bar')), 'Bar', 500),
];
$item = new NavItem($route, 'Test', 500, null, $children);

$this->assertSame('Test', $item->getLabel());
$this->assertNull($item->getRoute());
$this->assertSame(500, $item->getPriority());

$this->assertCount(2, $item->getChildren());
$this->assertSame($children, $item->getChildren());

$this->assertSame('Foo', $item->getChildren()[0]->getLabel());
$this->assertSame('Bar', $item->getChildren()[1]->getLabel());

$this->assertSame('foo.html', $item->getChildren()[0]->getUrl());
$this->assertSame('bar.html', $item->getChildren()[1]->getUrl());

$this->assertSame(500, $item->getChildren()[0]->getPriority());
$this->assertSame(500, $item->getChildren()[1]->getPriority());
}

public function testCanConstructWithChildrenWithoutRoute()
{
$children = [
new NavItem(new Route(new InMemoryPage('foo')), 'Foo', 500),
new NavItem(new Route(new InMemoryPage('bar')), 'Bar', 500),
];
$item = new NavItem('', 'Test', 500, null, $children);

$this->assertSame('Test', $item->getLabel());
$this->assertNull($item->getRoute());

$this->assertCount(2, $item->getChildren());
$this->assertSame($children, $item->getChildren());
}

public function testGetDestination()
{
$route = new Route(new InMemoryPage('foo'));
Expand Down Expand Up @@ -149,23 +108,6 @@ public function testGetGroup()
$this->assertNull($navItem->getGroupIdentifier());
}

public function testGetChildren()
{
$children = [
new NavItem(new Route(new InMemoryPage('foo')), 'Foo', 500),
new NavItem(new Route(new InMemoryPage('bar')), 'Bar', 500),
];

$navItem = new NavItem(new Route(new InMemoryPage('foo')), 'Page', 500, null, $children);
$this->assertSame($children, $navItem->getChildren());
}

public function testGetChildrenWithNoChildren()
{
$navItem = new NavItem(new Route(new InMemoryPage('foo')), 'Page', 500);
$this->assertEmpty($navItem->getChildren());
}

public function testFromRoute()
{
$route = new Route(new MarkdownPage());
Expand Down Expand Up @@ -288,21 +230,6 @@ public function testDropdownFacadeWithCustomPriority()
$this->assertSame(500, $item->getPriority());
}

public function testHasChildren()
{
$item = new NavItem(new Route(new MarkdownPage()), 'Test', 500);
$this->assertFalse($item->hasChildren());
}

public function testHasChildrenWithChildren()
{
$item = new NavItem(new Route(new MarkdownPage()), 'Test', 500, null, [
new NavItem(new Route(new InMemoryPage('foo')), 'Foo', 500),
new NavItem(new Route(new InMemoryPage('bar')), 'Bar', 500),
]);
$this->assertTrue($item->hasChildren());
}

public function testIsCurrent()
{
Render::swap(Mockery::mock(RenderData::class, [
Expand Down Expand Up @@ -384,68 +311,4 @@ public function testIdentifierUsesLabelForExternalRoute()
$item = NavItem::forLink('https://example.com', 'Foo Bar');
$this->assertSame('foo-bar', $item->getIdentifier());
}

public function testCanAddItemToDropdown()
{
$parent = new NavItem(new Route(new MarkdownPage()), 'Parent', 500, 'foo');
$child = new NavItem(new Route(new MarkdownPage()), 'Child', 500, 'foo');

$this->assertSame([], $parent->getChildren());

$parent->addChild($child);

$this->assertSame([$child], $parent->getChildren());
}

public function testAddChildMethodReturnsSelf()
{
$parent = new NavItem(new Route(new MarkdownPage()), 'Parent', 500, 'foo');
$child = new NavItem(new Route(new MarkdownPage()), 'Child', 500, 'foo');

$this->assertSame($parent, $parent->addChild($child));
}

public function testCanAddMultipleItemsToDropdown()
{
$parent = new NavItem(new Route(new MarkdownPage()), 'Parent', 500, 'foo');
$child1 = new NavItem(new Route(new MarkdownPage()), 'Child 1', 500, 'foo');
$child2 = new NavItem(new Route(new MarkdownPage()), 'Child 2', 500, 'foo');

$this->assertSame([], $parent->getChildren());

$parent->addChildren([$child1, $child2]);

$this->assertSame([$child1, $child2], $parent->getChildren());
}

public function testAddChildrenMethodReturnsSelf()
{
$parent = new NavItem(new Route(new MarkdownPage()), 'Parent', 500, 'foo');
$child1 = new NavItem(new Route(new MarkdownPage()), 'Child 1', 500, 'foo');
$child2 = new NavItem(new Route(new MarkdownPage()), 'Child 2', 500, 'foo');

$this->assertSame($parent, $parent->addChildren([$child1, $child2]));
}

public function testAddingAnItemWithAGroupKeyKeepsTheSetGroupKey()
{
$parent = new NavItem(new Route(new MarkdownPage()), 'Parent', 500, 'foo');
$child = new NavItem(new Route(new MarkdownPage()), 'Child', 500, 'bar');

$parent->addChild($child);

$this->assertSame('foo', $parent->getGroupIdentifier());
$this->assertSame('bar', $child->getGroupIdentifier());
}

public function testAddingAnItemWithNoGroupKeyUsesParentIdentifier()
{
$parent = new NavItem(new Route(new MarkdownPage()), 'Parent', 500, 'foo');
$child = new NavItem(new Route(new MarkdownPage()), 'Child', 500);

$parent->addChild($child);

$this->assertSame('foo', $parent->getGroupIdentifier());
$this->assertSame('foo', $child->getGroupIdentifier());
}
}

0 comments on commit 737734d

Please sign in to comment.