Skip to content

Commit 4925628

Browse files
committed
Added tests for general events and touch events dispatch extensions
1 parent 51d3a0f commit 4925628

3 files changed

Lines changed: 61 additions & 14 deletions

File tree

src/EventDispatchExtensions/GeneralEventDispatchExtensions.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
namespace Egil.RazorComponents.Testing.EventDispatchExtensions
1212
{
13-
// TODO: add support for all event types listed here: https://github.com/aspnet/AspNetCore/blob/master/src/Components/Web/src/Web/EventHandlers.cs
14-
1513
/// <summary>
1614
/// General event dispatch helper extension methods.
1715
/// </summary>

src/EventDispatchExtensions/TouchEventDispatchExtensions.cs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public static void TouchCancel(this IElement element, long detail = default, Tou
4242
/// </summary>
4343
/// <param name="element">The element to raise the event on.</param>
4444
/// <param name="eventArgs">The event arguments to pass to the event handler.</param>
45-
public static void TouchCancel(this IElement element, TouchEventArgs eventArgs) => _ = TouchCancelAsync(element, eventArgs);
45+
public static void TouchCancel(this IElement element, TouchEventArgs eventArgs)
46+
=> _ = TouchCancelAsync(element, eventArgs);
4647

4748
/// <summary>
4849
/// Raises the <c>@ontouchcancel</c> event on <paramref name="element"/>, passing the provided <paramref name="eventArgs"/>
@@ -51,7 +52,8 @@ public static void TouchCancel(this IElement element, long detail = default, Tou
5152
/// <param name="element"></param>
5253
/// <param name="eventArgs"></param>
5354
/// <returns>A task that completes when the event handler is done.</returns>
54-
public static Task TouchCancelAsync(this IElement element, TouchEventArgs eventArgs) => element.TriggerEventAsync("ontouchcancel", eventArgs);
55+
public static Task TouchCancelAsync(this IElement element, TouchEventArgs eventArgs)
56+
=> element.TriggerEventAsync("ontouchcancel", eventArgs);
5557

5658
/// <summary>
5759
/// Raises the <c>@ontouchend</c> event on <paramref name="element"/>, passing the provided
@@ -82,7 +84,8 @@ public static void TouchEnd(this IElement element, long detail = default, TouchP
8284
/// </summary>
8385
/// <param name="element">The element to raise the event on.</param>
8486
/// <param name="eventArgs">The event arguments to pass to the event handler.</param>
85-
public static void TouchEnd(this IElement element, TouchEventArgs eventArgs) => _ = TouchEndAsync(element, eventArgs);
87+
public static void TouchEnd(this IElement element, TouchEventArgs eventArgs)
88+
=> _ = TouchEndAsync(element, eventArgs);
8689

8790
/// <summary>
8891
/// Raises the <c>@ontouchend</c> event on <paramref name="element"/>, passing the provided <paramref name="eventArgs"/>
@@ -91,7 +94,8 @@ public static void TouchEnd(this IElement element, long detail = default, TouchP
9194
/// <param name="element"></param>
9295
/// <param name="eventArgs"></param>
9396
/// <returns>A task that completes when the event handler is done.</returns>
94-
public static Task TouchEndAsync(this IElement element, TouchEventArgs eventArgs) => element.TriggerEventAsync("ontouchend", eventArgs);
97+
public static Task TouchEndAsync(this IElement element, TouchEventArgs eventArgs)
98+
=> element.TriggerEventAsync("ontouchend", eventArgs);
9599

96100
/// <summary>
97101
/// Raises the <c>@ontouchmove</c> event on <paramref name="element"/>, passing the provided
@@ -122,7 +126,8 @@ public static void TouchMove(this IElement element, long detail = default, Touch
122126
/// </summary>
123127
/// <param name="element">The element to raise the event on.</param>
124128
/// <param name="eventArgs">The event arguments to pass to the event handler.</param>
125-
public static void TouchMove(this IElement element, TouchEventArgs eventArgs) => _ = TouchMoveAsync(element, eventArgs);
129+
public static void TouchMove(this IElement element, TouchEventArgs eventArgs)
130+
=> _ = TouchMoveAsync(element, eventArgs);
126131

127132
/// <summary>
128133
/// Raises the <c>@ontouchmove</c> event on <paramref name="element"/>, passing the provided <paramref name="eventArgs"/>
@@ -131,7 +136,8 @@ public static void TouchMove(this IElement element, long detail = default, Touch
131136
/// <param name="element"></param>
132137
/// <param name="eventArgs"></param>
133138
/// <returns>A task that completes when the event handler is done.</returns>
134-
public static Task TouchMoveAsync(this IElement element, TouchEventArgs eventArgs) => element.TriggerEventAsync("ontouchmove", eventArgs);
139+
public static Task TouchMoveAsync(this IElement element, TouchEventArgs eventArgs)
140+
=> element.TriggerEventAsync("ontouchmove", eventArgs);
135141

136142
/// <summary>
137143
/// Raises the <c>@ontouchstart</c> event on <paramref name="element"/>, passing the provided
@@ -162,7 +168,8 @@ public static void TouchStart(this IElement element, long detail = default, Touc
162168
/// </summary>
163169
/// <param name="element">The element to raise the event on.</param>
164170
/// <param name="eventArgs">The event arguments to pass to the event handler.</param>
165-
public static void TouchStart(this IElement element, TouchEventArgs eventArgs) => _ = TouchStartAsync(element, eventArgs);
171+
public static void TouchStart(this IElement element, TouchEventArgs eventArgs)
172+
=> _ = TouchStartAsync(element, eventArgs);
166173

167174
/// <summary>
168175
/// Raises the <c>@ontouchstart</c> event on <paramref name="element"/>, passing the provided <paramref name="eventArgs"/>
@@ -171,7 +178,8 @@ public static void TouchStart(this IElement element, long detail = default, Touc
171178
/// <param name="element"></param>
172179
/// <param name="eventArgs"></param>
173180
/// <returns>A task that completes when the event handler is done.</returns>
174-
public static Task TouchStartAsync(this IElement element, TouchEventArgs eventArgs) => element.TriggerEventAsync("ontouchstart", eventArgs);
181+
public static Task TouchStartAsync(this IElement element, TouchEventArgs eventArgs)
182+
=> element.TriggerEventAsync("ontouchstart", eventArgs);
175183

176184
/// <summary>
177185
/// Raises the <c>@ontouchenter</c> event on <paramref name="element"/>, passing the provided
@@ -202,7 +210,8 @@ public static void TouchEnter(this IElement element, long detail = default, Touc
202210
/// </summary>
203211
/// <param name="element">The element to raise the event on.</param>
204212
/// <param name="eventArgs">The event arguments to pass to the event handler.</param>
205-
public static void TouchEnter(this IElement element, TouchEventArgs eventArgs) => _ = TouchEnterAsync(element, eventArgs);
213+
public static void TouchEnter(this IElement element, TouchEventArgs eventArgs)
214+
=> _ = TouchEnterAsync(element, eventArgs);
206215

207216
/// <summary>
208217
/// Raises the <c>@ontouchenter</c> event on <paramref name="element"/>, passing the provided <paramref name="eventArgs"/>
@@ -211,7 +220,8 @@ public static void TouchEnter(this IElement element, long detail = default, Touc
211220
/// <param name="element"></param>
212221
/// <param name="eventArgs"></param>
213222
/// <returns>A task that completes when the event handler is done.</returns>
214-
public static Task TouchEnterAsync(this IElement element, TouchEventArgs eventArgs) => element.TriggerEventAsync("ontouchenter", eventArgs);
223+
public static Task TouchEnterAsync(this IElement element, TouchEventArgs eventArgs)
224+
=> element.TriggerEventAsync("ontouchenter", eventArgs);
215225

216226
/// <summary>
217227
/// Raises the <c>@ontouchleave</c> event on <paramref name="element"/>, passing the provided
@@ -242,7 +252,8 @@ public static void TouchLeave(this IElement element, long detail = default, Touc
242252
/// </summary>
243253
/// <param name="element">The element to raise the event on.</param>
244254
/// <param name="eventArgs">The event arguments to pass to the event handler.</param>
245-
public static void TouchLeave(this IElement element, TouchEventArgs eventArgs) => _ = TouchLeaveAsync(element, eventArgs);
255+
public static void TouchLeave(this IElement element, TouchEventArgs eventArgs)
256+
=> _ = TouchLeaveAsync(element, eventArgs);
246257

247258
/// <summary>
248259
/// Raises the <c>@ontouchleave</c> event on <paramref name="element"/>, passing the provided <paramref name="eventArgs"/>
@@ -251,6 +262,7 @@ public static void TouchLeave(this IElement element, long detail = default, Touc
251262
/// <param name="element"></param>
252263
/// <param name="eventArgs"></param>
253264
/// <returns>A task that completes when the event handler is done.</returns>
254-
public static Task TouchLeaveAsync(this IElement element, TouchEventArgs eventArgs) => element.TriggerEventAsync("ontouchleave", eventArgs);
265+
public static Task TouchLeaveAsync(this IElement element, TouchEventArgs eventArgs)
266+
=> element.TriggerEventAsync("ontouchleave", eventArgs);
255267
}
256268
}

tests/EventDispatchExtensions/GeneralEventDispatchExtensionsTest.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
using System.Reflection;
55
using System.Text;
66
using System.Threading.Tasks;
7+
using AngleSharp;
8+
using AngleSharp.Dom;
9+
using Moq;
10+
using Shouldly;
711
using Xunit;
812

913
namespace Egil.RazorComponents.Testing.EventDispatchExtensions
@@ -22,5 +26,38 @@ public async Task CanRaiseEvents(MethodInfo helper)
2226

2327
await VerifyEventRaisesCorrectly(helper, EventArgs.Empty);
2428
}
29+
30+
[Fact(DisplayName = "TriggerEventAsync throws element is null")]
31+
public void Test001()
32+
{
33+
IElement elm = default!;
34+
Should.Throw<ArgumentNullException>(() => elm.TriggerEventAsync("", EventArgs.Empty))
35+
.ParamName.ShouldBe("element");
36+
}
37+
38+
[Fact(DisplayName = "TriggerEventAsync throws if element does not contain an attribute with the blazor event-name")]
39+
public void Test002()
40+
{
41+
var elmMock = new Mock<IElement>();
42+
elmMock.Setup(x => x.GetAttribute(It.IsAny<string>())).Returns(() => null!);
43+
44+
Should.Throw<ArgumentException>(() => elmMock.Object.TriggerEventAsync("click", EventArgs.Empty));
45+
}
46+
47+
[Fact(DisplayName = "TriggerEventAsync throws if element was not rendered through blazor (has a TestRendere in its context)")]
48+
public void Test003()
49+
{
50+
var elmMock = new Mock<IElement>();
51+
var docMock = new Mock<IDocument>();
52+
var ctxMock = new Mock<IBrowsingContext>();
53+
54+
elmMock.Setup(x => x.GetAttribute(It.IsAny<string>())).Returns("1");
55+
elmMock.SetupGet(x => x.Owner).Returns(docMock.Object);
56+
docMock.SetupGet(x => x.Context).Returns(ctxMock.Object);
57+
ctxMock.Setup(x => x.GetService<TestRenderer>()).Returns(() => null!);
58+
59+
Should.Throw<InvalidOperationException>(() => elmMock.Object.TriggerEventAsync("click", EventArgs.Empty));
60+
}
61+
2562
}
2663
}

0 commit comments

Comments
 (0)