Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Blazor component testing #2944

Open
sand4rt opened this issue Jun 4, 2024 · 5 comments
Open

[Feature]: Blazor component testing #2944

sand4rt opened this issue Jun 4, 2024 · 5 comments

Comments

@sand4rt
Copy link

sand4rt commented Jun 4, 2024

🚀 Feature Request

Wondering if you've considered adding Playwright component testing for Blazor: https://learn.microsoft.com/en-us/aspnet/core/blazor/test?view=aspnetcore-8.0#test-components-with-bunit?

Example

<button @onclick="HandleClick">@title</button>

@code {
    [Parameter]
    public string title { get; set; } = "";
}
@code
{
    [Fact]
    public async Task HasTitle()
    { 
        var Component = await mount(@<Button title="Hello" />);
        await Expect(Component).ToHaveText("Hello");
    }
}

or

public class Button
{
    [Fact]
    public void HasTitle()
    {
        var Component = mount<Button>({ props = { title = "hello" });
        await Expect(Component).ToHaveText("Hello");
    }
}

Motivation

I'm not a Blazor user, but i think it might be interesting for the people who do?

@mxschmitt
Copy link
Member

Let's see if there is any interest for it.

@kelmelzer
Copy link

I would love this. I just recently adopted Blazor and can definitely see some use cases.

@thatstatsguy
Copy link

@sand4rt is your concern that bUnit isn't comprehensive enough? Just trying to better understand what the big benefit would be in bringing this into Playwright?

@mxschmitt
Copy link
Member

Playwright would be a real browser while bUnit is an emulated DOM like JSDom from my understanding.

@UniMichael
Copy link

@thatstatsguy Because it doesn't use an actual browser, bUnit expects you to mock your JavaScript interops and won't respect certain HTML attributes in its test (i.e. it'll still be able to click a disabled button).

While you can kind of get away with telling people to just confirm that those HTML attributes are there and not have them try to click a disabled button in their tests, having people mock their JavaScript isn't really possible if they're relying on that JavaScript to have non-trivial side-effects.

Some examples from our own Blazor component library where we've had to use Playwright include:

  • Using an input masking library that manipulates your underlying value: You want to make sure it's actually behaving properly and returning the correct value to your component.
  • Waiting for a specific JavaScript event listener to call your C# code through a DotNetObjectReference and [JSInvokable].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants