ComponentTestHost Class
A browser that renders Blazor components: the components are interactive (as in Blazor WebAssembly), their output is the DOM of a page of Meziantou.Framework.WebPlatform, with its styles, its layout and its user input.
public sealed class ComponentTestHost : TestPageExample
await using var host = new ComponentTestHost();
var counter = await host.RenderAsync<Counter>(parameters => parameters.Add(c => c.IncrementAmount, 2));
// A real click: the pointer moves to the center of the button, the hit testing finds the button
await counter.Find("button").ClickAsync();
await counter.GetByRole("button", "Click me").ClickAsync();
Assert.Equal("Current count: 4", counter.GetByRole("status").TextContent);
Assert.Equal("""
<p role="status">Current count: 4</p>
<button class="btn">Click me</button>
""", counter.Markup);- Inheritance
TestPageComponentTestHost
Constructors
| Name | Description |
|---|---|
| ComponentTestHost(ComponentTestHostOptions?) | Initializes a new instance of the ComponentTestHost class. |
ComponentTestHost(ComponentTestHostOptions?)
ComponentTestHost.csInitializes a new instance of the ComponentTestHost class.
public ComponentTestHost(ComponentTestHostOptions? options = null)Parameters
optionsComponentTestHostOptions?- The options, or null for the default options.
Properties
| Name | Description |
|---|---|
| JSInterop | Gets the JavaScript interop of the components. |
| NavigationManager | Gets the navigation manager of the components. |
| ServiceProvider | Gets the services of the components. The first access builds them from ComponentTestHost.Services. |
| Services | Gets the services of the components. Add services before the first component is rendered. |
JSInterop
ComponentTestHost.csGets the JavaScript interop of the components.
public ComponentJSInterop JSInterop { get; }Property value
ServiceProvider
ComponentTestHost.csGets the services of the components. The first access builds them from ComponentTestHost.Services.
public IServiceProvider ServiceProvider { get; }Property value
Services
ComponentTestHost.csGets the services of the components. Add services before the first component is rendered.
public IServiceCollection Services { get; }Property value
Methods
| Name | Description |
|---|---|
| GetElement(ElementReference) | Gets the element of an ElementReference captured by a component. |
| InvokeAsync(Action, CancellationToken) | Runs code on the dispatcher of the components (as ComponentBase.InvokeAsync), then waits until the components are idle. |
| InvokeAsync(Func<Task>, CancellationToken) | Runs code on the dispatcher of the components (as ComponentBase.InvokeAsync), then waits until the components are idle. |
| NavigateAsync(string, CancellationToken) | Navigates to a URL of the application, as a click on a link does (the router renders the page of the URL). |
| Render<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>>?, RenderOptions?) | Renders a component without waiting for its asynchronous lifecycle methods: the result shows the first render (for instance a loading indicator). Use TestPage.WaitForIdleAsync or TestPage.WaitForAsync afterwards. |
| RenderAsync(RenderFragment, RenderOptions?, CancellationToken) | Renders a fragment (for instance @<MyComponent Value="1" /> in a Razor test), and waits until it is idle. |
| RenderAsync<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>>?, RenderOptions?, CancellationToken) | Renders a component, and waits until the component and its descendants are idle (their asynchronous lifecycle methods and the tasks of the page are completed). |
GetElement(ElementReference)
ComponentTestHost.csGets the element of an ElementReference captured by a component.
public Element? GetElement(ElementReference reference)Parameters
referenceElementReference- The reference.
Returns
Element? The element, or null when it is not in the document.
InvokeAsync overloads
InvokeAsync(Action, CancellationToken)
ComponentTestHost.csRuns code on the dispatcher of the components (as ComponentBase.InvokeAsync), then waits until the components are idle.
public Task InvokeAsync(Action action, CancellationToken cancellationToken = default)Parameters
actionAction- The code.
cancellationTokenCancellationToken- A token to cancel the wait.
Returns
Task A task completed once the components are idle.
InvokeAsync(Func<Task>, CancellationToken)
ComponentTestHost.csRuns code on the dispatcher of the components (as ComponentBase.InvokeAsync), then waits until the components are idle.
net10.0
public Task InvokeAsync(Func<Task> action, CancellationToken cancellationToken = default)net11.0
public Task InvokeAsync(Func<Task> action, CancellationToken cancellationToken = default)Parameters
actionFunc<Task>- The code.
cancellationTokenCancellationToken- A token to cancel the wait.
Returns
Task A task completed once the components are idle.
Render<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>>?, RenderOptions?)
ComponentTestHost.csRenders a component without waiting for its asynchronous lifecycle methods: the result shows the first render (for instance a loading indicator). Use TestPage.WaitForIdleAsync or TestPage.WaitForAsync afterwards.
public RenderedComponent<TComponent> Render<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>>? parameters = null, RenderOptions? options = null) where TComponent : IComponentType parameters
TComponent- The type of the component.
Parameters
parametersAction<ComponentParameterCollectionBuilder<TComponent>>?- Sets the parameters of the component, or null.
optionsRenderOptions?- Where the component is rendered, or null to render it in the
bodyof the page.
Returns
RenderedComponent<TComponent> The rendered component.
RenderAsync overloads
RenderAsync(RenderFragment, RenderOptions?, CancellationToken)
ComponentTestHost.csRenders a fragment (for instance @<MyComponent Value="1" /> in a Razor test), and waits until it is idle.
public Task<RenderedComponent<FragmentContainer>> RenderAsync(RenderFragment fragment, RenderOptions? options = null, CancellationToken cancellationToken = default)Parameters
fragmentRenderFragment- The fragment.
optionsRenderOptions?- Where the fragment is rendered, or null to render it in the
bodyof the page. cancellationTokenCancellationToken- A token to cancel the wait.
Returns
Task<RenderedComponent<FragmentContainer>> The rendered fragment.
RenderAsync<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>>?, RenderOptions?, CancellationToken)
ComponentTestHost.csRenders a component, and waits until the component and its descendants are idle (their asynchronous lifecycle methods and the tasks of the page are completed).
net10.0
public Task<RenderedComponent<TComponent>> RenderAsync<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>>? parameters = null, RenderOptions? options = null, CancellationToken cancellationToken = default) where TComponent : IComponentnet11.0
public Task<RenderedComponent<TComponent>> RenderAsync<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>>? parameters = null, RenderOptions? options = null, CancellationToken cancellationToken = default) where TComponent : IComponentType parameters
TComponent- The type of the component.
Parameters
parametersAction<ComponentParameterCollectionBuilder<TComponent>>?- Sets the parameters of the component, or null.
optionsRenderOptions?- Where the component is rendered, or null to render it in the
bodyof the page. cancellationTokenCancellationToken- A token to cancel the wait.
Returns
Task<RenderedComponent<TComponent>> The rendered component.