Skip to content

Commit b8624d0

Browse files
anddrzejbegil
andauthored
Add SetVoidResult to Focus handlers with tests (#524)
* add SetVoidResult to Focus hanlders with tests * Update CHANGELOG.md Co-authored-by: Egil Hansen <egil@assimilated.dk>
1 parent f0cbe08 commit b8624d0

5 files changed

Lines changed: 43 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ List of added functionality in this release.
3535

3636
- The `Click` and `DoubleClick` extension methods now set the `MouseEventArgs.Detail` property to `1` and `2` respectively by default, unless the user specifies something else. This makes the methods more correctly emulate how Blazor reports single or double clicks on an element in the browser. Thanks to [@David-Moreira](https://github.com/David-Moreira) for the help troubleshooting this issue. By [@egil](https://github.com/egil).
3737

38+
- `FocusAsync()` method handler on `ElementReference` and `<FocusOnNavigate>` js handler return completed `Task`. By [@anddrzejb](https://github.com/anddrzejb).
39+
3840
## [1.2.49] - 2021-08-09
3941

4042
### Added

src/bunit.web/JSInterop/InvocationHandlers/Implementation/FocusAsyncInvocationHandler.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
#if NET5_0_OR_GREATER
23
using System;
34
using Microsoft.AspNetCore.Components;
@@ -21,7 +22,9 @@ internal sealed class FocusAsyncInvocationHandler : JSRuntimeInvocationHandler
2122
/// </summary>
2223
internal FocusAsyncInvocationHandler()
2324
: base(inv => inv.Identifier.Equals(FocusIdentifier, StringComparison.Ordinal), isCatchAllHandler: false)
24-
{ }
25+
{
26+
SetVoidResult();
27+
}
2528
}
2629
}
2730
#endif

src/bunit.web/JSInterop/InvocationHandlers/Implementation/FocusOnNavigateHandler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ internal sealed class FocusOnNavigateHandler : JSRuntimeInvocationHandler
2020
/// </summary>
2121
internal FocusOnNavigateHandler()
2222
: base(inv => inv.Identifier.Equals(Identifier, StringComparison.Ordinal), isCatchAllHandler: true)
23-
{ }
23+
{
24+
SetVoidResult();
25+
}
2426
}
2527
}
2628
#endif

tests/bunit.web.tests/JSInterop/InvocationHandlers/FocusAsyncInvocationHandlerTest.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,23 @@ public void Test002()
3131
invocations[1].Arguments[0].ShouldBeElementReferenceTo(inputs[1]);
3232
}
3333

34+
[Fact(DisplayName = "Will return completed task")]
35+
public void Test003()
36+
{
37+
var cut = RenderComponent<FocusingComponent>();
38+
Assert.True(cut.Instance.AfterFirstRender);
39+
}
40+
3441
private class FocusingComponent : ComponentBase
3542
{
3643
private ElementReference elmRef;
44+
internal bool AfterFirstRender { get; private set; }
3745
protected override async Task OnAfterRenderAsync(bool firstRender)
3846
{
3947
if (firstRender)
4048
{
4149
await elmRef.FocusAsync();
50+
AfterFirstRender = true;
4251
}
4352
}
4453

tests/bunit.web.tests/JSInterop/InvocationHandlers/FocusOnNavigateHandlerTest.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,31 @@ public void Test001()
3030
.Arguments[0]
3131
.ShouldBe(focusOnNavigateComponent.Instance.Selector);
3232
}
33+
34+
[Fact(DisplayName = "Will return completed task")]
35+
public void Test002()
36+
{
37+
var cut = RenderComponent<App>(ps => ps
38+
.Add<FocusOnNavigateInternal, RouteData>(p => p.FoundTemplate, routeData => cps => cps
39+
.Add(x => x.RouteData, routeData)
40+
.Add(x => x.Selector, "h1")));
41+
42+
var focusOnNavigateComponent = cut.FindComponent<FocusOnNavigateInternal>();
43+
Assert.True(focusOnNavigateComponent.Instance.AfterFirstRender);
44+
}
45+
46+
private class FocusOnNavigateInternal : FocusOnNavigate
47+
{
48+
internal bool AfterFirstRender { get; private set; }
49+
protected override async Task OnAfterRenderAsync(bool firstRender)
50+
{
51+
await base.OnAfterRenderAsync(firstRender);
52+
if (firstRender)
53+
{
54+
AfterFirstRender = true;
55+
}
56+
}
57+
}
3358
}
3459
}
3560
#endif

0 commit comments

Comments
 (0)