Skip to content

Commit f1c95e2

Browse files
committed
Tweaks to Virtualize handler to make it work with Placeholders, ItemProviders
1 parent 625e2d8 commit f1c95e2

File tree

8 files changed

+156
-559
lines changed

8 files changed

+156
-559
lines changed

src/bunit.web/JSInterop/BunitJSInteropExtensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#if NET5_0
2+
using Bunit.JSInterop.InvocationHandlers;
3+
#endif
4+
15
namespace Bunit.JSInterop
26
{
37
/// <summary>
@@ -11,7 +15,8 @@ public static class BunitJSInteropExtensions
1115
public static BunitJSInterop AddBuiltInJSRuntimeInvocationHandlers(this BunitJSInterop jsInterop)
1216
{
1317
#if NET5_0
14-
jsInterop.AddInvocationHandler(new InvocationHandlers.FocusAsyncInvocationHandler());
18+
jsInterop.AddInvocationHandler(new FocusAsyncInvocationHandler());
19+
jsInterop.AddInvocationHandler(new VirtualizeJSRuntimeInvocationHandler());
1520
#endif
1621
return jsInterop;
1722
}

src/bunit.web/JSInterop/ComponentSupport/BunitJSInteropExtensions.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/bunit.web/JSInterop/ComponentSupport/Virtualize/VirtualizeJSRuntimeInvocationHandler.cs renamed to src/bunit.web/JSInterop/InvocationHandlers/VirtualizeJSRuntimeInvocationHandler.cs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,32 @@
55
using Microsoft.AspNetCore.Components.Web.Virtualization;
66
using Microsoft.JSInterop;
77

8-
namespace Bunit.JSInterop.ComponentSupport.Virtualize
8+
namespace Bunit.JSInterop.InvocationHandlers
99
{
1010
/// <summary>
1111
/// Represents an JSInterop handler for the <see cref="Virtualize{TItem}"/> component.
1212
/// </summary>
1313
public class VirtualizeJSRuntimeInvocationHandler : JSRuntimeInvocationHandler
1414
{
1515
private const string JsFunctionsPrefix = "Blazor._internal.Virtualize.";
16-
private static readonly Type VirtualizeJsInteropType;
17-
private static readonly Type DotNetObjectReferenceVirtualizeJsInteropType;
18-
private static readonly PropertyInfo DotNetObjectReferenceValuePropertyInfo;
19-
private static readonly MethodInfo OnSpacerBeforeVisibleMethodInfo;
20-
21-
static VirtualizeJSRuntimeInvocationHandler()
16+
private static readonly Lazy<(PropertyInfo, MethodInfo)> VirtualizeReflection = new Lazy<(PropertyInfo, MethodInfo)>(() =>
2217
{
23-
// Get <Virtualize> types needed to emulate the <Virtualize>'s JavaScript
24-
VirtualizeJsInteropType = typeof(Virtualize<>)
18+
var VirtualizeJsInteropType = typeof(Virtualize<>)
2519
.Assembly
2620
.GetType("Microsoft.AspNetCore.Components.Web.Virtualization.VirtualizeJsInterop")
27-
?? throw new InvalidOperationException("Did not find the VirtualizeJsInterop in the expected namespace/assembly.");
28-
DotNetObjectReferenceVirtualizeJsInteropType = typeof(DotNetObjectReference<>).MakeGenericType(VirtualizeJsInteropType);
29-
DotNetObjectReferenceValuePropertyInfo = DotNetObjectReferenceVirtualizeJsInteropType
21+
?? throw new InvalidOperationException("Did not find the VirtualizeJsInterop in the expected namespace/assembly.");
22+
23+
var DotNetObjectReferenceVirtualizeJsInteropType = typeof(DotNetObjectReference<>).MakeGenericType(VirtualizeJsInteropType);
24+
25+
var dotNetObjectReferenceValuePropertyInfo = DotNetObjectReferenceVirtualizeJsInteropType
3026
.GetProperty("Value", BindingFlags.Public | BindingFlags.Instance)
3127
?? throw new InvalidOperationException("Did not find the Value property on the DotNetObjectReference<VirtualizeJsInterop> type.");
32-
OnSpacerBeforeVisibleMethodInfo = VirtualizeJsInteropType?.GetMethod("OnSpacerBeforeVisible")
28+
29+
var onSpacerBeforeVisibleMethodInfo = VirtualizeJsInteropType?.GetMethod("OnSpacerBeforeVisible")
3330
?? throw new InvalidOperationException("Did not find the OnSpacerBeforeVisible method on the VirtualizeJsInterop type.");
34-
}
31+
32+
return (dotNetObjectReferenceValuePropertyInfo, onSpacerBeforeVisibleMethodInfo);
33+
});
3534

3635
internal VirtualizeJSRuntimeInvocationHandler()
3736
: base(CatchAllIdentifier, i => i.Identifier.StartsWith(JsFunctionsPrefix, StringComparison.Ordinal))
@@ -48,23 +47,22 @@ protected override void OnInvocation(JSRuntimeInvocation invocation)
4847
Debug.Assert(invocation.Arguments.Count == 3, "Received an unexpected amount of arguments from the <Virtualize> component.");
4948
Debug.Assert(invocation.Arguments[0] is not null, "Received an unexpected null argument, expected an DotNetObjectReference<VirtualizeJsInterop> instance.");
5049

51-
var onSpacerBeforeVisible = GetOnSpacerBeforeVisibleCallback(invocation.Arguments[0]!);
52-
53-
onSpacerBeforeVisible(
54-
0f /* spacerSize */,
55-
0f /* spacerSeparation */,
56-
10_000_000f /* containerSize - very large number to ensure all items are loaded at once */
57-
);
50+
InvokeOnSpacerBeforeVisible(invocation.Arguments[0]!);
5851

5952
SetVoidResult();
6053
}
6154

62-
private static Action<float, float, float> GetOnSpacerBeforeVisibleCallback(object dotNetObjectReference)
55+
private static void InvokeOnSpacerBeforeVisible(object dotNetObjectReference)
6356
{
64-
var virtualizeJsInterop = DotNetObjectReferenceValuePropertyInfo?.GetValue(dotNetObjectReference);
65-
66-
return (spacerSize, spacerSeparation, containerSize)
67-
=> OnSpacerBeforeVisibleMethodInfo.Invoke(virtualizeJsInterop, new object[] { spacerSize, spacerSeparation, containerSize });
57+
var (dotNetObjectReferenceValuePropertyInfo, onSpacerBeforeVisibleMethodInfo) = VirtualizeReflection.Value;
58+
var virtualizeJsInterop = dotNetObjectReferenceValuePropertyInfo.GetValue(dotNetObjectReference);
59+
onSpacerBeforeVisibleMethodInfo.Invoke(
60+
virtualizeJsInterop,
61+
new object[] {
62+
0f /* spacerSize */,
63+
0f /* spacerSeparation */,
64+
1_000_000_000f /* containerSize - very large number to ensure all items are loaded at once */
65+
});
6866
}
6967
}
7068
}

src/bunit.web/TestContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public virtual IRenderedFragment Render(RenderFragment renderFragment)
7171
=> this.RenderInsideRenderTree(renderFragment);
7272

7373
/// <summary>
74-
/// Dummy method required to allow Blazors compiler to generate
74+
/// Dummy method required to allow Blazor's compiler to generate
7575
/// C# from .razor files.
7676
/// </summary>
7777
protected virtual void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder builder) { }

src/bunit.web/bunit.web.csproj

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<RazorLangVersion>3.0</RazorLangVersion>
@@ -17,12 +17,6 @@
1717
</Description>
1818
</PropertyGroup>
1919

20-
<ItemGroup>
21-
<Compile Remove="TestDoubles\Logging\**" />
22-
<EmbeddedResource Remove="TestDoubles\Logging\**" />
23-
<None Remove="TestDoubles\Logging\**" />
24-
</ItemGroup>
25-
2620
<ItemGroup>
2721
<PackageReference Include="AngleSharp" Version="0.14.0" />
2822
<PackageReference Include="AngleSharp.Css" Version="0.14.2" />

0 commit comments

Comments
 (0)