From e835c6ccb24c1102e420b0655646990758b7b777 Mon Sep 17 00:00:00 2001 From: Egil Hansen Date: Fri, 15 Nov 2024 16:29:59 +0000 Subject: [PATCH] fix: support passing assigned render mode via parameter builder (WIP) --- .../ComponentParameterCollectionBuilder.cs | 23 +++++++++----- .../Rendering/RenderModeTests.razor | 30 +++++++++++++++++++ 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/bunit.core/ComponentParameterCollectionBuilder.cs b/src/bunit.core/ComponentParameterCollectionBuilder.cs index f4c1fa04e..7014e3cbe 100644 --- a/src/bunit.core/ComponentParameterCollectionBuilder.cs +++ b/src/bunit.core/ComponentParameterCollectionBuilder.cs @@ -78,7 +78,7 @@ public ComponentParameterCollectionBuilder Add(Expr /// A lambda function that selects the parameter. /// The markup string to pass to the . /// This . - public ComponentParameterCollectionBuilder Add(Expression> parameterSelector, [StringSyntax("Html")]string markup) + public ComponentParameterCollectionBuilder Add(Expression> parameterSelector, [StringSyntax("Html")] string markup) => Add(parameterSelector, markup.ToMarkupRenderFragment()); /// @@ -266,7 +266,7 @@ public ComponentParameterCollectionBuilder AddChildContent(RenderFra /// /// The markup string to pass the ChildContent parameter wrapped in a . /// This . - public ComponentParameterCollectionBuilder AddChildContent([StringSyntax("Html")]string markup) + public ComponentParameterCollectionBuilder AddChildContent([StringSyntax("Html")] string markup) => AddChildContent(markup.ToMarkupRenderFragment()); /// @@ -344,11 +344,11 @@ public ComponentParameterCollectionBuilder Bind( Action changedAction, Expression>? valueExpression = null) { - #if !NET8_0_OR_GREATER +#if !NET8_0_OR_GREATER var (parameterName, _, isCascading) = GetParameterInfo(parameterSelector); - #else +#else var (parameterName, _, isCascading) = GetParameterInfo(parameterSelector, initialValue); - #endif +#endif if (isCascading) throw new ArgumentException("Using Bind with a cascading parameter is not allowed.", parameterName); @@ -397,6 +397,13 @@ static string TrimEnd(string source, string value) : source; } +#if NET9_0_OR_GREATER + public ComponentParameterCollectionBuilder SetAssignedRenderMode(IComponentRenderMode? renderMode) + { + return this; + } +#endif + /// /// Try to add a for a parameter with the , if /// has a property with that name, AND that property has a @@ -454,14 +461,14 @@ Expression> parameterSelector : propInfoCandidate; var paramAttr = propertyInfo?.GetCustomAttribute(inherit: true); - #if !NET8_0_OR_GREATER +#if !NET8_0_OR_GREATER var cascadingParamAttr = propertyInfo?.GetCustomAttribute(inherit: true); if (propertyInfo is null || (paramAttr is null && cascadingParamAttr is null)) 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)); return (propertyInfo.Name, CascadingValueName: cascadingParamAttr?.Name, IsCascading: cascadingParamAttr is not null); - #else +#else var cascadingParamAttrBase = propertyInfo?.GetCustomAttribute(inherit: true); if (propertyInfo is null || (paramAttr is null && cascadingParamAttrBase is null)) @@ -494,7 +501,7 @@ static ArgumentException CreateErrorMessageForSupplyFromQuery( NavigationManager.NavigateTo(uri); """); } - #endif +#endif } private static bool HasChildContentParameter() diff --git a/tests/bunit.core.tests/Rendering/RenderModeTests.razor b/tests/bunit.core.tests/Rendering/RenderModeTests.razor index d9a0fb514..db2701193 100644 --- a/tests/bunit.core.tests/Rendering/RenderModeTests.razor +++ b/tests/bunit.core.tests/Rendering/RenderModeTests.razor @@ -125,6 +125,36 @@ ); } + [Fact(DisplayName = "SetAssignedRenderMode on root component")] + public void Test011() + { + var cut = RenderComponent(ps => ps.SetAssignedRenderMode(RenderMode.InteractiveServer)); + cut.MarkupMatches(@

Assigned Render Mode: InteractiveServerRenderMode

); + } + + [Fact(DisplayName = "SetAssignedRenderMode on parent component cascades to children")] + public void Test012() + { + var cut = RenderComponent(ps => ps + .SetAssignedRenderMode(RenderMode.InteractiveWebAssembly) + .AddChildContent()); + + cut.MarkupMatches(@

Assigned Render Mode: InteractiveWebAssemblyRenderMode

); + } + + [Fact(DisplayName = "SetAssignedRenderMode child components")] + public void Test013() + { + var cut = RenderComponent(ps => ps + .AddChildContent(pps => pps.SetAssignedRenderMode(RenderMode.InteractiveServer)) + .AddChildContent(pps => pps.SetAssignedRenderMode(RenderMode.InteractiveWebAssembly))); + + cut.MarkupMatches( + @ +

Assigned Render Mode: InteractiveServerRenderMode

+

Assigned Render Mode: InteractiveWebAssemblyRenderMode

+
); + } [Fact(DisplayName = "Different assigned RenderMode between child and parent throws")] public void Test020()