Skip to content

NavLink ActiveClass is always active #903

Closed

Description

@TDroogers

Describe the bug

If you add an attribute ActiveClass this class is always added to the class.

I tried using var navMan = Services.GetRequiredService<NavigationManager>(); but with no success.

Example:
Testing this component:

        <NavLink @onclick="@HandleNavigation" 
                 class="@LinkClassname" 
                 @attributes="@Attributes"
                 Match="@Match" 
                 ActiveClass="@ActiveClass">
            @if (!string.IsNullOrEmpty(Icon))
            {
                <MudIcon Icon="@Icon" Color="@IconColor" Class="@IconClassname"/>
            }
            <div class="mud-nav-link-text">
                @ChildContent
            </div>
        </NavLink>

With this test:

        [Test]
        public async Task NavLink_Active()
        {
            const string activeClass = "Custom__nav_active_css";
            var comp = Context.RenderComponent<MudNavLink>(Parameter(nameof(MudNavLink.ActiveClass), activeClass));
            comp.Markup.Should().NotContain(activeClass); //this one fails
            comp.Find(".mud-nav-link").Click();
            comp.Markup.Should().Contain(activeClass);
        }

Results in this output:

Did not expect comp.Markup "<div class="mud-nav-item mud-ripple"><a blazor:onclick="1" rel="" class="mud-nav-link Custom__nav_active_css"><div class="...

Expected behavior:

ActiveClass should be part of css depending on the page you are.

Version info:

  • bUnit version: 1.11.7
  • .NET Runtime and Blazor version: 6.x
  • OS type and version: windows 11

Activity

  1. egil commented on Oct 31, 2022

    @egil
    Member

    Hi @TDroogers, thanks for reaching out.

    Just took a quick look at the source for NavLink and it looks like you have to specify a href attribute on NavLink and set a url in navigation manager to get NavLink to set the active class.

    Here are the docs for working with the built-in navigation manager in bUnit: https://bunit.dev/docs/test-doubles/fake-navigation-manager.html

  2. egil commented on Oct 31, 2022

    @egil
    Member

    Ps. ill convert this to a QA discussion since this is not a bug in bUnit.

  3. locked and limited conversation to collaborators on Oct 31, 2022
  4. converted this issue into a discussion #904 on Oct 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions