Skip to content

Commit 7568c17

Browse files
committed
docs: Added examples for query strings in net8
1 parent 5d4c799 commit 7568c17

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

docs/site/docs/providing-input/passing-parameters-to-components.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,36 @@ When rendering a `RenderFragment` using the <xref:Bunit.TestContext.Render(Micro
466466
}
467467
```
468468

469+
## Passing query parameters (`SupplyParameterFromQuery`) to a component
470+
Since .NET 6 components can receive parameters from the query string. From .NET 6 until .NET 8 `SupplyParameterFromQuery` also had to be a `Parameter`. The fact that they represent regular Blazor parameters means that <xref:Bunit.ComponentParameterCollectionBuilder`1>'s `Add` can be used. However, from .NET 8 onwards `SupplyParameterFromQuery` can be used without the need to be a `Parameter`. If it used without ther `Parameter` attribute, the <xref:Bunit.TestDoubles.FakeNavigationManager> can be used.
471+
472+
```razor
473+
@code {
474+
[SupplyParameterFromQuery]
475+
public string Name { get; set; }
476+
}
477+
```
478+
479+
A simple example of how to test a component that receives parameters from the query string:
480+
481+
```razor
482+
@inherits TestContext
483+
484+
@code {
485+
[Fact]
486+
public void Component_receives_parameters_from_query_string()
487+
{
488+
var navigationManager = Services.GetRequiredService<NavigationManager>();
489+
var uri = navigationManager.GetUriWithQueryParameter("Name", "bUnit");
490+
navigationManager.NavigateTo(uri);
491+
492+
var cut = RenderComponent<SupplyFromQueryParameterComponent>();
493+
494+
cut.Instance.Name.ShouldBe("bUnit");
495+
}
496+
}
497+
```
498+
469499
## Further Reading
470500

471501
- <xref:inject-services>

0 commit comments

Comments
 (0)