Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
9d63a0d
Fixed spelling mistake in comment
egil Jan 6, 2020
e7b79ac
Reordered parameters to top of TestRenderer
egil Jan 6, 2020
457409c
Add support for asynchronous Razor tests (#27)
duracellko Jan 15, 2020
057e005
Dotnetconf samples (#33)
egil Jan 14, 2020
51d3a0f
Added unit tests of CompareTo, Generic and Collection assert extensions.
egil Jan 16, 2020
4925628
Added tests for general events and touch events dispatch extensions
egil Jan 16, 2020
26be467
Added tests for anglesharp extensions, JsRuntimeInvocation and Compon…
egil Jan 16, 2020
e99b1d7
Removed assert helpers that conflict with Shoudly
egil Jan 16, 2020
7dedd33
Tests of JsRuntimeAsserts
egil Jan 16, 2020
d077e11
Reorganized test library
egil Jan 16, 2020
ac61758
Suppressing warnings in sample
egil Jan 16, 2020
95f14fa
Changed ITestContext to have a CreateNodes method instead of HtmlPars…
egil Jan 16, 2020
751c43d
Removed empty test
egil Jan 16, 2020
e04e331
Added missing code documentation
egil Jan 16, 2020
9d50e6f
Moved MockJsRuntime to its own namespace
egil Jan 17, 2020
a9d65f6
Pulled sample from main solution into own solution
egil Jan 17, 2020
0af8b13
Update main.yml
egil Jan 17, 2020
31c4a4f
Change GetNodes and GetMarkup to Nodes and Markup properties in IRend…
egil Jan 20, 2020
c78be10
Add SetupVoid and SetVoidResult capabilities to JsRuntime mock (#35)
egil Jan 21, 2020
7411c68
Add default JsRuntime (#32)
Siphonophora Jan 22, 2020
7721421
Add .vscode to gitignore
egil Jan 22, 2020
6d57f19
TestServiceProvider now explictly implements IServiceCOlelction (#40)
egil Jan 22, 2020
ea034d1
Added async setup method to snapshot test
egil Jan 22, 2020
893fdad
Added test of SnapshotTests use of setup methods
egil Jan 22, 2020
865ea40
Update to readme
egil Jan 22, 2020
41f8ece
Merge branch 'master' into dev
egil Jan 22, 2020
d246f73
Removed duplicated MarkupMatches method
egil Jan 22, 2020
64faa76
Removed PR trigger
egil Jan 22, 2020
3e4e3c8
new version of diffing
egil Jan 23, 2020
adb94bd
Merge branch 'master' into dev
egil Jan 23, 2020
cc4c71f
Fix for issue #43
egil Jan 23, 2020
aed5ff3
Tweaks to tests
egil Jan 24, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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