Repository: dotnet/aspnetcore
File: src/Components/Web/test/Routing/NavLinkTest.cs (C#)

Code before:
        var batch = renderer.Batches.Single();
        var hrefFrame = batch.ReferenceFrames.First(f => f.AttributeName == "href");
        Assert.Equal("https://example.com/sub-site/details", hrefFrame.AttributeValue);
    }

    [Fact]
    public async Task NavLink_WithRelativeToCurrentUri_HandlesNestedPaths()
    {
        var testNavigationManager = new TestNavigationManager();
        testNavigationManager.Initialize("https://example.com/", "https://example.com/a/b/c/page");

        var renderer = new TestRenderer();
        var component = new NavLink();
        
        var componentId = renderer.AssignRootComponentId(component);
        SetNavigationManager(component, testNavigationManager);
        
        var parameters = ParameterView.FromDictionary(new Dictionary<string, object?>
        {
            [nameof(NavLink.RelativeToCurrentUri)] = true,
            [nameof(NavLink.AdditionalAttributes)] = new Dictionary<string, object>
            {
                ["href"] = "sibling"
            }
        });
        
        await renderer.RenderRootComponentAsync(componentId, parameters);

        var batch = renderer.Batches.Single();
        var hrefFrame = batch.ReferenceFrames.First(f => f.AttributeName == "href");
        Assert.Equal("https://example.com/a/b/c/sibling", hrefFrame.AttributeValue);
    }

    [Fact]
    public async Task NavLink_WithRelativeToCurrentUri_HandlesQueryAndFragment()
    {
        var testNavigationManager = new TestNavigationManager();
        testNavigationManager.Initialize("https://example.com/", "https://example.com/folder/page?query=value#hash");

        var renderer = new TestRenderer();
        var component = new NavLink();
        
        var componentId = renderer.AssignRootComponentId(component);
        SetNavigationManager(component, testNavigationManager);
        
        var parameters = ParameterView.FromDictionary(new Dictionary<string, object?>
        {
            [nameof(NavLink.RelativeToCurrentUri)] = true,
            [nameof(NavLink.AdditionalAttributes)] = new Dictionary<string, object>
            {
                ["href"] = "other"

Review comment (refactor):
Lots of repetition on these tests. Consider creating helper methods for common setup and maybe combining some tests into theories where it makes sense.

Code after:

    [Fact]
    public async Task NavLink_WithRelativeToCurrentUri_PreservesActiveClassLogic()
    {
        var classValue = await RenderNavLinkAndGetAttributeAsync(
            "https://example.com/", "https://example.com/sub-site/details",
            href: "details", relativeToCurrentUri: true, attributeName: "class");

        Assert.Equal("active", classValue);
    }

    private async Task<object?> RenderNavLinkAndGetAttributeAsync(
        string baseUri, string currentUri, string href, bool relativeToCurrentUri, string attributeName)
    {
        var navigationManager = new TestNavigationManager();
        navigationManager.Initialize(baseUri, currentUri);

        var renderer = new TestRenderer();
        var component = new NavLink { NavigationManager = navigationManager };
        var componentId = renderer.AssignRootComponentId(component);

        var parameters = ParameterView.FromDictionary(new Dictionary<string, object?>
        {
            [nameof(NavLink.RelativeToCurrentUri)] = relativeToCurrentUri,
            [nameof(NavLink.AdditionalAttributes)] = new Dictionary<string, object> { ["href"] = href }
        });

        await renderer.RenderRootComponentAsync(componentId, parameters);

        var batch = renderer.Batches.Single();
        return batch.ReferenceFrames.FirstOrDefault(f => f.AttributeName == attributeName).AttributeValue;
    }

    private class TestNavigationManager : NavigationManager
    {
        public new void Initialize(string baseUri, string uri) => base.Initialize(baseUri, uri);

        protected override void NavigateToCore(string uri, NavigationOptions options)
        {
            Uri = uri;
            NotifyLocationChanged(false);
        }
    }
}