Skip to content

Commit 31c4a4f

Browse files
authored
Change GetNodes and GetMarkup to Nodes and Markup properties in IRenderedFragment (#34)
1 parent 0af8b13 commit 31c4a4f

16 files changed

Lines changed: 73 additions & 74 deletions

sample/tests/RazorTestComponents/Components/ThemedButtonTest.razor

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
void Test()
1616
{
1717
var cut = GetComponentUnderTest<ThemedButton>();
18-
var x = cut.GetMarkup();
1918
cut.Find("button").ClassList.ShouldContain("btn");
2019
}
2120
}

src/Asserting/CompareToDiffingExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ public static IReadOnlyList<IDiff> CompareTo(this IRenderedFragment actual, stri
2525
if (actual is null) throw new ArgumentNullException(nameof(actual));
2626
if (expected is null) throw new ArgumentNullException(nameof(expected));
2727

28-
var actualNodes = actual.GetNodes();
2928
var expectedNodes = actual.TestContext.CreateNodes(expected);
3029

31-
return actualNodes.CompareTo(expectedNodes);
30+
return actual.Nodes.CompareTo(expectedNodes);
3231
}
3332

3433
/// <summary>
@@ -43,7 +42,7 @@ public static IReadOnlyList<IDiff> CompareTo(this IRenderedFragment actual, IRen
4342
if (actual is null) throw new ArgumentNullException(nameof(actual));
4443
if (expected is null) throw new ArgumentNullException(nameof(expected));
4544

46-
return actual.GetNodes().CompareTo(expected.GetNodes());
45+
return actual.Nodes.CompareTo(expected.Nodes);
4746
}
4847

4948
/// <summary>

src/Asserting/MarkupMatchesAssertExtensions.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ public static void MarkupMatches(this IRenderedFragment actual, string expected,
2525
if (actual is null) throw new ArgumentNullException(nameof(actual));
2626
if (expected is null) throw new ArgumentNullException(nameof(expected));
2727

28-
var actualNodes = actual.GetNodes();
2928
var expectedNodes = actual.TestContext.CreateNodes(expected);
3029

31-
actualNodes.MarkupMatches(expectedNodes, userMessage);
30+
actual.Nodes.MarkupMatches(expectedNodes, userMessage);
3231
}
3332

3433
/// <summary>
@@ -44,7 +43,7 @@ public static void MarkupMatches(this IRenderedFragment actual, IRenderedFragmen
4443
if (actual is null) throw new ArgumentNullException(nameof(actual));
4544
if (expected is null) throw new ArgumentNullException(nameof(expected));
4645

47-
actual.GetNodes().MarkupMatches(expected.GetNodes(), userMessage);
46+
actual.Nodes.MarkupMatches(expected.Nodes, userMessage);
4847
}
4948

5049
/// <summary>
@@ -61,7 +60,7 @@ public static void MarkupMatches(this INodeList actual, IRenderedFragment expect
6160
if (actual is null) throw new ArgumentNullException(nameof(actual));
6261
if (expected is null) throw new ArgumentNullException(nameof(expected));
6362

64-
actual.MarkupMatches(expected.GetNodes(), userMessage);
63+
actual.MarkupMatches(expected.Nodes, userMessage);
6564
}
6665

6766
/// <summary>
@@ -78,7 +77,7 @@ public static void MarkupMatches(this INode actual, IRenderedFragment expected,
7877
if (actual is null) throw new ArgumentNullException(nameof(actual));
7978
if (expected is null) throw new ArgumentNullException(nameof(expected));
8079

81-
actual.MarkupMatches(expected.GetNodes(), userMessage);
80+
actual.MarkupMatches(expected.Nodes, userMessage);
8281
}
8382

8483
/// <summary>

src/Asserting/ShouldBeAdditionAssertExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static void ShouldBeAddition(this IDiff actualChange, string expectedChan
5555
public static void ShouldBeAddition(this IDiff actualChange, IRenderedFragment expectedChange, string? userMessage = null)
5656
{
5757
if (expectedChange is null) throw new ArgumentNullException(nameof(expectedChange));
58-
ShouldBeAddition(actualChange, expectedChange.GetNodes(), userMessage);
58+
ShouldBeAddition(actualChange, expectedChange.Nodes, userMessage);
5959
}
6060

6161
/// <summary>

src/Asserting/ShouldBeRemovalAssertExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static void ShouldBeRemoval(this IDiff actualChange, string expectedChang
5454
public static void ShouldBeRemoval(this IDiff actualChange, IRenderedFragment expectedChange, string? userMessage = null)
5555
{
5656
if (expectedChange is null) throw new ArgumentNullException(nameof(expectedChange));
57-
ShouldBeRemoval(actualChange, expectedChange.GetNodes(), userMessage);
57+
ShouldBeRemoval(actualChange, expectedChange.Nodes, userMessage);
5858
}
5959

6060
/// <summary>

src/Asserting/ShouldBeTextChangeAssertExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static void ShouldBeTextChange(this IDiff actualChange, string expectedCh
5353
public static void ShouldBeTextChange(this IDiff actualChange, IRenderedFragment expectedChange, string? userMessage = null)
5454
{
5555
if (expectedChange is null) throw new ArgumentNullException(nameof(expectedChange));
56-
ShouldBeTextChange(actualChange, expectedChange.GetNodes(), userMessage);
56+
ShouldBeTextChange(actualChange, expectedChange.Nodes, userMessage);
5757
}
5858

5959
/// <summary>

src/Rendering/IRenderedFragment.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ public interface IRenderedFragment
1919
/// <summary>
2020
/// Gets the HTML markup from the rendered fragment/component.
2121
/// </summary>
22-
/// <returns></returns>
23-
string GetMarkup();
22+
string Markup { get; }
2423

2524
/// <summary>
2625
/// Gets the AngleSharp <see cref="INodeList"/> based
2726
/// on the HTML markup from the rendered fragment/component.
2827
/// </summary>
29-
/// <returns></returns>
30-
INodeList GetNodes();
28+
INodeList Nodes { get; }
3129

3230
/// <summary>
3331
/// Performs a comparison of the markup produced by the initial rendering of the
@@ -60,7 +58,7 @@ public interface IRenderedFragment
6058
/// <param name="cssSelector">The group of selectors to use.</param>
6159
public IElement Find(string cssSelector)
6260
{
63-
var result = GetNodes().QuerySelector(cssSelector);
61+
var result = Nodes.QuerySelector(cssSelector);
6462
if (result is null)
6563
throw new ElementNotFoundException(cssSelector);
6664
else
@@ -75,7 +73,7 @@ public IElement Find(string cssSelector)
7573
/// <param name="cssSelector">The group of selectors to use.</param>
7674
public IHtmlCollection<IElement> FindAll(string cssSelector)
7775
{
78-
return GetNodes().QuerySelectorAll(cssSelector);
76+
return Nodes.QuerySelectorAll(cssSelector);
7977
}
8078
}
8179
}

src/Rendering/RenderedComponent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public RenderedComponent(ITestContext testContext, RenderFragment renderFragment
4343
: base(testContext, renderFragment)
4444
{
4545
(ComponentId, Instance) = Container.GetComponent<TComponent>();
46-
FirstRenderMarkup = GetMarkup();
46+
FirstRenderMarkup = Markup;
4747
}
4848

4949
/// <inheritdoc/>

src/Rendering/RenderedFragment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class RenderedFragment : RenderedFragmentBase
2626
public RenderedFragment(ITestContext testContext, RenderFragment renderFragment)
2727
: base(testContext, renderFragment)
2828
{
29-
FirstRenderMarkup = GetMarkup();
29+
FirstRenderMarkup = Markup;
3030
}
3131
}
3232
}

src/Rendering/RenderedFragmentBase.cs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,28 @@ public abstract class RenderedFragmentBase : IRenderedFragment
3939
/// <inheritdoc/>
4040
public ITestContext TestContext { get; }
4141

42+
/// <inheritdoc/>
43+
public string Markup
44+
{
45+
get
46+
{
47+
if (_latestRenderMarkup is null)
48+
_latestRenderMarkup = Htmlizer.GetHtml(TestContext.Renderer, ComponentId);
49+
return _latestRenderMarkup;
50+
}
51+
}
52+
53+
/// <inheritdoc/>
54+
public INodeList Nodes
55+
{
56+
get
57+
{
58+
if (_latestRenderNodes is null)
59+
_latestRenderNodes = TestContext.CreateNodes(Markup);
60+
return _latestRenderNodes;
61+
}
62+
}
63+
4264
/// <summary>
4365
/// Creates an instance of the <see cref="RenderedFragmentBase"/> class.
4466
/// </summary>
@@ -56,7 +78,7 @@ public RenderedFragmentBase(ITestContext testContext, RenderFragment renderFragm
5678
public void SaveSnapshot()
5779
{
5880
_snapshotNodes = null;
59-
_snapshotMarkup = GetMarkup();
81+
_snapshotMarkup = Markup;
6082
}
6183

6284
/// <inheritdoc/>
@@ -65,10 +87,10 @@ public IReadOnlyList<IDiff> GetChangesSinceSnapshot()
6587
if (_snapshotMarkup is null)
6688
throw new InvalidOperationException($"No snapshot exists to compare with. Call {nameof(SaveSnapshot)} to create one.");
6789

68-
if(_snapshotNodes is null)
90+
if (_snapshotNodes is null)
6991
_snapshotNodes = TestContext.CreateNodes(_snapshotMarkup);
7092

71-
return GetNodes().CompareTo(_snapshotNodes);
93+
return Nodes.CompareTo(_snapshotNodes);
7294
}
7395

7496

@@ -77,24 +99,7 @@ public IReadOnlyList<IDiff> GetChangesSinceFirstRender()
7799
{
78100
if (_firstRenderNodes is null)
79101
_firstRenderNodes = TestContext.CreateNodes(FirstRenderMarkup);
80-
return GetNodes().CompareTo(_firstRenderNodes);
81-
}
82-
83-
84-
/// <inheritdoc/>
85-
public string GetMarkup()
86-
{
87-
if (_latestRenderMarkup is null)
88-
_latestRenderMarkup = Htmlizer.GetHtml(TestContext.Renderer, ComponentId);
89-
return _latestRenderMarkup;
90-
}
91-
92-
/// <inheritdoc/>
93-
public INodeList GetNodes()
94-
{
95-
if (_latestRenderNodes is null)
96-
_latestRenderNodes = TestContext.CreateNodes(GetMarkup());
97-
return _latestRenderNodes;
102+
return Nodes.CompareTo(_firstRenderNodes);
98103
}
99104

100105
private void ComponentMarkupChanged(in RenderBatch renderBatch)

0 commit comments

Comments
 (0)