-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support finding render modes specified via @rendermode directive
- Loading branch information
Showing
8 changed files
with
238 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#if NET9_0_OR_GREATER | ||
namespace Bunit.Rendering; | ||
|
||
/// <summary> | ||
/// Represents an exception that is thrown when a component under test has mismatching render modes assigned between parent and child components. | ||
/// </summary> | ||
public sealed class RenderModeMisMatchException : Exception | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MissingRendererInfoException"/> class. | ||
/// </summary> | ||
public RenderModeMisMatchException() | ||
: base(""" | ||
A component under test has mismatching render modes assigned between parent and child components. | ||
Ensure that the render mode of the parent component matches the render mode of the child component. | ||
Learn more about render modes at https://learn.microsoft.com/en-us/aspnet/core/blazor/components/render-modes?view=aspnetcore-9.0#render-mode-propagation. | ||
""") | ||
{ | ||
HelpLink = "https://learn.microsoft.com/en-us/aspnet/core/blazor/components/render-modes?view=aspnetcore-9.0#render-mode-propagation"; | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
@using Bunit.TestAssets.RenderModes; | ||
@inherits TestContext | ||
@code { | ||
#if NET9_0_OR_GREATER | ||
[Fact(DisplayName = "TestRenderer provides RendererInfo")] | ||
public void Test001() | ||
{ | ||
Renderer.SetRendererInfo(new RendererInfo("Server", true)); | ||
var cut = RenderComponent<RendererInfoComponent>(); | ||
|
||
cut.MarkupMatches( | ||
@<text> | ||
<p>Is interactive: True</p> | ||
<p>Rendermode: Server</p> | ||
</text>); | ||
} | ||
|
||
[Fact(DisplayName = "Renderer throws exception if RendererInfo is not specified")] | ||
public void Test002() | ||
{ | ||
Action act = () => RenderComponent<RendererInfoComponent>(); | ||
|
||
act.ShouldThrow<MissingRendererInfoException>(); | ||
} | ||
|
||
[Fact(DisplayName = "Renderer should set the RenderModeAttribute on the component")] | ||
public void Test003() | ||
{ | ||
var cut = RenderComponent<ComponentWithServerRenderMode>(); | ||
|
||
cut.MarkupMatches(@<div>Assigned render mode: InteractiveServerRenderMode</div>); | ||
} | ||
|
||
[Fact(DisplayName = "The AssignedRenderMode is based on the RenderModeAttribute in the component hierarchy where parent component has no RenderMode")] | ||
public void Test004() | ||
{ | ||
var cut = Render( | ||
@<ComponentWithoutRenderMode> | ||
<ComponentWithWebAssemblyRenderMode /> | ||
</ComponentWithoutRenderMode>); | ||
|
||
cut.MarkupMatches( | ||
@<text> | ||
<div>Parent assigned render mode: </div> | ||
<div>Assigned render mode: InteractiveWebAssemblyRenderMode</div> | ||
</text>); | ||
} | ||
|
||
[Fact(DisplayName = "Parent and child render mode is specified")] | ||
public void Test005() | ||
{ | ||
var cut = Render( | ||
@<ComponentWithServerRenderMode> | ||
<ComponentWithServerRenderMode /> | ||
</ComponentWithServerRenderMode>); | ||
|
||
cut.MarkupMatches( | ||
@<text> | ||
<div>Parent assigned render mode: InteractiveServerRenderMode</div> | ||
<div>Assigned render mode: InteractiveServerRenderMode</div> | ||
</text>); | ||
} | ||
|
||
[Fact(DisplayName = "Parent and child render mode is not specified")] | ||
public void Test006() | ||
{ | ||
var cut = Render( | ||
@<ComponentWithoutRenderMode> | ||
<ComponentWithoutRenderMode /> | ||
</ComponentWithoutRenderMode>); | ||
|
||
cut.MarkupMatches( | ||
@<text> | ||
<div>Parent assigned render mode: </div> | ||
<div>Assigned render mode: </div> | ||
</text>); | ||
} | ||
|
||
[Fact(DisplayName = "Rendermode specified on child")] | ||
public void Test007() | ||
{ | ||
var cut = Render( | ||
@<ComponentWithChildContent> | ||
<ComponentThatPrintsAssignedRenderMode @rendermode="RenderMode.InteractiveServer" /> | ||
</ComponentWithChildContent>); | ||
|
||
cut.MarkupMatches(@<p>Assigned Render Mode: InteractiveServerRenderMode</p>); | ||
} | ||
|
||
[Fact(DisplayName = "Assigned Render Mode is inherited all the way down the component hierarchy")] | ||
public void Test008() | ||
{ | ||
var cut = Render( | ||
@<ComponentWithChildContent @rendermode="RenderMode.InteractiveServer"> | ||
<ComponentWithChildContent> | ||
<ComponentThatPrintsAssignedRenderMode /> | ||
</ComponentWithChildContent> | ||
</ComponentWithChildContent>); | ||
|
||
cut.MarkupMatches(@<p>Assigned Render Mode: InteractiveServerRenderMode</p>); | ||
} | ||
|
||
[Fact(DisplayName = "Having a component with section outlet and RenderMode is specifying for child component")] | ||
public void Test009() | ||
{ | ||
// See: https://learn.microsoft.com/en-us/aspnet/core/blazor/components/sections?view=aspnetcore-8.0#section-interaction-with-other-blazor-features | ||
var cut = Render(@<SectionOutletComponent />); | ||
|
||
cut.MarkupMatches(@<p>Assigned Render Mode: InteractiveWebAssemblyRenderMode</p>); | ||
} | ||
|
||
[Fact(DisplayName = "Assigned Render Mode on siblings")] | ||
public void Test010() | ||
{ | ||
var cut = Render( | ||
@<ComponentWithChildContent> | ||
<ComponentThatPrintsAssignedRenderMode @rendermode="RenderMode.InteractiveServer"/> | ||
<ComponentThatPrintsAssignedRenderMode @rendermode="RenderMode.InteractiveWebAssembly"/> | ||
</ComponentWithChildContent>); | ||
|
||
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() | ||
{ | ||
var act = () => Render( | ||
@<ComponentWithChildContent @rendermode="RenderMode.InteractiveServer"> | ||
<ComponentWithChildContent @rendermode="RenderMode.InteractiveWebAssembly"> | ||
<ComponentThatPrintsAssignedRenderMode /> | ||
</ComponentWithChildContent> | ||
</ComponentWithChildContent>); | ||
|
||
act.ShouldThrow<RenderModeMisMatchException>(); // todo: figure out good exception to use | ||
} | ||
#endif | ||
} |
9 changes: 9 additions & 0 deletions
9
tests/bunit.testassets/RenderModes/ComponentThatPrintsAssignedRenderMode.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
@{ | ||
#if NET9_0_OR_GREATER | ||
} | ||
|
||
<p>Assigned Render Mode: @AssignedRenderMode?.GetType().Name</p> | ||
|
||
@{ | ||
#endif | ||
} |
6 changes: 6 additions & 0 deletions
6
tests/bunit.testassets/RenderModes/ComponentWithChildContent.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@ChildContent | ||
@code { | ||
|
||
[Parameter] public RenderFragment ChildContent { get; set; } = default!; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,4 @@ | |
|
||
@{ | ||
#endif | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/bunit.testassets/RenderModes/SectionOutletComponent.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
@{ | ||
#if NET9_0_OR_GREATER | ||
} | ||
|
||
<Microsoft.AspNetCore.Components.Sections.SectionOutlet SectionId="1" @rendermode="RenderMode.InteractiveWebAssembly" /> | ||
<Microsoft.AspNetCore.Components.Sections.SectionContent SectionId="1"> | ||
<ComponentThatPrintsAssignedRenderMode/> | ||
</Microsoft.AspNetCore.Components.Sections.SectionContent> | ||
|
||
@{ | ||
#endif | ||
} |