Anchor click and NavigationManager #641
-
I would like to try a test case, the user click the anchor and the navigation can get the uri, unfortunately, the use the dotnet cli to new a WASM project, and write a testing for here is my test case
testing result
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
Hey @jiaming0708. |
Beta Was this translation helpful? Give feedback.
-
Hi there, Can you share what your component under test looks like? |
Beta Was this translation helpful? Give feedback.
-
@egil he's using the default <div class="top-row ps-3 navbar navbar-dark">
<div class="container-fluid">
<a class="navbar-brand" href="">BlazorApp1</a>
<button title="Navigation menu" class="navbar-toggler" @onclick="ToggleNavMenu">
<span class="navbar-toggler-icon"></span>
</button>
</div>
</div>
<div class="@NavMenuCssClass" @onclick="ToggleNavMenu">
<nav class="flex-column">
<div class="nav-item px-3">
<NavLink class="nav-link" href="" Match="NavLinkMatch.All">
<span class="oi oi-home" aria-hidden="true"></span> Home
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="counter">
<span class="oi oi-plus" aria-hidden="true"></span> Counter
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="fetchdata">
<span class="oi oi-list-rich" aria-hidden="true"></span> Fetch data
</NavLink>
</div>
</nav>
</div>
@code {
private bool collapseNavMenu = true;
private string? NavMenuCssClass => collapseNavMenu ? "collapse" : null;
private void ToggleNavMenu()
{
collapseNavMenu = !collapseNavMenu;
}
} |
Beta Was this translation helpful? Give feedback.
Hey @jiaming0708.
It looks like you are testing what a browser would do when you click on a a href element. That is more like an integration test than a unit test (which is the intention of bUnit).
Furthermore you would test the implementation provided by Blazor. As a rule of thumb you do not want to test 3rd party code.
You can relay that Blazor (NavigationManager, Router, ...) does the right thing once you click on a href element.