Skip to content

Change GetNodes and GetMarkup to Nodes and Markup properties in IRenderedFragment #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
void Test()
{
var cut = GetComponentUnderTest<ThemedButton>();
var x = cut.GetMarkup();
cut.Find("button").ClassList.ShouldContain("btn");
}
}
5 changes: 2 additions & 3 deletions src/Asserting/CompareToDiffingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ public static IReadOnlyList<IDiff> CompareTo(this IRenderedFragment actual, stri
if (actual is null) throw new ArgumentNullException(nameof(actual));
if (expected is null) throw new ArgumentNullException(nameof(expected));

var actualNodes = actual.GetNodes();
var expectedNodes = actual.TestContext.CreateNodes(expected);

return actualNodes.CompareTo(expectedNodes);
return actual.Nodes.CompareTo(expectedNodes);
}

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

return actual.GetNodes().CompareTo(expected.GetNodes());
return actual.Nodes.CompareTo(expected.Nodes);
}

/// <summary>
Expand Down
9 changes: 4 additions & 5 deletions src/Asserting/MarkupMatchesAssertExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ public static void MarkupMatches(this IRenderedFragment actual, string expected,
if (actual is null) throw new ArgumentNullException(nameof(actual));
if (expected is null) throw new ArgumentNullException(nameof(expected));

var actualNodes = actual.GetNodes();
var expectedNodes = actual.TestContext.CreateNodes(expected);

actualNodes.MarkupMatches(expectedNodes, userMessage);
actual.Nodes.MarkupMatches(expectedNodes, userMessage);
}

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

actual.GetNodes().MarkupMatches(expected.GetNodes(), userMessage);
actual.Nodes.MarkupMatches(expected.Nodes, userMessage);
}

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

actual.MarkupMatches(expected.GetNodes(), userMessage);
actual.MarkupMatches(expected.Nodes, userMessage);
}

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

