Skip to content

Commit

Permalink
New render notification system and wait for helpers to solve the issue
Browse files Browse the repository at this point in the history
…#118

Squashed commit of the following:
commit e8bde9b
Author: Egil Hansen <egil@assimilated.dk>
Date:   Fri May 8 11:31:40 2020 +0000

    Fix to code doc

commit 833e67d
Author: Egil Hansen <egil@assimilated.dk>
Date:   Fri May 8 11:29:22 2020 +0000

    Code clean up

commit 5f3b969
Author: Egil Hansen <egil@assimilated.dk>
Date:   Fri May 8 11:12:13 2020 +0000

    Removed unused using statements and reordered

commit 2c809e1
Author: Egil Hansen <egil@assimilated.dk>
Date:   Fri May 8 11:10:04 2020 +0000

    Tweaks to code, simpler and general WaitForHelper class

commit fbf0b2c
Author: Egil Hansen <egil@assimilated.dk>
Date:   Fri May 8 08:50:56 2020 +0000

    Added additional test for testing changes to properties in components

commit 0969f9d
Author: Egil Hansen <egil@assimilated.dk>
Date:   Thu May 7 17:48:25 2020 +0000

    Removed test context waitfor functionality

commit ff48f3d
Author: Egil Hansen <egil@assimilated.dk>
Date:   Thu May 7 17:11:01 2020 +0000

    Added time stamp to log, and wrapped in try/catch, to handle scenarios where a message arrives after the test run is over.

commit 46176c3
Author: Egil Hansen <egil@assimilated.dk>
Date:   Thu May 7 10:53:40 2020 +0000

    Moved to wait helper classes with locking and better protection for race conditions

commit 98bfb08
Author: Egil Hansen <egil@assimilated.dk>
Date:   Wed May 6 23:52:02 2020 +0000

    Attempt at reworking notification of render events and provding more stable logic around waiting for functionality
  • Loading branch information
egil committed May 8, 2020
1 parent 7f40a2c commit 102fd21
Show file tree
Hide file tree
Showing 155 changed files with 8,533 additions and 8,484 deletions.
772 changes: 387 additions & 385 deletions src/bunit.core.tests/ComponentParameterBuilderTests.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;

using Bunit.TestAssets.SampleComponents;

using Shouldly;

using Xunit;
using Xunit.Abstractions;

namespace Bunit.Extensions.WaitForHelpers
{
public class RenderedFragmentWaitForHelperExtensionsTest : TestContext
{
public RenderedFragmentWaitForHelperExtensionsTest(ITestOutputHelper testOutput)
{
Services.AddXunitLogger(testOutput);
}

[Fact(DisplayName = "WaitForAssertion can wait for multiple renders and changes to occur")]
public void Test110()
{
// Initial state is stopped
var cut = RenderComponent<TwoRendersTwoChanges>();
var stateElement = cut.Find("#state");
stateElement.TextContent.ShouldBe("Stopped");

// Clicking 'tick' changes the state, and starts a task
cut.Find("#tick").Click();
cut.Find("#state").TextContent.ShouldBe("Started");

// Clicking 'tock' completes the task, which updates the state
// This click causes two renders, thus something is needed to await here.
cut.Find("#tock").Click();
cut.WaitForAssertion(
() => cut.Find("#state").TextContent.ShouldBe("Stopped")
);
}

[Fact(DisplayName = "WaitForAssertion throws exception after timeout")]
public void Test011()
{
var cut = RenderComponent<Simple1>();

var expected = Should.Throw<WaitForFailedException>(() =>
cut.WaitForAssertion(() => cut.Markup.ShouldBeEmpty(), TimeSpan.FromMilliseconds(10))
);

expected.Message.ShouldBe(WaitForAssertionHelper.TIMEOUT_MESSAGE);
expected.InnerException.ShouldBeOfType<ShouldAssertException>();
}

[Fact(DisplayName = "WaitForState throws exception after timeout")]
public void Test012()
{
var cut = RenderComponent<Simple1>();

var expected = Should.Throw<WaitForFailedException>(() =>
cut.WaitForState(() => string.IsNullOrEmpty(cut.Markup), TimeSpan.FromMilliseconds(100))
);

expected.Message.ShouldBe(WaitForStateHelper.TIMEOUT_BEFORE_PASS);
}

[Fact(DisplayName = "WaitForState throws exception if statePredicate throws on a later render")]
public void Test013()
{
const string expectedInnerMessage = "INNER MESSAGE";
var cut = RenderComponent<TwoRendersTwoChanges>();
cut.Find("#tick").Click();
cut.Find("#tock").Click();

var expected = Should.Throw<WaitForFailedException>(() =>
cut.WaitForState(() =>
{
if (cut.Find("#state").TextContent == "Stopped")
throw new InvalidOperationException(expectedInnerMessage);
return false;
})
);

expected.Message.ShouldBe(WaitForStateHelper.EXCEPTION_IN_PREDICATE);
expected.InnerException.ShouldBeOfType<InvalidOperationException>()
.Message.ShouldBe(expectedInnerMessage);
}

[Fact(DisplayName = "WaitForState can wait for multiple renders and changes to occur")]
public void Test100()
{
// Initial state is stopped
var cut = RenderComponent<TwoRendersTwoChanges>();
var stateElement = cut.Find("#state");
stateElement.TextContent.ShouldBe("Stopped");

// Clicking 'tick' changes the state, and starts a task
cut.Find("#tick").Click();
cut.Find("#state").TextContent.ShouldBe("Started");

// Clicking 'tock' completes the task, which updates the state
// This click causes two renders, thus something is needed to await here.
cut.Find("#tock").Click();
cut.WaitForState(() => cut.Find("#state").TextContent == "Stopped");

cut.Find("#state").TextContent.ShouldBe("Stopped");
}

[Fact(DisplayName = "WaitForState can detect async changes to properties in the CUT")]
public void Test200()
{
var cut = RenderComponent<AsyncRenderChangesProperty>();
cut.Instance.Counter.ShouldBe(0);

// Clicking 'tick' changes the counter, and starts a task
cut.Find("#tick").Click();
cut.Instance.Counter.ShouldBe(1);

// Clicking 'tock' completes the task, which updates the counter
// This click causes two renders, thus something is needed to await here.
cut.Find("#tock").Click();
cut.WaitForState(() => cut.Instance.Counter == 2);

cut.Instance.Counter.ShouldBe(2);
}
}
}
111 changes: 57 additions & 54 deletions src/bunit.core.tests/Rendering/ComponentParameterTest.cs
Original file line number Diff line number Diff line change
@@ -1,70 +1,73 @@
using System;
using System.Collections.Generic;

using Bunit.Rendering;

using Shouldly;

using Xunit;

