Skip to content

Commit

Permalink
fix: support passing assigned render mode via parameter builder (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
egil committed Nov 15, 2024
1 parent f44ac45 commit e835c6c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/bunit.core/ComponentParameterCollectionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public ComponentParameterCollectionBuilder<TComponent> Add<TChildComponent>(Expr
/// <param name="parameterSelector">A lambda function that selects the parameter.</param>
/// <param name="markup">The markup string to pass to the <see cref="RenderFragment"/>.</param>
/// <returns>This <see cref="ComponentParameterCollectionBuilder{TComponent}"/>.</returns>
public ComponentParameterCollectionBuilder<TComponent> Add(Expression<Func<TComponent, RenderFragment?>> parameterSelector, [StringSyntax("Html")]string markup)
public ComponentParameterCollectionBuilder<TComponent> Add(Expression<Func<TComponent, RenderFragment?>> parameterSelector, [StringSyntax("Html")] string markup)
=> Add(parameterSelector, markup.ToMarkupRenderFragment());

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

/// <summary>
Expand Down Expand Up @@ -344,11 +344,11 @@ public ComponentParameterCollectionBuilder<TComponent> Bind<TValue>(
Action<TValue> changedAction,
Expression<Func<TValue>>? 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);
Expand Down Expand Up @@ -397,6 +397,13 @@ static string TrimEnd(string source, string value)
: source;
}

#if NET9_0_OR_GREATER
public ComponentParameterCollectionBuilder<TComponent> SetAssignedRenderMode(IComponentRenderMode? renderMode)

Check failure on line 401 in src/bunit.core/ComponentParameterCollectionBuilder.cs

View workflow job for this annotation

GitHub Actions / run-test (windows-latest)

Missing XML comment for publicly visible type or member 'ComponentParameterCollectionBuilder<TComponent>.SetAssignedRenderMode(IComponentRenderMode?)'
{
return this;
}
#endif

/// <summary>
/// Try to add a <paramref name="value"/> for a parameter with the <paramref name="name"/>, if
/// <typeparamref name="TComponent"/> has a property with that name, AND that property has a <see cref="ParameterAttribute"/>
Expand Down Expand Up @@ -454,14 +461,14 @@ Expression<Func<TComponent, TValue>> parameterSelector
: propInfoCandidate;

var paramAttr = propertyInfo?.GetCustomAttribute<ParameterAttribute>(inherit: true);
#if !NET8_0_OR_GREATER
#if !NET8_0_OR_GREATER
var cascadingParamAttr = propertyInfo?.GetCustomAttribute<CascadingParameterAttribute>(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<CascadingParameterAttributeBase>(inherit: true);

if (propertyInfo is null || (paramAttr is null && cascadingParamAttrBase is null))
Expand Down Expand Up @@ -494,7 +501,7 @@ static ArgumentException CreateErrorMessageForSupplyFromQuery(
NavigationManager.NavigateTo(uri);
""");
}
#endif
#endif
}

private static bool HasChildContentParameter()
Expand Down
30 changes: 30 additions & 0 deletions tests/bunit.core.tests/Rendering/RenderModeTests.razor
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,36 @@
</text>);
}

[Fact(DisplayName = "SetAssignedRenderMode on root component")]
public void Test011()
{
var cut = RenderComponent<ComponentThatPrintsAssignedRenderMode>(ps => ps.SetAssignedRenderMode(RenderMode.InteractiveServer));
cut.MarkupMatches(@<p>Assigned Render Mode: InteractiveServerRenderMode</p>);
}

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

cut.MarkupMatches(@<p>Assigned Render Mode: InteractiveWebAssemblyRenderMode</p>);
}

[Fact(DisplayName = "SetAssignedRenderMode child components")]
public void Test013()
{
var cut = RenderComponent<ComponentWithChildContent>(ps => ps
.AddChildContent<ComponentThatPrintsAssignedRenderMode>(pps => pps.SetAssignedRenderMode(RenderMode.InteractiveServer))
.AddChildContent<ComponentThatPrintsAssignedRenderMode>(pps => pps.SetAssignedRenderMode(RenderMode.InteractiveWebAssembly)));

cut.MarkupMatches(
@<text>
<p>Assigned Render Mode: InteractiveServerRenderMode</p>
<p>Assigned Render Mode: InteractiveWebAssemblyRenderMode</p>
</text>);
}

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

0 comments on commit e835c6c

Please sign in to comment.