Skip to content

Commit 582bc03

Browse files
authored
Beta 5.1 (#44) - fixed #43
* Fix for issue #43 * Tweaks to tests
1 parent 70b897f commit 582bc03

File tree

12 files changed

+111
-48
lines changed

12 files changed

+111
-48
lines changed

sample/tests/Tests/Components/FocussingInputTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Egil.RazorComponents.Testing.Asserting;
77
using Egil.RazorComponents.Testing.Mocking.JSInterop;
88
using Egil.RazorComponents.Testing.SampleApp.Components;
9+
using Egil.RazorComponents.Testing.Mocking.JSInterop;
910
using Xunit;
1011

1112
namespace Egil.RazorComponents.Testing.SampleApp.CodeOnlyTests.Components

sample/tests/Tests/Components/TodoListTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Egil.RazorComponents.Testing.Asserting;
44
using Egil.RazorComponents.Testing.Mocking.JSInterop;
55
using Egil.RazorComponents.Testing.EventDispatchExtensions;
6+
using Egil.RazorComponents.Testing.Mocking.JSInterop;
67
using Egil.RazorComponents.Testing.SampleApp.Components;
78
using Egil.RazorComponents.Testing.SampleApp.Data;
89
using Microsoft.AspNetCore.Components;

src/Components/ContainerComponent.cs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using System.Threading.Tasks;
99
using Egil.RazorComponents.Testing.Extensions;
10+
using AngleSharp.Css.Dom;
1011

1112
namespace Egil.RazorComponents.Testing
1213
{
@@ -32,7 +33,7 @@ public ContainerComponent(TestRenderer renderer)
3233
{
3334
if (renderer is null) throw new ArgumentNullException(nameof(renderer));
3435
_renderer = renderer;
35-
ComponentId = _renderer.AttachTestRootComponent(this);
36+
ComponentId = _renderer.AttachTestRootComponent(this);
3637
}
3738

3839
/// <inheritdoc/>
@@ -58,17 +59,14 @@ public void Render(RenderFragment renderFragment)
5859
/// component is found, its child content is also searched recursively.
5960
/// </summary>
6061
/// <typeparam name="TComponent">The type of component to find</typeparam>
61-
/// <exception cref="InvalidOperationException">When there are more than one component of type <typeparamref name="TComponent"/> found or if none are found.</exception>
62+
/// <exception cref="InvalidOperationException">When a component of type <typeparamref name="TComponent"/> was not found.</exception>
6263
public (int Id, TComponent Component) GetComponent<TComponent>() where TComponent : IComponent
6364
{
64-
var result = GetComponents<TComponent>();
65-
66-
if (result.Count == 1)
67-
return result[0];
68-
else if (result.Count == 0)
69-
throw new InvalidOperationException($"No components of type {typeof(TComponent)} were found in the render tree.");
65+
var result = GetComponent<TComponent>(ComponentId);
66+
if (result.HasValue)
67+
return result.Value;
7068
else
71-
throw new InvalidOperationException($"More than one component of type {typeof(TComponent)} was found in the render tree.");
69+
throw new InvalidOperationException($"No components of type {typeof(TComponent)} were found in the render tree.");
7270
}
7371

7472
/// <summary>
@@ -84,7 +82,7 @@ public void Render(RenderFragment renderFragment)
8482
var ownFrames = _renderer.GetCurrentRenderTreeFrames(componentId);
8583
if (ownFrames.Count == 0)
8684
{
87-
throw new InvalidOperationException($"{nameof(ContainerComponent)} hasn't yet rendered");
85+
return Array.Empty<(int Id, TComponent Component)>();
8886
}
8987

9088
var result = new List<(int Id, TComponent Component)>();
@@ -97,20 +95,32 @@ public void Render(RenderFragment renderFragment)
9795
{
9896
result.Add((frame.ComponentId, component));
9997
}
100-
else if (frame.Component.IsCascadingValueComponent())
98+
result.AddRange(GetComponents<TComponent>(frame.ComponentId));
99+
}
100+
}
101+
102+
return result;
103+
}
104+
105+
private (int Id, TComponent Component)? GetComponent<TComponent>(int componentId) where TComponent : IComponent
106+
{
107+
var ownFrames = _renderer.GetCurrentRenderTreeFrames(componentId);
108+
109+
for (int i = 0; i < ownFrames.Count; i++)
110+
{
111+
ref var frame = ref ownFrames.Array[i];
112+
if (frame.FrameType == RenderTreeFrameType.Component)
113+
{
114+
if (frame.Component is TComponent component)
101115
{
102-
// It seems as if CascadingValue components works a little different
103-
// than regular components with child content is not rendered
104-
// and available via GetCurrentRenderTreeFrames for the componentId
105-
// of the component that had the CascadingValue as a child.
106-
// Thus we call GetComponents recursively with the CascadingValue's
107-
// componentId to see if the TComponent is inside it.
108-
result.AddRange(GetComponents<TComponent>(frame.ComponentId));
116+
return (frame.ComponentId, component);
109117
}
118+
var result = GetComponent<TComponent>(frame.ComponentId);
119+
if (result != null) return result;
110120
}
111121
}
112122

113-
return result;
123+
return null;
114124
}
115125
}
116126
}

