Skip to content

Commit 95f14fa

Browse files
committed
Changed ITestContext to have a CreateNodes method instead of HtmlParser property
1 parent ac61758 commit 95f14fa

12 files changed

Lines changed: 68 additions & 19 deletions

src/Asserting/CompareToDiffingExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static IReadOnlyList<IDiff> CompareTo(this IRenderedFragment actual, stri
2626
if (expected is null) throw new ArgumentNullException(nameof(expected));
2727

2828
var actualNodes = actual.GetNodes();
29-
var expectedNodes = actual.TestContext.HtmlParser.Parse(expected);
29+
var expectedNodes = actual.TestContext.CreateNodes(expected);
3030

3131
return actualNodes.CompareTo(expectedNodes);
3232
}

src/Asserting/MarkupMatchesAssertExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static void MarkupMatches(this IRenderedFragment actual, string expected,
2626
if (expected is null) throw new ArgumentNullException(nameof(expected));
2727

2828
var actualNodes = actual.GetNodes();
29-
var expectedNodes = actual.TestContext.HtmlParser.Parse(expected);
29+
var expectedNodes = actual.TestContext.CreateNodes(expected);
3030

3131
actualNodes.MarkupMatches(expectedNodes, userMessage);
3232
}

src/Asserting/ShouldBeTextChangeAssertExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
namespace Egil.RazorComponents.Testing.Asserting
1212
{
13+
/// <summary>
14+
/// Verification helpers for text
15+
/// </summary>
1316
public static class ShouldBeTextChangeAssertExtensions
1417
{
1518
public static void ShouldHaveSingleTextChange(this IReadOnlyList<IDiff> diffs, string expectedChange, string? userMessage = null)

src/Components/TestComponentBase.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Runtime.CompilerServices;
44
using System.Text;
55
using System.Threading.Tasks;
6+
using AngleSharp.Dom;
67
using Egil.RazorComponents.Testing.Asserting;
78
using Egil.RazorComponents.Testing.Diffing;
89
using Microsoft.AspNetCore.Components;
@@ -33,10 +34,6 @@ public override TestServiceProvider Services
3334
public override TestRenderer Renderer
3435
=> _testContextAdapter.HasActiveContext ? _testContextAdapter.Renderer : base.Renderer;
3536

36-
/// <inheritdoc/>
37-
public override TestHtmlParser HtmlParser
38-
=> _testContextAdapter.HasActiveContext ? _testContextAdapter.HtmlParser : base.HtmlParser;
39-
4037
/// <inheritdoc/>
4138
public TestComponentBase()
4239
{
@@ -61,7 +58,7 @@ public async Task RazorTest()
6158
await ExecuteFixtureTests(container).ConfigureAwait(false);
6259
ExecuteSnapshotTests(container);
6360
}
64-
61+
6562
/// <inheritdoc/>
6663
public IRenderedFragment GetComponentUnderTest()
6764
=> _testContextAdapter.GetComponentUnderTest();
@@ -78,6 +75,12 @@ public IRenderedFragment GetFragment(string? id = null)
7875
public IRenderedComponent<TComponent> GetFragment<TComponent>(string? id = null) where TComponent : class, IComponent
7976
=> _testContextAdapter.GetFragment<TComponent>(id);
8077

78+
/// <inheritdoc/>
79+
public override INodeList CreateNodes(string markup)
80+
=> _testContextAdapter.HasActiveContext
81+
? _testContextAdapter.CreateNodes(markup)
82+
: base.CreateNodes(markup);
83+
8184
/// <inheritdoc/>
8285
public override IRenderedComponent<TComponent> RenderComponent<TComponent>(params ComponentParameter[] parameters)
8386
=> _testContextAdapter.HasActiveContext

src/Components/TestContextAdapter.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using AngleSharp.Dom;
34
using Egil.RazorComponents.Testing.Diffing;
45
using Microsoft.AspNetCore.Components;
56

@@ -14,8 +15,6 @@ internal sealed class TestContextAdapter : IDisposable
1415

1516
public TestRenderer Renderer => _testContext?.Renderer ?? throw new InvalidOperationException("No active test context in the adapter");
1617

17-
public TestHtmlParser HtmlParser => _testContext?.HtmlParser ?? throw new InvalidOperationException("No active test context in the adapter");
18-
1918
public bool HasActiveContext => !(_testContext is null);
2019

2120
public SnapshotTestContext ActivateSnapshotTestContext(IReadOnlyList<FragmentBase> testData)
@@ -41,6 +40,7 @@ public RazorTestContext ActivateRazorTestContext(IReadOnlyList<FragmentBase> tes
4140
public void Dispose()
4241
{
4342
_testContext?.Dispose();
43+
_razorTestContext?.Dispose();
4444
_testContext = null;
4545
_razorTestContext = null;
4646
}
@@ -67,5 +67,8 @@ public void WaitForNextRender(Action renderTrigger, TimeSpan? timeout = null)
6767

6868
public IRenderedComponent<TComponent> RenderComponent<TComponent>(params ComponentParameter[] parameters) where TComponent : class, IComponent
6969
=> _testContext?.RenderComponent<TComponent>(parameters) ?? throw new InvalidOperationException("No active test context in the adapter");
70+
71+
public INodeList CreateNodes(string markup)
72+
=> _testContext?.CreateNodes(markup) ?? throw new InvalidOperationException("No active test context in the adapter");
7073
}
7174
}

src/ElementNotFoundException.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,17 @@ public ElementNotFoundException(string cssSelector) : base($"No elements were fo
2323
{
2424
CssSelector = cssSelector;
2525
}
26+
27+
/// <inheritdoc/>
28+
public ElementNotFoundException()
29+
{
30+
CssSelector = string.Empty;
31+
}
32+
33+
/// <inheritdoc/>
34+
public ElementNotFoundException(string message, Exception innerException) : base(message, innerException)
35+
{
36+
CssSelector = string.Empty;
37+
}
2638
}
2739
}