actual.MarkupMatches(expected.GetNodes(), userMessage);
actual.MarkupMatches(expected.Nodes, userMessage);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Asserting/ShouldBeAdditionAssertExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static void ShouldBeAddition(this IDiff actualChange, string expectedChan
public static void ShouldBeAddition(this IDiff actualChange, IRenderedFragment expectedChange, string? userMessage = null)
{
if (expectedChange is null) throw new ArgumentNullException(nameof(expectedChange));
ShouldBeAddition(actualChange, expectedChange.GetNodes(), userMessage);
ShouldBeAddition(actualChange, expectedChange.Nodes, userMessage);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Asserting/ShouldBeRemovalAssertExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static void ShouldBeRemoval(this IDiff actualChange, string expectedChang
public static void ShouldBeRemoval(this IDiff actualChange, IRenderedFragment expectedChange, string? userMessage = null)
{
if (expectedChange is null) throw new ArgumentNullException(nameof(expectedChange));
ShouldBeRemoval(actualChange, expectedChange.GetNodes(), userMessage);
ShouldBeRemoval(actualChange, expectedChange.Nodes, userMessage);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Asserting/ShouldBeTextChangeAssertExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void ShouldBeTextChange(this IDiff actualChange, string expectedCh
public static void ShouldBeTextChange(this IDiff actualChange, IRenderedFragment expectedChange, string? userMessage = null)
{
if (expectedChange is null) throw new ArgumentNullException(nameof(expectedChange));
ShouldBeTextChange(actualChange, expectedChange.GetNodes(), userMessage);
ShouldBeTextChange(actualChange, expectedChange.Nodes, userMessage);
}

public static void ShouldBeTextChange(this IDiff actualChange, INodeList expectedChange, string? userMessage = null)
Expand Down
10 changes: 4 additions & 6 deletions src/Rendering/IRenderedFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ public interface IRenderedFragment
/// <summary>
/// Gets the HTML markup from the rendered fragment/component.
/// </summary>
/// <returns></returns>
string GetMarkup();
string Markup { get; }

/// <summary>
/// Gets the AngleSharp <see cref="INodeList"/> based
/// on the HTML markup from the rendered fragment/component.
/// </summary>
/// <returns></returns>
INodeList GetNodes();
INodeList Nodes { get; }

/// <summary>
/// Performs a comparison of the markup produced by the initial rendering of the
Expand Down Expand Up @@ -60,7 +58,7 @@ public interface IRenderedFragment
/// <param name="cssSelector">The group of selectors to use.</param>
public IElement Find(string cssSelector)
{
var result = GetNodes().QuerySelector(cssSelector);
var result = Nodes.QuerySelector(cssSelector);
if (result is null)
throw new ElementNotFoundException(cssSelector);
else
Expand All @@ -75,7 +73,7 @@ public IElement Find(string cssSelector)
/// <param name="cssSelector">The group of selectors to use.</param>
public IHtmlCollection<IElement> FindAll(string cssSelector)
{
return GetNodes().QuerySelectorAll(cssSelector);
return Nodes.QuerySelectorAll(cssSelector);
}
}
}
2 changes: 1 addition & 1 deletion src/Rendering/RenderedComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public RenderedComponent(ITestContext testContext, RenderFragment renderFragment
: base(testContext, renderFragment)
{
(ComponentId, Instance) = Container.GetComponent<TComponent>();
FirstRenderMarkup = GetMarkup();
FirstRenderMarkup = Markup;
}

/// <inheritdoc/>
Expand Down
2 changes: 1 addition & 1 deletion src/Rendering/RenderedFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class RenderedFragment : RenderedFragmentBase
public RenderedFragment(ITestContext testContext, RenderFragment renderFragment)
: base(testContext, renderFragment)
{
FirstRenderMarkup = GetMarkup();
FirstRenderMarkup = Markup;
}
}
}
47 changes: 26 additions & 21 deletions src/Rendering/RenderedFragmentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@ public abstract class RenderedFragmentBase : IRenderedFragment
/// <inheritdoc/>
public ITestContext TestContext { get; }

/// <inheritdoc/>
public string Markup
{
get
{
if (_latestRenderMarkup is null)
_latestRenderMarkup = Htmlizer.GetHtml(TestContext.Renderer, ComponentId);
return _latestRenderMarkup;
}
}

/// <inheritdoc/>
public INodeList Nodes
{
get
{
if (_latestRenderNodes is null)
_latestRenderNodes = TestContext.CreateNodes(Markup);
return _latestRenderNodes;
}
}

/// <summary>
/// Creates an instance of the <see cref="RenderedFragmentBase"/> class.
/// </summary>
Expand All @@ -56,7 +78,7 @@ public RenderedFragmentBase(ITestContext testContext, RenderFragment renderFragm
public void SaveSnapshot()
{
_snapshotNodes = null;
_snapshotMarkup = GetMarkup();
_snapshotMarkup = Markup;
}

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

if(_snapshotNodes is null)
if (_snapshotNodes is null)
_snapshotNodes = TestContext.CreateNodes(_snapshotMarkup);

return GetNodes().CompareTo(_snapshotNodes);
return Nodes.CompareTo(_snapshotNodes);
}


Expand All @@ -77,24 +99,7 @@ public IReadOnlyList<IDiff> GetChangesSinceFirstRender()
{
if (_firstRenderNodes is null)
_firstRenderNodes = TestContext.CreateNodes(FirstRenderMarkup);
return GetNodes().CompareTo(_firstRenderNodes);
}


/// <inheritdoc/>
public string GetMarkup()
{
if (_latestRenderMarkup is null)
_latestRenderMarkup = Htmlizer.GetHtml(TestContext.Renderer, ComponentId);
return _latestRenderMarkup;
}

/// <inheritdoc/>
public INodeList GetNodes()
{
if (_latestRenderNodes is null)
_latestRenderNodes = TestContext.CreateNodes(GetMarkup());
return _latestRenderNodes;
return Nodes.CompareTo(_firstRenderNodes);
}

private void ComponentMarkupChanged(in RenderBatch renderBatch)
Expand Down
7 changes: 3 additions & 4 deletions tests/Asserting/CompareToDiffingExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void Test002()
var rf1 = RenderComponent<Simple1>((nameof(Simple1.Header), "FOO"));
var rf2 = RenderComponent<Simple1>((nameof(Simple1.Header), "BAR"));

rf1.CompareTo(rf2.GetMarkup()).Count.ShouldBe(1);
rf1.CompareTo(rf2.Markup).Count.ShouldBe(1);
}

[Fact(DisplayName = "CompareTo with rendered fragment and rendered fragment")]
Expand All @@ -73,18 +73,17 @@ public void Test004()
var rf2 = RenderComponent<Simple1>((nameof(Simple1.Header), "BAR"));

var elm = rf1.Find("h1");
elm.CompareTo(rf2.GetNodes()).Count.ShouldBe(1);
elm.CompareTo(rf2.Nodes).Count.ShouldBe(1);
}


[Fact(DisplayName = "CompareTo with INodeList and INode")]
public void Test005()
{
var rf1 = RenderComponent<Simple1>((nameof(Simple1.Header), "FOO"));
var rf2 = RenderComponent<Simple1>((nameof(Simple1.Header), "BAR"));

var elm = rf1.Find("h1");
rf2.GetNodes().CompareTo(elm).Count.ShouldBe(1);
rf2.Nodes.CompareTo(elm).Count.ShouldBe(1);
}
}
}
8 changes: 4 additions & 4 deletions tests/ComponentTestFixtureTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public void Test001()
instance.NamedCascadingValue.ShouldBe(1337);
Should.Throw<Exception>(async () => await instance.NonGenericCallback.InvokeAsync(null)).Message.ShouldBe("NonGenericCallback");
Should.Throw<Exception>(async () => await instance.GenericCallback.InvokeAsync(EventArgs.Empty)).Message.ShouldBe("GenericCallback");
new RenderedFragment(this, instance.ChildContent!).GetMarkup().ShouldBe(nameof(ChildContent));
new RenderedFragment(this, instance.OtherContent!).GetMarkup().ShouldBe(nameof(AllTypesOfParams<string>.OtherContent));
new RenderedFragment(this, instance.ChildContent!).Markup.ShouldBe(nameof(ChildContent));
new RenderedFragment(this, instance.OtherContent!).Markup.ShouldBe(nameof(AllTypesOfParams<string>.OtherContent));
Should.Throw<Exception>(() => instance.ItemTemplate!("")(null)).Message.ShouldBe("ItemTemplate");
}

