Repository: dotnet/aspnetcore
File: src/Components/Components/test/NavigationManagerTest.cs (C#)

Code before:
        var navigationManager = new TestNavigationManager(baseUri);
        var values = new Dictionary<string, object>
        {
            ["name1"] = "value1",
            [string.Empty] = "value2",
        };

        var exception = Assert.Throws<InvalidOperationException>(() => navigationManager.GetUriWithQueryParameters(values));
        Assert.StartsWith("Cannot have empty query parameter names.", exception.Message);
    }

    [Theory]
    [InlineData("scheme://host/", "section1", "scheme://host/#section1")]
    [InlineData("scheme://host/", "#section1", "scheme://host/#section1")]
    [InlineData("scheme://host/path", "section1", "scheme://host/path#section1")]
    [InlineData("scheme://host/path/", "section1", "scheme://host/path/#section1")]
    [InlineData("scheme://host/path?query=value", "section1", "scheme://host/path?query=value#section1")]
    [InlineData("scheme://host/path?query=value#oldHash", "section1", "scheme://host/path?query=value#section1")]
    [InlineData("scheme://host/path#oldHash", "newHash", "scheme://host/path#newHash")]
    [InlineData("scheme://host/path#old", "#new", "scheme://host/path#new")]
    public void GetUriWithHash_AddsOrReplacesHash(string baseUri, string hash, string expectedUri)
    {
        var navigationManager = new TestNavigationManager(baseUri);
        var actualUri = navigationManager.GetUriWithHash(hash);

        Assert.Equal(expectedUri, actualUri);
    }

    [Theory]
    [InlineData("scheme://host/", "scheme://host/")]
    [InlineData("scheme://host/path", "scheme://host/path")]
    [InlineData("scheme://host/path#hash", "scheme://host/path")]
    [InlineData("scheme://host/path?query=value#hash", "scheme://host/path?query=value")]
    public void GetUriWithHash_RemovesHashWhenHashIsNullOrEmpty(string baseUri, string expectedUri)
    {
        var navigationManager = new TestNavigationManager(baseUri);

        var actualUriWithNull = navigationManager.GetUriWithHash(null);
        Assert.Equal(expectedUri, actualUriWithNull);

        var actualUriWithEmpty = navigationManager.GetUriWithHash(string.Empty);
        Assert.Equal(expectedUri, actualUriWithEmpty);
    }

    [Fact]
    public void GetUriWithHash_ThrowsWhenNavigationManagerIsNull()
    {
        NavigationManager navigationManager = null;

        var exception = Assert.Throws<ArgumentNullException>(() => navigationManager.GetUriWithHash("hash"));
        Assert.Equal("navigationManager", exception.ParamName);

Review comment (suggestion):
None of these tests really checks base uri different than root. In the issue we had an example of a case with `<base href="/subdir/">`, we should test it.

Code after:
        var navigationManager = new TestNavigationManager(baseUri);
        var values = new Dictionary<string, object>
        {
            ["name1"] = "value1",
            [string.Empty] = "value2",
        };

        var exception = Assert.Throws<InvalidOperationException>(() => navigationManager.GetUriWithQueryParameters(values));
        Assert.StartsWith("Cannot have empty query parameter names.", exception.Message);
    }

    [Theory]
    [InlineData("scheme://host/", "section1", "scheme://host/#section1")]
    [InlineData("scheme://host/", "#section1", "scheme://host/#section1")]
    [InlineData("scheme://host/path", "section1", "scheme://host/path#section1")]
    [InlineData("scheme://host/path/", "section1", "scheme://host/path/#section1")]
    [InlineData("scheme://host/path?query=value", "section1", "scheme://host/path?query=value#section1")]
    [InlineData("scheme://host/path?query=value#oldHash", "section1", "scheme://host/path?query=value#section1")]
    [InlineData("scheme://host/path#oldHash", "newHash", "scheme://host/path#newHash")]
    [InlineData("scheme://host/path#old", "#new", "scheme://host/path#new")]
    // Tests with non-root base paths (e.g., when using <base href="/subdir/">)
    [InlineData("scheme://host/subdir/page", "section1", "scheme://host/subdir/page#section1")]
    [InlineData("scheme://host/subdir/page", "#section1", "scheme://host/subdir/page#section1")]
    [InlineData("scheme://host/subdir/page#old", "section1", "scheme://host/subdir/page#section1")]
    [InlineData("scheme://host/app/subdir/page?query=value", "section1", "scheme://host/app/subdir/page?query=value#section1")]
    public void GetUriWithHash_AddsOrReplacesHash(string baseUri, string hash, string expectedUri)
    {
        var navigationManager = new TestNavigationManager(baseUri);
        var actualUri = navigationManager.GetUriWithHash(hash);

        Assert.Equal(expectedUri, actualUri);
    }

    [Theory]
    [InlineData("scheme://host/", "scheme://host/")]
    [InlineData("scheme://host/path", "scheme://host/path")]
    [InlineData("scheme://host/path#hash", "scheme://host/path")]
    [InlineData("scheme://host/path?query=value#hash", "scheme://host/path?query=value")]
    // Tests with non-root base paths (e.g., when using <base href="/subdir/">)
    [InlineData("scheme://host/subdir/page#hash", "scheme://host/subdir/page")]
    [InlineData("scheme://host/app/subdir/page?query=value#hash", "scheme://host/app/subdir/page?query=value")]
    public void GetUriWithHash_RemovesHashWhenHashIsEmpty(string baseUri, string expectedUri)
    {
        var navigationManager = new TestNavigationManager(baseUri);

        var actualUriWithEmpty = navigationManager.GetUriWithHash(string.Empty);
        Assert.Equal(expectedUri, actualUriWithEmpty);
    }

    [Fact]
    public void GetUriWithHash_ThrowsWhenNavigationManagerIsNull()