Skip to content

Commit b5c8029

Browse files
committed
fix: support passing assigned render mode via parameter builder
1 parent f44ac45 commit b5c8029

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

src/bunit.core/ComponentParameterCollectionBuilder.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public ComponentParameterCollectionBuilder<TComponent> Add<TChildComponent>(Expr
7878
/// <param name="parameterSelector">A lambda function that selects the parameter.</param>
7979
/// <param name="markup">The markup string to pass to the <see cref="RenderFragment"/>.</param>
8080
/// <returns>This <see cref="ComponentParameterCollectionBuilder{TComponent}"/>.</returns>
81-
public ComponentParameterCollectionBuilder<TComponent> Add(Expression<Func<TComponent, RenderFragment?>> parameterSelector, [StringSyntax("Html")]string markup)
81+
public ComponentParameterCollectionBuilder<TComponent> Add(Expression<Func<TComponent, RenderFragment?>> parameterSelector, [StringSyntax("Html")] string markup)
8282
=> Add(parameterSelector, markup.ToMarkupRenderFragment());
8383

8484
/// <summary>
@@ -266,7 +266,7 @@ public ComponentParameterCollectionBuilder<TComponent> AddChildContent(RenderFra
266266
/// </summary>
267267
/// <param name="markup">The markup string to pass the ChildContent parameter wrapped in a <see cref="RenderFragment"/>.</param>
268268
/// <returns>This <see cref="ComponentParameterCollectionBuilder{TComponent}"/>.</returns>
269-
public ComponentParameterCollectionBuilder<TComponent> AddChildContent([StringSyntax("Html")]string markup)
269+
public ComponentParameterCollectionBuilder<TComponent> AddChildContent([StringSyntax("Html")] string markup)
270270
=> AddChildContent(markup.ToMarkupRenderFragment());
271271

272272
/// <summary>
@@ -344,11 +344,11 @@ public ComponentParameterCollectionBuilder<TComponent> Bind<TValue>(
344344
Action<TValue> changedAction,
345345
Expression<Func<TValue>>? valueExpression = null)
346346
{
347-
#if !NET8_0_OR_GREATER
347+
#if !NET8_0_OR_GREATER
348348
var (parameterName, _, isCascading) = GetParameterInfo(parameterSelector);
349-
#else
349+
#else
350350
var (parameterName, _, isCascading) = GetParameterInfo(parameterSelector, initialValue);
351-
#endif
351+
#endif
352352

353353
if (isCascading)
354354
throw new ArgumentException("Using Bind with a cascading parameter is not allowed.", parameterName);
@@ -397,6 +397,13 @@ static string TrimEnd(string source, string value)
397397
: source;
398398
}
399399

400+
#if NET9_0_OR_GREATER
401+
public ComponentParameterCollectionBuilder<TComponent> SetAssignedRenderMode(IComponentRenderMode? renderMode)
402+
{
403+
return this;
404+
}
405+
#endif
406+
400407
/// <summary>
401408
/// Try to add a <paramref name="value"/> for a parameter with the <paramref name="name"/>, if
402409
/// <typeparamref name="TComponent"/> has a property with that name, AND that property has a <see cref="ParameterAttribute"/>
@@ -454,14 +461,14 @@ Expression<Func<TComponent, TValue>> parameterSelector
454461
: propInfoCandidate;
455462

456463
var paramAttr = propertyInfo?.GetCustomAttribute<ParameterAttribute>(inherit: true);
457-
#if !NET8_0_OR_GREATER
464+
#if !NET8_0_OR_GREATER
458465
var cascadingParamAttr = propertyInfo?.GetCustomAttribute<CascadingParameterAttribute>(inherit: true);
459466

460467
if (propertyInfo is null || (paramAttr is null && cascadingParamAttr is null))
461468
throw new ArgumentException($"The parameter selector '{parameterSelector}' does not resolve to a public property on the component '{typeof(TComponent)}' with a [Parameter] or [CascadingParameter] attribute.", nameof(parameterSelector));
462469

463470
return (propertyInfo.Name, CascadingValueName: cascadingParamAttr?.Name, IsCascading: cascadingParamAttr is not null);
464-
#else
471+
#else
465472
var cascadingParamAttrBase = propertyInfo?.GetCustomAttribute<CascadingParameterAttributeBase>(inherit: true);
466473

467474
if (propertyInfo is null || (paramAttr is null && cascadingParamAttrBase is null))
@@ -494,7 +501,7 @@ static ArgumentException CreateErrorMessageForSupplyFromQuery(
494501
NavigationManager.NavigateTo(uri);
495502
""");
496503
}
497-
#endif
504+
#endif
498505
}
499506

500507
private static bool HasChildContentParameter()

tests/bunit.core.tests/Rendering/RenderModeTests.razor

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,36 @@
125125
</text>);
126126
}
127127

128+
[Fact(DisplayName = "SetAssignedRenderMode on root component")]
129+
public void Test011()
130+
{
131+
var cut = RenderComponent<ComponentThatPrintsAssignedRenderMode>(ps => ps.SetAssignedRenderMode(RenderMode.InteractiveServer));
132+
cut.MarkupMatches(@<p>Assigned Render Mode: InteractiveServerRenderMode</p>);
133+
}
134+
135+
[Fact(DisplayName = "SetAssignedRenderMode on parent component cascades to children")]
136+
public void Test012()
137+
{
138+
var cut = RenderComponent<ComponentWithChildContent>(ps => ps
139+
.SetAssignedRenderMode(RenderMode.InteractiveWebAssembly)
140+
.AddChildContent<ComponentThatPrintsAssignedRenderMode>());
141+
142+
cut.MarkupMatches(@<p>Assigned Render Mode: InteractiveWebAssemblyRenderMode</p>);
143+
}
144+
145+
[Fact(DisplayName = "SetAssignedRenderMode child components")]
146+
public void Test013()
147+
{
148+
var cut = RenderComponent<ComponentWithChildContent>(ps => ps
149+
.AddChildContent<ComponentThatPrintsAssignedRenderMode>(pps => pps.SetAssignedRenderMode(RenderMode.InteractiveServer))
150+
.AddChildContent<ComponentThatPrintsAssignedRenderMode>(pps => pps.SetAssignedRenderMode(RenderMode.InteractiveWebAssembly)));
151+
152+
cut.MarkupMatches(
153+
@<text>
154+
<p>Assigned Render Mode: InteractiveServerRenderMode</p>
155+
<p>Assigned Render Mode: InteractiveWebAssemblyRenderMode</p>
156+
</text>);
157+
}
128158

129159
[Fact(DisplayName = "Different assigned RenderMode between child and parent throws")]
130160
public void Test020()

0 commit comments

Comments
 (0)