src/ITestContext.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using AngleSharp.Dom;
23
using Egil.RazorComponents.Testing.Diffing;
34
using Microsoft.AspNetCore.Components;
45

@@ -19,11 +20,14 @@ public interface ITestContext : IDisposable
1920
/// Gets the renderer used to render the components and fragments in this test context.
2021
/// </summary>
2122
TestRenderer Renderer { get; }
22-
23+
2324
/// <summary>
24-
/// Gets the HTML parser used to parse HTML produced by components and fragments in this test context.
25+
/// Parses a markup HTML string using the AngleSharps HTML5 parser
26+
/// and returns a list of nodes.
2527
/// </summary>
26-
TestHtmlParser HtmlParser { get; }
28+
/// <param name="markup">The markup to parse.</param>
29+
/// <returns>The <see cref="INodeList"/>.</returns>
30+
INodeList CreateNodes(string markup);
2731

2832
/// <summary>
2933
/// Instantiates and performs a first render of a component of type <typeparamref name="TComponent"/>.

src/Mocking/JSInterop/JsRuntimeAssertExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public static JsRuntimeInvocation VerifyInvoke(this MockJsRuntimeInvokeHandler h
4343
/// </summary>
4444
/// <param name="handler">Handler to verify against.</param>
4545
/// <param name="identifier">Identifier of invocation that should have been invoked.</param>
46+
/// <param name="calledTimes">The number of times the invocation is expected to have been called.</param>
4647
/// <param name="userMessage">A custom user message to display if the assertion fails.</param>
4748
/// <returns>The <see cref="JsRuntimeInvocation"/>.</returns>
4849
public static IReadOnlyList<JsRuntimeInvocation> VerifyInvoke(this MockJsRuntimeInvokeHandler handler, string identifier, int calledTimes, string? userMessage = null)

src/Mocking/JSInterop/JsRuntimeInvocation.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using System.Collections.Generic;
22
using System;
3+
using System.Diagnostics.CodeAnalysis;
34
using System.Threading;
45

56
namespace Egil.RazorComponents.Testing
67
{
78
/// <summary>
89
/// Represents an invocation of JavaScript via the JsRuntime Mock
910
/// </summary>
11+
[SuppressMessage("Design", "CA1068:CancellationToken parameters must come last", Justification = "<Pending>")]
1012
public readonly struct JsRuntimeInvocation : IEquatable<JsRuntimeInvocation>
1113
{
1214
/// <summary>
@@ -24,6 +26,7 @@ namespace Egil.RazorComponents.Testing
2426
/// </summary>
2527
public IReadOnlyList<object> Arguments { get; }
2628

29+
2730
/// <summary>
2831
/// Creates an instance of the <see cref="JsRuntimeInvocation"/>.
2932
/// </summary>

src/Rendering/RenderedFragmentBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public IReadOnlyList<IDiff> GetChangesSinceSnapshot()
6666
throw new InvalidOperationException($"No snapshot exists to compare with. Call {nameof(SaveSnapshot)} to create one.");
6767

6868
if(_snapshotNodes is null)
69-
_snapshotNodes = TestContext.HtmlParser.Parse(_snapshotMarkup);
69+
_snapshotNodes = TestContext.CreateNodes(_snapshotMarkup);
7070

7171
return GetNodes().CompareTo(_snapshotNodes);
7272
}
@@ -76,7 +76,7 @@ public IReadOnlyList<IDiff> GetChangesSinceSnapshot()
7676
public IReadOnlyList<IDiff> GetChangesSinceFirstRender()
7777
{
7878
if (_firstRenderNodes is null)
79-
_firstRenderNodes = TestContext.HtmlParser.Parse(FirstRenderMarkup);
79+
_firstRenderNodes = TestContext.CreateNodes(FirstRenderMarkup);
8080
return GetNodes().CompareTo(_firstRenderNodes);
8181
}
8282

@@ -93,7 +93,7 @@ public string GetMarkup()
9393
public INodeList GetNodes()
9494
{
9595
if (_latestRenderNodes is null)
96-
_latestRenderNodes = TestContext.HtmlParser.Parse(GetMarkup());
96+
_latestRenderNodes = TestContext.CreateNodes(GetMarkup());
9797
return _latestRenderNodes;
9898
}
9999

0 commit comments

Comments
 (0)