Skip to content

Commit

Permalink
Beta 5.1 (#44) - fixed #43
Browse files Browse the repository at this point in the history
* Fix for issue #43
* Tweaks to tests
  • Loading branch information
egil authored Jan 24, 2020
1 parent 70b897f commit 582bc03
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 48 deletions.
1 change: 1 addition & 0 deletions sample/tests/Tests/Components/FocussingInputTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Egil.RazorComponents.Testing.Asserting;
using Egil.RazorComponents.Testing.Mocking.JSInterop;
using Egil.RazorComponents.Testing.SampleApp.Components;
using Egil.RazorComponents.Testing.Mocking.JSInterop;
using Xunit;

namespace Egil.RazorComponents.Testing.SampleApp.CodeOnlyTests.Components
Expand Down
1 change: 1 addition & 0 deletions sample/tests/Tests/Components/TodoListTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Egil.RazorComponents.Testing.Asserting;
using Egil.RazorComponents.Testing.Mocking.JSInterop;
using Egil.RazorComponents.Testing.EventDispatchExtensions;
using Egil.RazorComponents.Testing.Mocking.JSInterop;
using Egil.RazorComponents.Testing.SampleApp.Components;
using Egil.RazorComponents.Testing.SampleApp.Data;
using Microsoft.AspNetCore.Components;
Expand Down
48 changes: 29 additions & 19 deletions src/Components/ContainerComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using System.Threading.Tasks;
using Egil.RazorComponents.Testing.Extensions;
using AngleSharp.Css.Dom;

namespace Egil.RazorComponents.Testing
{
Expand All @@ -32,7 +33,7 @@ public ContainerComponent(TestRenderer renderer)
{
if (renderer is null) throw new ArgumentNullException(nameof(renderer));
_renderer = renderer;
ComponentId = _renderer.AttachTestRootComponent(this);
ComponentId = _renderer.AttachTestRootComponent(this);
}

/// <inheritdoc/>
Expand All @@ -58,17 +59,14 @@ public void Render(RenderFragment renderFragment)
/// component is found, its child content is also searched recursively.
/// </summary>
/// <typeparam name="TComponent">The type of component to find</typeparam>
/// <exception cref="InvalidOperationException">When there are more than one component of type <typeparamref name="TComponent"/> found or if none are found.</exception>
/// <exception cref="InvalidOperationException">When a component of type <typeparamref name="TComponent"/> was not found.</exception>
public (int Id, TComponent Component) GetComponent<TComponent>() where TComponent : IComponent
{
var result = GetComponents<TComponent>();

if (result.Count == 1)
return result[0];
else if (result.Count == 0)
throw new InvalidOperationException($"No components of type {typeof(TComponent)} were found in the render tree.");
var result = GetComponent<TComponent>(ComponentId);
if (result.HasValue)
return result.Value;
else
throw new InvalidOperationException($"More than one component of type {typeof(TComponent)} was found in the render tree.");
throw new InvalidOperationException($"No components of type {typeof(TComponent)} were found in the render tree.");
}

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

var result = new List<(int Id, TComponent Component)>();
Expand All @@ -97,20 +95,32 @@ public void Render(RenderFragment renderFragment)
{
result.Add((frame.ComponentId, component));
}
else if (frame.Component.IsCascadingValueComponent())
result.AddRange(GetComponents<TComponent>(frame.ComponentId));
}
}

return result;
}

private (int Id, TComponent Component)? GetComponent<TComponent>(int componentId) where TComponent : IComponent
{
var ownFrames = _renderer.GetCurrentRenderTreeFrames(componentId);

for (int i = 0; i < ownFrames.Count; i++)
{
ref var frame = ref ownFrames.Array[i];
if (frame.FrameType == RenderTreeFrameType.Component)
{
if (frame.Component is TComponent component)
{
// It seems as if CascadingValue components works a little different
// than regular components with child content is not rendered
// and available via GetCurrentRenderTreeFrames for the componentId
// of the component that had the CascadingValue as a child.
// Thus we call GetComponents recursively with the CascadingValue's
// componentId to see if the TComponent is inside it.
result.AddRange(GetComponents<TComponent>(frame.ComponentId));
return (frame.ComponentId, component);
}
var result = GetComponent<TComponent>(frame.ComponentId);
if (result != null) return result;
}
}

return result;
return null;
}
}
}
2 changes: 1 addition & 1 deletion src/Egil.RazorComponents.Testing.Library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ This library's goal is to make it easy to write comprehensive, stable unit tests
<ItemGroup>
<PackageReference Include="AngleSharp" Version="0.13.0" />
<PackageReference Include="AngleSharp.Css" Version="0.13.0" />
<PackageReference Include="AngleSharp.Diffing" Version="0.13.2" />
<PackageReference Include="AngleSharp.Diffing" Version="0.13.3-alpha-44" />
<PackageReference Include="RichardSzalay.MockHttp" Version="6.0.0" />
<PackageReference Include="xunit.assert" Version="2.4.1" />
<PackageReference Include="xunit.extensibility.core" Version="2.4.1" />
Expand Down
2 changes: 1 addition & 1 deletion src/ElementNotFoundException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Text;
using System.Threading.Tasks;

namespace Egil.RazorComponents.Testing
namespace Xunit.Sdk
{
/// <summary>
/// Represents a failure to find an element in the searched target
Expand Down
1 change: 1 addition & 0 deletions src/Rendering/IRenderedFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using AngleSharp.Diffing.Core;
using AngleSharp.Dom;
using Xunit.Sdk;

namespace Egil.RazorComponents.Testing
{
Expand Down
21 changes: 13 additions & 8 deletions src/Rendering/RenderedFragmentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ public IReadOnlyList<IDiff> GetChangesSinceSnapshot()
return Nodes.CompareTo(_snapshotNodes);
}


/// <inheritdoc/>
public IReadOnlyList<IDiff> GetChangesSinceFirstRender()
{
Expand All @@ -104,24 +103,30 @@ public IReadOnlyList<IDiff> GetChangesSinceFirstRender()

private void ComponentMarkupChanged(in RenderBatch renderBatch)
{
if (renderBatch.HasUpdatesTo(ComponentId) || HasChildComponentUpdated(renderBatch))
if (renderBatch.HasUpdatesTo(ComponentId) || HasChildComponentUpdated(renderBatch, ComponentId))
{
ResetLatestRenderCache();
}
}

private bool HasChildComponentUpdated(in RenderBatch renderBatch)
private bool HasChildComponentUpdated(in RenderBatch renderBatch, int componentId)
{
var frames = TestContext.Renderer.GetCurrentRenderTreeFrames(ComponentId);
var frames = TestContext.Renderer.GetCurrentRenderTreeFrames(componentId);

for (int i = 0; i < frames.Count; i++)
{
var frame = frames.Array[i];

if (renderBatch.HasUpdatesTo(frame.ComponentId))
if (frame.FrameType == RenderTreeFrameType.Component)
{
return true;
if (renderBatch.HasUpdatesTo(frame.ComponentId))
{
return true;
}
if (HasChildComponentUpdated(in renderBatch, frame.ComponentId))
{
return true;
}
}

}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
@inherits TestComponentBase


<Fixture Test="Test">
<Fragment>
<div @ref="refElm" />
Expand Down
19 changes: 2 additions & 17 deletions tests/RenderComponentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void Test003()

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

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

Assert.NotSame(initialNodes, cut.Nodes);
}

[Fact(DisplayName = "Nodes should return new instance " +
"when a event handler trigger has caused changes to DOM tree")]
public void Tets006()
{
var cut = RenderComponent<ClickCounter>();
var initialNodes = cut.Nodes;

cut.Find("button").Click();

Assert.NotSame(initialNodes, cut.Nodes);
}


}

}
50 changes: 49 additions & 1 deletion tests/RenderedFragmentTest.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using Egil.RazorComponents.Testing.Extensions;
using Egil.RazorComponents.Testing.EventDispatchExtensions;
using Egil.RazorComponents.Testing.Extensions;
using Egil.RazorComponents.Testing.SampleComponents;
using Egil.RazorComponents.Testing.SampleComponents.Data;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
using Xunit.Sdk;

namespace Egil.RazorComponents.Testing
{
Expand Down Expand Up @@ -70,6 +73,51 @@ public void Test005()

Assert.NotSame(initialValue, cut.Nodes);
}


[Fact(DisplayName = "Nodes should return new instance " +
"when a event handler trigger has caused changes to DOM tree")]
public void Test006()
{
var cut = RenderComponent<ClickCounter>();
var initialNodes = cut.Nodes;

cut.Find("button").Click();

Assert.NotSame(initialNodes, cut.Nodes);
}

[Fact(DisplayName = "Nodes should return new instance " +
"when a nested component has caused the DOM tree to change")]
public void Test007()
{
var cut = RenderComponent<Wrapper>(
ChildContent<CascadingValue<string>>(
("Value", "FOO"),
ChildContent<ClickCounter>()
)
);
var initialNodes = cut.Nodes;

cut.Find("button").Click();

Assert.NotSame(initialNodes, cut.Nodes);
}

[Fact(DisplayName = "Nodes should return the same instance " +
"when a re-render does not causes the DOM to change")]
public void Test008()
{
var cut = RenderComponent<RenderOnClick>();
var initialNodes = cut.Nodes;

cut.Find("button").Click();

cut.Instance.RenderCount.ShouldBe(2);
Assert.Same(initialNodes, cut.Nodes);
}


}

}
9 changes: 9 additions & 0 deletions tests/SampleComponents/RenderOnClick.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<button @onclick="IncreaseCount">Click to render again</button>

@code {
public int RenderCount { get; private set; }

void IncreaseCount() { }

protected override void OnAfterRender(bool firstRender) => RenderCount++;
}
4 changes: 4 additions & 0 deletions tests/_Imports.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.Extensions.DependencyInjection
@using Egil.RazorComponents.Testing
@using Egil.RazorComponents.Testing.Asserting
@using Egil.RazorComponents.Testing.EventDispatchExtensions
@using Egil.RazorComponents.Testing.Mocking.JSInterop
@using Egil.RazorComponents.Testing.SampleComponents
@using Egil.RazorComponents.Testing.SampleComponents.Data
@using Shouldly
Expand Down

0 comments on commit 582bc03

Please sign in to comment.