Skip to content

Commit

Permalink
test(components): add tests for the navbar components
Browse files Browse the repository at this point in the history
  • Loading branch information
desmondinho committed Jul 14, 2024
1 parent e129d53 commit 1169a74
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
103 changes: 103 additions & 0 deletions tests/LumexUI.Tests/Components/Navbar/NavbarTests.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
@namespace LumexUI.Tests.Components
@inherits TestContext

@using TailwindMerge
@using LumexUI.Common
@using Microsoft.Extensions.DependencyInjection

@code {
public NavbarTests()
{
Services.AddSingleton<TwMerge>();
JSInterop.Setup<int>( "Lumex.elementReference.getScrollHeight", _ => true );
JSInterop.SetupVoid( "Lumex.elementReference.moveElementTo", _ => true );
}

[Fact]
public void ShouldRenderCorrectly()
{
var action = () => Render(@<LumexNavbar />);

action.Should().NotThrow();
}

[Fact]
public void ShouldRenderCorrectlyWithBrand()
{
var cut = Render(
@<LumexNavbar>
<LumexNavbarBrand>LumexUI</LumexNavbarBrand>
</LumexNavbar>
);

cut.FindComponent<LumexNavbarBrand>().Should().NotBeNull();
}

[Fact]
public void ShouldRenderCorrectlyWithContentChildren()
{
var cut = Render(
@<LumexNavbar>
<LumexNavbarContent>
<LumexNavbarItem>Dashboard</LumexNavbarItem>
<LumexNavbarItem>Team</LumexNavbarItem>
<LumexNavbarItem>Deployments</LumexNavbarItem>
<LumexNavbarItem>Activity</LumexNavbarItem>
<LumexNavbarItem>Settings</LumexNavbarItem>
</LumexNavbarContent>
</LumexNavbar>
);

var content = cut.FindComponent<LumexNavbarContent>();

content.FindComponents<LumexNavbarItem>().Should().HaveCount( 5 );
}

[Fact]
public void ShouldRenderCorrectlyWithMenu()
{
string[] menuItems = ["item1", "item2", "item3", "item4", "item5"];

var cut = Render(
@<LumexNavbar>
<LumexNavbarMenuToggle />
<LumexNavbarContent>
<LumexNavbarItem>Dashboard</LumexNavbarItem>
<LumexNavbarItem>Team</LumexNavbarItem>
<LumexNavbarItem>Deployments</LumexNavbarItem>
<LumexNavbarItem>Activity</LumexNavbarItem>
<LumexNavbarItem>Settings</LumexNavbarItem>
</LumexNavbarContent>
<LumexNavbarMenu>
@foreach( var item in menuItems )
{
<LumexNavbarMenuItem>@item</LumexNavbarMenuItem>
}
</LumexNavbarMenu>
</LumexNavbar>
);

cut.FindComponent<LumexNavbarMenuToggle>().Find( "button" ).Click();

var menu = cut.FindComponent<LumexNavbarMenu>();
menu.FindAll( "li" ).Should().HaveCount( menuItems.Length );
}

[Fact]
public void ShouldNotRenderContextualComponentsAlone()
{
var action1 = () => Render(@<LumexNavbarBrand />);
var action2 = () => Render(@<LumexNavbarContent />);
var action3 = () => Render(@<LumexNavbarItem />);
var action4 = () => Render(@<LumexNavbarMenu />);
var action5 = () => Render(@<LumexNavbarMenuItem />);
var action6 = () => Render(@<LumexNavbarMenuToggle />);

action1.Should().Throw<ContextNullException>();
action2.Should().Throw<ContextNullException>();
action3.Should().Throw<ContextNullException>();
action4.Should().Throw<ContextNullException>();
action5.Should().Throw<ContextNullException>();
action6.Should().Throw<ContextNullException>();
}
}
2 changes: 1 addition & 1 deletion tests/LumexUI.Tests/LumexUI.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand Down

0 comments on commit 1169a74

Please sign in to comment.