Skip to content

Commit 62c724c

Browse files
committed
WIP _ PLEASE SQUASH ME!
1 parent 1514e0c commit 62c724c

File tree

5 files changed

+98
-15
lines changed

5 files changed

+98
-15
lines changed

src/bunit.core/Rendering/TestRenderer.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,29 @@ protected override IComponent ResolveComponentForRenderMode(Type componentType,
255255
return renderModeAttribute.Mode;
256256
}
257257

258-
var parentComponentState = GetComponentState(component).ParentComponentState;
258+
var componentState = GetComponentState(component);
259+
// Should this be LogicalComponentState instead?
260+
if (componentState.ParentComponentState is not null)
261+
{
262+
var parentFrames = GetCurrentRenderTreeFrames(componentState.ParentComponentState.ComponentId);
263+
var foundComponentStart = false;
264+
for (var i = 0; i < parentFrames.Count; i++)
265+
{
266+
ref var frame = ref parentFrames.Array[i];
267+
268+
if (frame.FrameType == RenderTreeFrameType.Component)
269+
{
270+
foundComponentStart = frame.ComponentId == componentState.ComponentId;
271+
}
272+
else if (foundComponentStart
273+
&& frame.FrameType == RenderTreeFrameType.ComponentRenderMode)
274+
{
275+
return frame.ComponentRenderMode;
276+
}
277+
}
278+
}
279+
280+
var parentComponentState = componentState.ParentComponentState;
259281
return parentComponentState is not null
260282
? GetComponentRenderMode(parentComponentState.Component)
261283
: null;

tests/bunit.core.tests/Rendering/RenderModeTests.cs renamed to tests/bunit.core.tests/Rendering/RenderModeTests.razor

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1+
@{
12
#if NET9_0_OR_GREATER
2-
using Bunit.TestAssets.RenderModes;
3+
}
34

4-
namespace Bunit.Rendering;
5+
@using Bunit.TestAssets.RenderModes;
6+
@inherits TestContext
57

6-
public class RenderModeTests : TestContext
7-
{
8+
@code {
9+
#if NET9_0_OR_GREATER
810
[Fact(DisplayName = "TestRenderer provides RendererInfo")]
911
public void Test001()
1012
{
1113
Renderer.SetRendererInfo(new RendererInfo("Server", true));
1214
var cut = RenderComponent<RendererInfoComponent>();
1315

14-
cut.MarkupMatches("""
16+
cut.MarkupMatches(@"
1517
<p>Is interactive: True</p>
1618
<p>Rendermode: Server</p>
17-
""");
19+
");
1820
}
1921

2022
[Fact(DisplayName = "Renderer throws exception if RendererInfo is not specified")]
@@ -39,10 +41,10 @@ public void Test004()
3941
var cut = RenderComponent<ComponentWithoutRenderMode>(
4042
c => c.AddChildContent<ComponentWithWebAssemblyRenderMode>());
4143

42-
cut.MarkupMatches("""
44+
cut.MarkupMatches(@"
4345
<div>Parent assigned render mode: </div>
4446
<div>Assigned render mode: InteractiveWebAssemblyRenderMode</div>
45-
""");
47+
");
4648
}
4749

4850
[Fact(DisplayName = "Parent and child render mode is specified")]
@@ -51,10 +53,10 @@ public void Test005()
5153
var cut = RenderComponent<ComponentWithWebAssemblyRenderMode>(
5254
c => c.AddChildContent<ComponentWithServerRenderMode>());
5355

54-
cut.MarkupMatches("""
56+
cut.MarkupMatches(@"
5557
<div>Parent assigned render mode: InteractiveWebAssemblyRenderMode</div>
5658
<div>Assigned render mode: InteractiveServerRenderMode</div>
57-
""");
59+
");
5860
}
5961

6062
[Fact(DisplayName = "Parent and child render mode is not specified")]
@@ -63,11 +65,44 @@ public void Test006()
6365
var cut = RenderComponent<ComponentWithoutRenderMode>(
6466
c => c.AddChildContent<ComponentWithoutRenderMode>());
6567

66-
cut.MarkupMatches("""
68+
cut.MarkupMatches(@"
6769
<div>Parent assigned render mode: </div>
6870
<div>Assigned render mode: </div>
69-
""");
70-
71+
");
72+
}
73+
74+
[Fact(DisplayName = "Parent that sets render mode to children is specified on child")]
75+
public void Test007()
76+
{
77+
var cut = RenderComponent<ComponentWithChildContent>(
78+
p => p.AddChildContent(
79+
@<ComponentThatPrintsAssignedRenderMode @rendermode="RenderMode.InteractiveServer"/>));
80+
81+
cut.MarkupMatches(@<p>Assigned Render Mode: InteractiveServerRenderMode</p>);
82+
}
83+
84+
[Fact(DisplayName = "Assigned Render Mode is inherited all the way down the component hierarchy")]
85+
public void Test008()
86+
{
87+
var cut = RenderComponent<ComponentWithChildContent>(
88+
p => p.AddChildContent(
89+
@<ComponentWithChildContent @rendermode="RenderMode.InteractiveServer">
90+
<ComponentThatPrintsAssignedRenderMode/>
91+
</ComponentWithChildContent>));
92+
93+
cut.MarkupMatches(@<p>Assigned Render Mode: InteractiveServerRenderMode</p>);
94+
}
95+
96+
[Fact(DisplayName = "Having a component with section outlet and RenderMode is specifing for child component")]
97+
public void Test009()
98+
{
99+
// See: https://learn.microsoft.com/en-us/aspnet/core/blazor/components/sections?view=aspnetcore-8.0#section-interaction-with-other-blazor-features
100+
var cut = Render(@<SectionOutletComponent/>);
101+
102+
cut.MarkupMatches(@<p>Assigned Render Mode: InteractiveWebAssemblyRenderMode</p>);
71103
}
104+
#endif
72105
}
73-
#endif
106+
@{
107+
#endif
108+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@{
2+
#if NET9_0_OR_GREATER
3+
}
4+
5+
<p>Assigned Render Mode: @AssignedRenderMode?.GetType().Name</p>
6+
7+
@{
8+
#endif
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@ChildContent
2+
@code {
3+
4+
[Parameter] public RenderFragment ChildContent { get; set; } = default!;
5+
6+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@{
2+
#if NET9_0_OR_GREATER
3+
}
4+
5+
<Microsoft.AspNetCore.Components.Sections.SectionOutlet SectionId="1" @rendermode="RenderMode.InteractiveWebAssembly" />
6+
<Microsoft.AspNetCore.Components.Sections.SectionContent SectionId="1">
7+
<ComponentThatPrintsAssignedRenderMode/>
8+
</Microsoft.AspNetCore.Components.Sections.SectionContent>
9+
@{
10+
#endif
11+
}

0 commit comments

Comments
 (0)