Skip to content

when use JSInterop to mock js operation, the test will be blocked #771

@cysnet

Description

@cysnet

bUnitVersion: 1.7.7 - 1.9.8

when use JSInterop to mock js operation, the test will be blocked.
image
image

razor code as below

@using Syncfusion.Blazor.Popups
@using Syncfusion.Blazor.Inputs

<div id="target">
    <SfDialog Target="#target" Width="500px">
        <DialogTemplates>
            <Header> 登录 </Header>
            <Content>
                <SfTextBox Placeholder="用户" @bind-Value="UserName" FloatLabelType="@FloatLabelType.Always"></SfTextBox>

                <SfTextBox Placeholder="密码" @bind-Value="Password" FloatLabelType="@FloatLabelType.Always" Type="InputType.Password"></SfTextBox>
            </Content>
        </DialogTemplates>
        <DialogButtons>
            <DialogButton id="login" IsPrimary="true" Content="登录" OnClick="@OnBtnClickAsync" />
        </DialogButtons>
    </SfDialog>
</div>


<style>
    #target .e-dialog {
        height: 270px;
    }

    #target {
        height: 500px;
    }
</style>

    public partial class Login
    {
        public string UserName { get; set; }
        public string Password { get; set; }

        [Inject]
        public ILoginService LoginService { get; set; }

        [Inject]
        public AuthenticationStateProvider AuthProvider { get; set; }

        private async Task OnBtnClickAsync()
        {
            if (await LoginService.LoginAsync(UserName, Password))
            {
                await JSRuntime.InvokeVoidAsync("localStorage.setItem", "userName", UserName);
                //System.Diagnostics.Trace.WriteLine("set item done");
                await AuthProvider.GetAuthenticationStateAsync();
                //System.Diagnostics.Trace.WriteLine("get auth state done");
                NavigationManager.NavigateTo("/patch/list");
                //System.Diagnostics.Trace.WriteLine("navigate done");
            }
        }
    }

test code as below

        [Fact]
        public void LoginComponentTest()
        {
            var testUser = "test";
            Services.AddSyncFusionBlazor();
            Services.AddScoped(p =>
            {
                var loginService = new Mock<ILoginService>();
                loginService.Setup(m => m.LoginAsync(testUser, It.IsAny<string>())).ReturnsAsync(true);
                return loginService.Object;
            });

            JSInterop.SetupVoid("localStorage.setItem", "userName", testUser);
            //JSInterop.SetupVoid("localStorage.setItem", _ => true);
            this.AddTestAuthorization().SetAuthorized(testUser);

            var cut = RenderComponent<U9C.SystemManage.Login.Login>();
            cut.Instance.UserName = "test";
            cut.Instance.Password = "123";
            cut.Find("#login").Click();

            cut.WaitForState(() =>
            {
                var navMan = Services.GetRequiredService<FakeNavigationManager>();
                return "http://localhost/patch/list" == navMan.Uri;
            }, TimeSpan.FromSeconds(30));
            System.Diagnostics.Trace.WriteLine(cut.Markup);

            var navMan = Services.GetRequiredService<FakeNavigationManager>();
            Assert.Equal("http://localhost/patch/list", navMan.Uri);
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions