|
| 1 | +@using Bunit.TestAssets.RenderModes; |
| 2 | +@inherits TestContext |
| 3 | +@code { |
| 4 | +#if NET9_0_OR_GREATER |
| 5 | + [Fact(DisplayName = "TestRenderer provides RendererInfo")] |
| 6 | + public void Test001() |
| 7 | + { |
| 8 | + Renderer.SetRendererInfo(new RendererInfo("Server", true)); |
| 9 | + var cut = RenderComponent<RendererInfoComponent>(); |
| 10 | + |
| 11 | + cut.MarkupMatches( |
| 12 | + @<text> |
| 13 | + <p>Is interactive: True</p> |
| 14 | + <p>Rendermode: Server</p> |
| 15 | + </text>); |
| 16 | + } |
| 17 | + |
| 18 | + [Fact(DisplayName = "Renderer throws exception if RendererInfo is not specified")] |
| 19 | + public void Test002() |
| 20 | + { |
| 21 | + Action act = () => RenderComponent<RendererInfoComponent>(); |
| 22 | + |
| 23 | + act.ShouldThrow<MissingRendererInfoException>(); |
| 24 | + } |
| 25 | + |
| 26 | + [Fact(DisplayName = "Renderer should set the RenderModeAttribute on the component")] |
| 27 | + public void Test003() |
| 28 | + { |
| 29 | + var cut = RenderComponent<ComponentWithServerRenderMode>(); |
| 30 | + |
| 31 | + cut.MarkupMatches(@<div>Assigned render mode: InteractiveServerRenderMode</div>); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact(DisplayName = "The AssignedRenderMode is based on the RenderModeAttribute in the component hierarchy where parent component has no RenderMode")] |
| 35 | + public void Test004() |
| 36 | + { |
| 37 | + var cut = Render( |
| 38 | + @<ComponentWithoutRenderMode> |
| 39 | + <ComponentWithWebAssemblyRenderMode /> |
| 40 | + </ComponentWithoutRenderMode>); |
| 41 | + |
| 42 | + cut.MarkupMatches( |
| 43 | + @<text> |
| 44 | + <div>Parent assigned render mode: </div> |
| 45 | + <div>Assigned render mode: InteractiveWebAssemblyRenderMode</div> |
| 46 | + </text>); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact(DisplayName = "Parent and child render mode is specified")] |
| 50 | + public void Test005() |
| 51 | + { |
| 52 | + var cut = Render( |
| 53 | + @<ComponentWithServerRenderMode> |
| 54 | + <ComponentWithServerRenderMode /> |
| 55 | + </ComponentWithServerRenderMode>); |
| 56 | + |
| 57 | + cut.MarkupMatches( |
| 58 | + @<text> |
| 59 | + <div>Parent assigned render mode: InteractiveServerRenderMode</div> |
| 60 | + <div>Assigned render mode: InteractiveServerRenderMode</div> |
| 61 | + </text>); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact(DisplayName = "Parent and child render mode is not specified")] |
| 65 | + public void Test006() |
| 66 | + { |
| 67 | + var cut = Render( |
| 68 | + @<ComponentWithoutRenderMode> |
| 69 | + <ComponentWithoutRenderMode /> |
| 70 | + </ComponentWithoutRenderMode>); |
| 71 | + |
| 72 | + cut.MarkupMatches( |
| 73 | + @<text> |
| 74 | + <div>Parent assigned render mode: </div> |
| 75 | + <div>Assigned render mode: </div> |
| 76 | + </text>); |
| 77 | + } |
| 78 | + |
| 79 | + [Fact(DisplayName = "Rendermode specified on child")] |
| 80 | + public void Test007() |
| 81 | + { |
| 82 | + var cut = Render( |
| 83 | + @<ComponentWithChildContent> |
| 84 | + <ComponentThatPrintsAssignedRenderMode @rendermode="RenderMode.InteractiveServer" /> |
| 85 | + </ComponentWithChildContent>); |
| 86 | + |
| 87 | + cut.MarkupMatches(@<p>Assigned Render Mode: InteractiveServerRenderMode</p>); |
| 88 | + } |
| 89 | + |
| 90 | + [Fact(DisplayName = "Assigned Render Mode is inherited all the way down the component hierarchy")] |
| 91 | + public void Test008() |
| 92 | + { |
| 93 | + var cut = Render( |
| 94 | + @<ComponentWithChildContent @rendermode="RenderMode.InteractiveServer"> |
| 95 | + <ComponentWithChildContent> |
| 96 | + <ComponentThatPrintsAssignedRenderMode /> |
| 97 | + </ComponentWithChildContent> |
| 98 | + </ComponentWithChildContent>); |
| 99 | + |
| 100 | + cut.MarkupMatches(@<p>Assigned Render Mode: InteractiveServerRenderMode</p>); |
| 101 | + } |
| 102 | + |
| 103 | + [Fact(DisplayName = "Having a component with section outlet and RenderMode is specifying for child component")] |
| 104 | + public void Test009() |
| 105 | + { |
| 106 | + // See: https://learn.microsoft.com/en-us/aspnet/core/blazor/components/sections?view=aspnetcore-8.0#section-interaction-with-other-blazor-features |
| 107 | + var cut = Render(@<SectionOutletComponent />); |
| 108 | + |
| 109 | + cut.MarkupMatches(@<p>Assigned Render Mode: InteractiveWebAssemblyRenderMode</p>); |
| 110 | + } |
| 111 | + |
| 112 | + [Fact(DisplayName = "Assigned Render Mode on siblings")] |
| 113 | + public void Test010() |
| 114 | + { |
| 115 | + var cut = Render( |
| 116 | + @<ComponentWithChildContent> |
| 117 | + <ComponentThatPrintsAssignedRenderMode @rendermode="RenderMode.InteractiveServer"/> |
| 118 | + <ComponentThatPrintsAssignedRenderMode @rendermode="RenderMode.InteractiveWebAssembly"/> |
| 119 | + </ComponentWithChildContent>); |
| 120 | + |
| 121 | + cut.MarkupMatches( |
| 122 | + @<text> |
| 123 | + <p>Assigned Render Mode: InteractiveServerRenderMode</p> |
| 124 | + <p>Assigned Render Mode: InteractiveWebAssemblyRenderMode</p> |
| 125 | + </text>); |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + [Fact(DisplayName = "Different assigned RenderMode between child and parent throws")] |
| 130 | + public void Test020() |
| 131 | + { |
| 132 | + var act = () => Render( |
| 133 | + @<ComponentWithChildContent @rendermode="RenderMode.InteractiveServer"> |
| 134 | + <ComponentWithChildContent @rendermode="RenderMode.InteractiveWebAssembly"> |
| 135 | + <ComponentThatPrintsAssignedRenderMode /> |
| 136 | + </ComponentWithChildContent> |
| 137 | + </ComponentWithChildContent>); |
| 138 | + |
| 139 | + act.ShouldThrow<RenderModeMisMatchException>(); // todo: figure out good exception to use |
| 140 | + } |
| 141 | +#endif |
| 142 | +} |
0 commit comments