Skip to content

Commit ee3e5ae

Browse files
committed
Docs: Trigger event handlers
1 parent 44780bd commit ee3e5ae

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

docs/site/docs/interaction/index.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ title: Interacting with a Component Under Test
55

66
# Interacting with a Component Under Test
77

8+
This section covers the various ways to interact with a component under test, e.g. trigger event handlers. This and more is covered in the following sub sections:
9+
10+
- **<xref:trigger-event-handlers>:** This covers how to invoke event handlers bound to elements rendered by a component under test.
11+
- **<xref:trigger-renders>:** This covers how to manually trigger a render cycle for a component under test.
12+
- **<xref:awaiting-async-state>:** This covers how to await one or more asynchronous changes to a component under test's state, before continuing the test.

docs/site/docs/interaction/trigger-event-handlers.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,53 @@ title: Triggering Event Handlers in Components
55

66
# Triggering Event Handlers in Components
77

8-
TODO!
8+
Blazor makes it possible to bind many event handlers to elements in a Blazor component, using the `@onXXXX` syntax, e.g. `@onclick="MyClickHandler"`.
9+
10+
bUnit comes with event dispatch helper methods that makes it possible to invoke event handlers for all event types supported by Blazor.
11+
12+
**The built-in dispatch event helpers are:**
13+
14+
- [Clipboard events](xref:Bunit.ClipboardEventDispatchExtensions)
15+
- [Drag events](xref:Bunit.DragEventDispatchExtensions)
16+
- [Focus events](xref:Bunit.FocusEventDispatchExtensions)
17+
- [General events](xref:Bunit.GeneralEventDispatchExtensions)
18+
- [Input events](xref:Bunit.InputEventDispatchExtensions)
19+
- [Keyboard events](xref:Bunit.KeyboardEventDispatchExtensions)
20+
- [Media events](xref:Bunit.MediaEventDispatchExtensions)
21+
- [Mouse events](xref:Bunit.MouseEventDispatchExtensions)
22+
- [Pointer events](xref:Bunit.PointerEventDispatchExtensions)
23+
- [Progress events](xref:Bunit.ProgressEventDispatchExtensions)
24+
- [Touch event](xref:Bunit.TouchEventDispatchExtensions)
25+
26+
To use these, first find the element in the component under test where the event handler is bound to, this is usually done with the [`Find(string cssSelector)`](xref:Bunit.RenderedFragmentExtensions.Find(Bunit.IRenderedFragment,System.String)) method, and then invoke the event dispatch helper method of choice.
27+
28+
The following section demonstrates how.
29+
30+
## Invoking an Event Handler on an Element
31+
32+
To invoke an event handler on an element, first find the element in the component under test, and then call the desired event dispatch helper method.
33+
34+
Let's look at a common example, where a `@onclick` event handler is invoked. The example will use the `<ClickMe>` component listed here:
35+
36+
[!code-html[ClickMe.razor](../../../samples/components/ClickMe.razor)]
37+
38+
To trigger the `@onclick` `ClickHandler` event handler method in the `<ClickMe>` component, do the following:
39+
40+
# [C# test code](#tab/csharp)
41+
42+
[!code-csharp[ClickMeTest.cs](../../../samples/tests/xunit/ClickMeTest.cs?range=12-23&highlight=7-9)]
43+
44+
# [Razor test code](#tab/razor)
45+
46+
[!code-html[ClickMeTest.razor](../../../samples/tests/razor/ClickMeTest.razor?highlight=17-19)]
47+
48+
***
49+
50+
This is what happens in the test:
51+
52+
1. In the arrange step of the test, the `<ClickMe>` component is rendered and the `<button>` element is found using the [`Find(string cssSelector)`](xref:Bunit.RenderedFragmentExtensions.Find(Bunit.IRenderedFragment,System.String)) method.
53+
2. In the act step of the test, the `<button>`'s click event handler, in this case, the `ClickHandler` event handler method, is invoked in three different ways:
54+
- The first and second invocation uses the same [`Click`](xref:Bunit.MouseEventDispatchExtensions.Click(AngleSharp.Dom.IElement,System.Int64,System.Double,System.Double,System.Double,System.Double,System.Int64,System.Int64,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.String)) method. It has a number of optional arguments, some of which are passed in the second invocation. If any arguments are provided, they are added to an instance of the `MouseEventArgs` type, which is passed to the event handler, if it has it as an argument.
55+
- The last invocation uses the [`Click`](xref:Bunit.MouseEventDispatchExtensions.Click(AngleSharp.Dom.IElement,Microsoft.AspNetCore.Components.Web.MouseEventArgs)) method that takes an instance of the `MouseEventArgs` type, which is passed to the event handler, if it has it as an argument.
56+
57+
All the event dispatch helper methods has the same two overloads, one that takes a number of optional arguments, and one that takes the event types `EventArgs` type.

0 commit comments

Comments
 (0)