Skip to content

Commit 74098f7

Browse files
committed
docs: fixture options and tweaks to toc
1 parent 422882c commit 74098f7

15 files changed

Lines changed: 204 additions & 15 deletions

File tree

.github/workflows/CI-CD-Docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545
- name: Deploy
4646
uses: JamesIves/github-pages-deploy-action@releases/v3
4747
with:
48-
ACCESS_TOKEN: ${{ secrets.GH_DOCS }}
48+
ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4949
BRANCH: gh-pages # The branch the action should deploy to.
5050
FOLDER: docs/_site # The folder the action should deploy.
5151
CLEAN: true

docs/docs/getting-started/fixture-options.md

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ Let us start by looking at the parameters that takes a method as input first. Th
2727

2828
In the example above, the setup and test methods are declared in a `@code { }` block nested inside the <xref:Bunit.Fixture> component. This visually groups the methods nicely to the <xref:Bunit.Fixture> component, but it's not a requirement.
2929

30-
You can have the methods anywhere inside the test component you want, which can be useful, if you for example have a common setup method that multiple Razor tests in the same test component shares. The methods can also be declared in the parameter directly, e.g.: `<Fixture Setup="f => f.Services.AddMockJsRuntime" ...>`.
30+
You can have the methods anywhere inside the test component you want, which can be useful, if you for example have a common setup method that multiple Razor tests in the same test component shares. The methods can also be declared in the parameter directly, e.g.: `<Fixture Setup="f => f.Services.AddMockJsRuntime()" ...>`.
31+
32+
> [!TIP]
33+
> Learn more about mocking and `AddMockJsRuntime()` on the <xref:mocking-ijsruntime> page.
3134
3235
**Other parameters**
3336

@@ -80,10 +83,26 @@ The generic versions of <xref:Bunit.Fixture.GetComponentUnderTest``1> and <xref:
8083
8184
## Example
8285

83-
TODO:
86+
Let's look at a complete example, where we have a simple task list component, `<SimpleTodo>`, listed below, that have a service injected, receive a cascading value, and changes between renders:
87+
88+
[!code-html[SimpleTodo.razor](../../samples/components/SimpleTodo.razor)]
89+
90+
In the test, we want to verify that:
91+
92+
- The `<form>` resets itself correctly after a task has been added
93+
- That the task was added correctly to the task list
94+
- That the "Theme" cascading value was correctly assigned to the task list
95+
96+
The test looks like this:
97+
98+
[!code-html[SimpleTodoTest.razor](../../samples/tests/razor/SimpleTodoTest.razor?highlight=4,5,8-10,13,20,29,30,35-37,44)]
99+
100+
Let's look at whats going on in this test:
101+
102+
1. The fixture has both a setup and test method specified. The setup methods is used to register an empty list of tasks, that the `<SimpleTodo>` component requires.
103+
2. The `<SimpleTodo>` component is wrapped in a `<CascadingValue>` component that passes down the "Theme" cascading value.
104+
3. The first `<Fragment>` does not have an `Id`, since the `GetFragment()` method will pick the first fragment, if no `id` is provided to it.
105+
4. The second `<Fragment Id="expected tasks">` does have an `Id` to make it possible to get it, through a call to the `GetFragment("expected tasks")` method.
106+
5. The test uses the generic version of `GetComponentUnderTest<SimpleTodo>()`, which gives us access to the instance of `SimpleTodo`, and allows us to inspect its properties, e.g. `ThemeClass`.
84107

85-
1. Create a basic component that takes a cascading value, a service, and maybe some child content, and it should have a initial rendered output and something after e.g. a click event.
86-
2. Add a setup method that registers the service
87-
3. Add two fragments, one to verify the inital rendered output and one for the output after click.
88-
4. Show how GetCompnentUnderTest returns not the cascading value
89-
5. Show how get fragment is used with and without id, and returns fragments without
108+
We will cover the details of the "act" and "assertion" step in the <xref:interaction> and <xref:verification> pages. Learn more about injecting services into components under test on the <xref:inject-services-into-components> page.

docs/docs/getting-started/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ These three pages will teach you *the basics*:
1818
After reading those, these topics will take you to the *next level*:
1919

2020
1. **[Providing different types of input](xref:providing-input)** to a component under test, e.g. passing parameters or injecting services.
21-
2. **[Verifying output in various ways](xref:verifying-output)** from a component under test, e.g. inspecting the rendered markup.
21+
2. **[Verifying output in various ways](xref:verification)** from a component under test, e.g. inspecting the rendered markup.
2222
3. **[Mocking dependencies](xref:mocking)** a component under test has, e.g. the `IJsRuntime` or `HttpClient`.
2323

2424
## Examples

docs/docs/getting-started/writing-razor-tests.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ title: Writing Tests in Razor Syntax for Blazor Components
1010
1111
A test for a Blazor component can be written in a Blazor component, using a mix of Razor and C# syntax. The advantage of this is the familiarity in declaring the component under test, and other HTML or Razor fragments that will be used in the test, _in Razor and HTML markup_. This is especially useful when testing components that takes a lot of parameters and child content as input.
1212

13-
> [!INFO]
13+
> [!NOTE]
1414
> Tests declared inside Blazor test components can be discovered and invoked individually, and will show up in e.g. Visual Studio's Test Explorer.
1515
>
1616
> However, they will _not_ show up before the Blazor test component has been compiled into C# by the Blazor compiler, and if there are compile-errors from the Blazor compiler, they might appear to come and go in the Test Explorer.

docs/docs/interaction/index.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
uid: interaction
3+
title: Interacting with a Component Under Test
4+
---
5+
6+
# Interacting with a Component Under Test
File renamed without changes.
File renamed without changes.

docs/docs/toc.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
# [Providing Input](xref:providing-input)
99
## [Parameters and Cascading Values](xref:passing-parameters-to-components)
1010
## [Inject Services](xref:inject-services-into-components)
11+
## [Configure 3rd Party Libraries](xref:configure-3rd-party-libs)
12+
13+
# [Interaction](xref:interaction)
1114
## [Trigger Event Handlers](xref:trigger-event-handlers)
1215
## [Trigger Renders](xref:trigger-renders)
13-
## [Configure 3rd Party Libraries](xref:configure-3rd-party-libs)
1416

15-
# [Verifying output](xref:verifying-output)
17+
# [Verifying output](xref:verification)
1618
## [Verify Markup](xref:verify-markup)
1719
## [Verify Component State](xref:verify-component-state)
1820
## [Semantic HTML Markup Comparison](xref:semantic-html-comparison)

docs/docs/verification/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
uid: verifying-output
2+
uid: verification
33
title: Verifying Output from a Component Under Test
44
---
55

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@inject List<string> Tasks
2+
3+
<form>
4+
<input @bind-value=@newTaskValue placeholder="Add todo here . . ." type="text" />
5+
<button type="submit" @onclick=@HandleTaskAdded>Add task</button>
6+
</form>
7+
<ul id="tasks" class=@ThemeClass>
8+
@foreach (var task in Tasks)
9+
{
10+
<li>@task</li>
11+
}
12+
</ul>
13+
@code {
14+
[CascadingParameter(Name = "Theme")]
15+
public string ThemeClass { get; set; } = string.Empty;
16+
17+
private string newTaskValue = string.Empty;
18+
19+
private void HandleTaskAdded()
20+
{
21+
if (!string.IsNullOrWhiteSpace(newTaskValue))
22+
Tasks.Add(newTaskValue);
23+
24+
newTaskValue = string.Empty;
25+
}
26+
}

0 commit comments

Comments
 (0)