Expand Down Expand Up @@ -78,8 +78,8 @@ public void Test002()
instance.RegularParam.ShouldBe("some value");
Should.Throw<Exception>(async () => await instance.NonGenericCallback.InvokeAsync(null)).Message.ShouldBe("NonGenericCallback");
Should.Throw<Exception>(async () => await instance.GenericCallback.InvokeAsync(EventArgs.Empty)).Message.ShouldBe("GenericCallback");
new RenderedFragment(this, instance.ChildContent!).GetMarkup().ShouldBe(nameof(ChildContent));
new RenderedFragment(this, instance.OtherContent!).GetMarkup().ShouldBe(nameof(AllTypesOfParams<string>.OtherContent));
new RenderedFragment(this, instance.ChildContent!).Markup.ShouldBe(nameof(ChildContent));
new RenderedFragment(this, instance.OtherContent!).Markup.ShouldBe(nameof(AllTypesOfParams<string>.OtherContent));
Should.Throw<Exception>(() => instance.ItemTemplate!("")(null)).Message.ShouldBe("ItemTemplate");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{
var cut = GetFragment();

var html = cut.GetMarkup();
var html = cut.Markup;

html.ShouldContain($"=\"{refElm.Id}\"");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
var cut2 = GetComponentUnderTest<Wrapper>();

Assert.True(ReferenceEquals(cut1, cut2), "Getting CUT multiple times should return the same instance");
Assert.Equal("CUT", cut1.GetMarkup());
Assert.Equal("CUT", cut1.Markup);

var firstFragmentNoId1 = GetFragment();
var firstFragmentId1 = GetFragment("first");
Expand All @@ -25,13 +25,13 @@
Assert.True(ReferenceEquals(firstFragmentNoId1, firstFragmentId1), "Getting first fragment with and without id should return the same instance");
Assert.True(ReferenceEquals(firstFragmentNoId1, firstFragmentNoId2), "Getting first fragment multiple times should return the same instance");
Assert.True(ReferenceEquals(firstFragmentId1, firstFragmentId2), "Getting first fragment multiple times should return the same instance");
Assert.Equal("first", firstFragmentNoId1.GetMarkup());
Assert.Equal("first", firstFragmentNoId1.Markup);

var secondFragmentId1 = GetFragment<Wrapper>("second");
var secondFragmentId2 = GetFragment<Wrapper>("second");

Assert.True(ReferenceEquals(secondFragmentId1, secondFragmentId2), "Getting fragment multiple times should return the same instance");
Assert.Equal("second", secondFragmentId2.GetMarkup());
Assert.Equal("second", secondFragmentId2.Markup);
}
}

Expand Down
24 changes: 12 additions & 12 deletions tests/RenderComponentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,54 @@ namespace Egil.RazorComponents.Testing
{
public class RenderComponentTest : ComponentTestFixture
{
[Fact(DisplayName = "GetNodes should return the same instance " +
[Fact(DisplayName = "Nodes should return the same instance " +
"when a render has not resulted in any changes")]
public void Test003()
{
var cut = RenderComponent<Wrapper>(ChildContent("<div>"));
var initialNodes = cut.GetNodes();
var initialNodes = cut.Nodes;

cut.Render();
cut.SetParametersAndRender(ChildContent("<div>"));

Assert.Same(initialNodes, cut.GetNodes());
Assert.Same(initialNodes, cut.Nodes);
}

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

cut.SetParametersAndRender(ChildContent("<p>"));

Assert.NotSame(initialNodes, cut.GetNodes());
Assert.NotSame(initialNodes, cut.Nodes);
cut.Find("p").ShouldNotBeNull();
}

[Fact(DisplayName = "GetNodes should return new instance " +
[Fact(DisplayName = "Nodes should return new instance " +
"when a Render has caused changes to DOM tree")]
public void Tets005()
{
var cut = RenderComponent<RenderCounter>();
var initialNodes = cut.GetNodes();
var initialNodes = cut.Nodes;

cut.Render();

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

[Fact(DisplayName = "GetNodes should return new instance " +
[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.GetNodes();
var initialNodes = cut.Nodes;

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

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


Expand Down
Loading