src/Egil.RazorComponents.Testing.Library.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ This library's goal is to make it easy to write comprehensive, stable unit tests
5151
<ItemGroup>
5252
<PackageReference Include="AngleSharp" Version="0.13.0" />
5353
<PackageReference Include="AngleSharp.Css" Version="0.13.0" />
54-
<PackageReference Include="AngleSharp.Diffing" Version="0.13.2" />
54+
<PackageReference Include="AngleSharp.Diffing" Version="0.13.3-alpha-44" />
5555
<PackageReference Include="RichardSzalay.MockHttp" Version="6.0.0" />
5656
<PackageReference Include="xunit.assert" Version="2.4.1" />
5757
<PackageReference Include="xunit.extensibility.core" Version="2.4.1" />

src/ElementNotFoundException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Text;
66
using System.Threading.Tasks;
77

8-
namespace Egil.RazorComponents.Testing
8+
namespace Xunit.Sdk
99
{
1010
/// <summary>
1111
/// Represents a failure to find an element in the searched target

src/Rendering/IRenderedFragment.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using AngleSharp.Diffing.Core;
55
using AngleSharp.Dom;
6+
using Xunit.Sdk;
67

78
namespace Egil.RazorComponents.Testing
89
{

src/Rendering/RenderedFragmentBase.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ public IReadOnlyList<IDiff> GetChangesSinceSnapshot()
9393
return Nodes.CompareTo(_snapshotNodes);
9494
}
9595

96-
9796
/// <inheritdoc/>
9897
public IReadOnlyList<IDiff> GetChangesSinceFirstRender()
9998
{
@@ -104,24 +103,30 @@ public IReadOnlyList<IDiff> GetChangesSinceFirstRender()
104103

105104
private void ComponentMarkupChanged(in RenderBatch renderBatch)
106105
{
107-
if (renderBatch.HasUpdatesTo(ComponentId) || HasChildComponentUpdated(renderBatch))
106+
if (renderBatch.HasUpdatesTo(ComponentId) || HasChildComponentUpdated(renderBatch, ComponentId))
108107
{
109108
ResetLatestRenderCache();
110109
}
111110
}
112111

113-
private bool HasChildComponentUpdated(in RenderBatch renderBatch)
112+
private bool HasChildComponentUpdated(in RenderBatch renderBatch, int componentId)
114113
{
115-
var frames = TestContext.Renderer.GetCurrentRenderTreeFrames(ComponentId);
114+
var frames = TestContext.Renderer.GetCurrentRenderTreeFrames(componentId);
115+
116116
for (int i = 0; i < frames.Count; i++)
117117
{
118118
var frame = frames.Array[i];
119-
120-
if (renderBatch.HasUpdatesTo(frame.ComponentId))
119+
if (frame.FrameType == RenderTreeFrameType.Component)
121120
{
122-
return true;
121+
if (renderBatch.HasUpdatesTo(frame.ComponentId))
122+
{
123+
return true;
124+
}
125+
if (HasChildComponentUpdated(in renderBatch, frame.ComponentId))
126+
{
127+
return true;
128+
}
123129
}
124-
125130
}
126131
return false;
127132
}

tests/Components/TestComponentBaseTest/BlazorElementReferencesIncludedInRenderedMarkup.razor

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
@inherits TestComponentBase
22

3-
43
<Fixture Test="Test">
54
<Fragment>
65
<div @ref="refElm" />

tests/RenderComponentTest.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void Test003()
2222

2323
[Fact(DisplayName = "Nodes should return new instance " +
2424
"when a SetParametersAndRender has caused changes to DOM tree")]
25-
public void Tets004()
25+
public void Test004()
2626
{
2727
var cut = RenderComponent<Wrapper>(ChildContent("<div>"));
2828
var initialNodes = cut.Nodes;
@@ -35,7 +35,7 @@ public void Tets004()
3535

3636
[Fact(DisplayName = "Nodes should return new instance " +
3737
"when a Render has caused changes to DOM tree")]
38-
public void Tets005()
38+
public void Test005()
3939
{
4040
var cut = RenderComponent<RenderCounter>();
4141
var initialNodes = cut.Nodes;
@@ -44,20 +44,5 @@ public void Tets005()
4444

4545
Assert.NotSame(initialNodes, cut.Nodes);
4646
}
47-
48-
[Fact(DisplayName = "Nodes should return new instance " +
49-
"when a event handler trigger has caused changes to DOM tree")]
50-
public void Tets006()
51-
{
52-
var cut = RenderComponent<ClickCounter>();
53-
var initialNodes = cut.Nodes;
54-
55-
cut.Find("button").Click();
56-
57-
Assert.NotSame(initialNodes, cut.Nodes);
58-
}
59-
60-
6147
}
62-
6348
}

tests/RenderedFragmentTest.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
using Egil.RazorComponents.Testing.Extensions;
1+
using Egil.RazorComponents.Testing.EventDispatchExtensions;
2+
using Egil.RazorComponents.Testing.Extensions;
23
using Egil.RazorComponents.Testing.SampleComponents;
34
using Egil.RazorComponents.Testing.SampleComponents.Data;
5+
using Microsoft.AspNetCore.Components;
46
using Microsoft.Extensions.DependencyInjection;
57
using Shouldly;
68
using System;
79
using System.Collections.Generic;
810
using System.Linq;
911
using System.Text;
1012
using Xunit;
13+
using Xunit.Sdk;
1114

1215
namespace Egil.RazorComponents.Testing
1316
{
@@ -70,6 +73,51 @@ public void Test005()
7073

7174
Assert.NotSame(initialValue, cut.Nodes);
7275
}
76+
77+
78+
[Fact(DisplayName = "Nodes should return new instance " +
79+
"when a event handler trigger has caused changes to DOM tree")]
80+
public void Test006()
81+
{
82+
var cut = RenderComponent<ClickCounter>();
83+
var initialNodes = cut.Nodes;
84+
85+
cut.Find("button").Click();
86+
87+
Assert.NotSame(initialNodes, cut.Nodes);
88+
}
89+
90+
[Fact(DisplayName = "Nodes should return new instance " +
91+
"when a nested component has caused the DOM tree to change")]
92+
public void Test007()
93+
{
94+
var cut = RenderComponent<Wrapper>(
95+
ChildContent<CascadingValue<string>>(
96+
("Value", "FOO"),
97+
ChildContent<ClickCounter>()
98+
)
99+
);
100+
var initialNodes = cut.Nodes;
101+
102+
cut.Find("button").Click();
103+
104+
Assert.NotSame(initialNodes, cut.Nodes);
105+
}
106+
107+
[Fact(DisplayName = "Nodes should return the same instance " +
108+
"when a re-render does not causes the DOM to change")]
109+
public void Test008()
110+
{
111+
var cut = RenderComponent<RenderOnClick>();
112+
var initialNodes = cut.Nodes;
113+
114+
cut.Find("button").Click();
115+
116+
cut.Instance.RenderCount.ShouldBe(2);
117+
Assert.Same(initialNodes, cut.Nodes);
118+
}
119+
120+
73121
}
74122

75123
}

0 commit comments

Comments
 (0)