namespace Bunit
{
public class ComponentParameterTest
{
public static IEnumerable<object[]> GetEqualsTestData()
{
var name = "foo";
var value = "bar";
var p1 = ComponentParameter.CreateParameter(name, value);
var p2 = ComponentParameter.CreateParameter(name, value);
var p3 = ComponentParameter.CreateCascadingValue(name, value);
var p4 = ComponentParameter.CreateParameter(string.Empty, value);
var p5 = ComponentParameter.CreateParameter(name, string.Empty);
{
public static IEnumerable<object[]> GetEqualsTestData()
{
var name = "foo";
var value = "bar";
var p1 = ComponentParameter.CreateParameter(name, value);
var p2 = ComponentParameter.CreateParameter(name, value);
var p3 = ComponentParameter.CreateCascadingValue(name, value);
var p4 = ComponentParameter.CreateParameter(string.Empty, value);
var p5 = ComponentParameter.CreateParameter(name, string.Empty);

yield return new object[] { p1, p1, true };
yield return new object[] { p1, p2, true };
yield return new object[] { p3, p3, true };
yield return new object[] { p1, p3, false };
yield return new object[] { p1, p4, false };
yield return new object[] { p1, p5, false };
}
yield return new object[] { p1, p1, true };
yield return new object[] { p1, p2, true };
yield return new object[] { p3, p3, true };
yield return new object[] { p1, p3, false };
yield return new object[] { p1, p4, false };
yield return new object[] { p1, p5, false };
}

[Fact(DisplayName = "Creating a cascading value throws")]
public void Test001()
{
Should.Throw<ArgumentNullException>(() => ComponentParameter.CreateCascadingValue(null, null!));
Should.Throw<ArgumentNullException>(() => { ComponentParameter p = (null, null, true); });
}
[Fact(DisplayName = "Creating a cascading value throws")]
public void Test001()
{
Should.Throw<ArgumentNullException>(() => ComponentParameter.CreateCascadingValue(null, null!));
Should.Throw<ArgumentNullException>(() => { ComponentParameter p = (null, null, true); });
}

[Fact(DisplayName = "Creating a regular parameter without a name throws")]
public void Test002()
{
Should.Throw<ArgumentNullException>(() => ComponentParameter.CreateParameter(null!, null));
Should.Throw<ArgumentNullException>(() => { ComponentParameter p = (null, null, false); });
}
[Fact(DisplayName = "Creating a regular parameter without a name throws")]
public void Test002()
{
Should.Throw<ArgumentNullException>(() => ComponentParameter.CreateParameter(null!, null));
Should.Throw<ArgumentNullException>(() => { ComponentParameter p = (null, null, false); });
}

[Theory(DisplayName = "Equals compares correctly")]
[MemberData(nameof(GetEqualsTestData))]
public void Test003(ComponentParameter left, ComponentParameter right, bool expectedResult)
{
left.Equals(right).ShouldBe(expectedResult);
right.Equals(left).ShouldBe(expectedResult);
(left == right).ShouldBe(expectedResult);
(left != right).ShouldNotBe(expectedResult);
left.Equals((object)right).ShouldBe(expectedResult);
right.Equals((object)left).ShouldBe(expectedResult);
}
[Theory(DisplayName = "Equals compares correctly")]
[MemberData(nameof(GetEqualsTestData))]
public void Test003(ComponentParameter left, ComponentParameter right, bool expectedResult)
{
left.Equals(right).ShouldBe(expectedResult);
right.Equals(left).ShouldBe(expectedResult);
(left == right).ShouldBe(expectedResult);
(left != right).ShouldNotBe(expectedResult);
left.Equals((object)right).ShouldBe(expectedResult);
right.Equals((object)left).ShouldBe(expectedResult);
}

[Fact(DisplayName = "Equals operator works as expected with non compatible types")]
public void Test004()
{
ComponentParameter.CreateParameter(string.Empty, string.Empty)
.Equals(new object())
.ShouldBeFalse();
}
[Fact(DisplayName = "Equals operator works as expected with non compatible types")]
public void Test004()
{
ComponentParameter.CreateParameter(string.Empty, string.Empty)
.Equals(new object())
.ShouldBeFalse();
}

[Theory(DisplayName = "GetHashCode returns same result for equal ComponentParameter")]
[MemberData(nameof(GetEqualsTestData))]
public void Test005(ComponentParameter left, ComponentParameter right, bool expectedResult)
{
left.GetHashCode().Equals(right.GetHashCode()).ShouldBe(expectedResult);
}
}
[Theory(DisplayName = "GetHashCode returns same result for equal ComponentParameter")]
[MemberData(nameof(GetEqualsTestData))]
public void Test005(ComponentParameter left, ComponentParameter right, bool expectedResult)
{
left.GetHashCode().Equals(right.GetHashCode()).ShouldBe(expectedResult);
}
}
}

This file was deleted.

Loading

0 comments on commit 102fd21

Please sign in to comment.