Skip to content

Commit 102fd21

Browse files
committed
New render notification system and wait for helpers to solve the issue #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
1 parent 7f40a2c commit 102fd21

155 files changed

Lines changed: 8533 additions & 8484 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/bunit.core.tests/ComponentParameterBuilderTests.cs

Lines changed: 387 additions & 385 deletions
Large diffs are not rendered by default.
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
using System;
2+
3+
using Bunit.TestAssets.SampleComponents;
4+
5+
using Shouldly;
6+
7+
using Xunit;
8+
using Xunit.Abstractions;
9+
10+
namespace Bunit.Extensions.WaitForHelpers
11+
{
12+
public class RenderedFragmentWaitForHelperExtensionsTest : TestContext
13+
{
14+
public RenderedFragmentWaitForHelperExtensionsTest(ITestOutputHelper testOutput)
15+
{
16+
Services.AddXunitLogger(testOutput);
17+
}
18+
19+
[Fact(DisplayName = "WaitForAssertion can wait for multiple renders and changes to occur")]
20+
public void Test110()
21+
{
22+
// Initial state is stopped
23+
var cut = RenderComponent<TwoRendersTwoChanges>();
24+
var stateElement = cut.Find("#state");
25+
stateElement.TextContent.ShouldBe("Stopped");
26+
27+
// Clicking 'tick' changes the state, and starts a task
28+
cut.Find("#tick").Click();
29+
cut.Find("#state").TextContent.ShouldBe("Started");
30+
31+
// Clicking 'tock' completes the task, which updates the state
32+
// This click causes two renders, thus something is needed to await here.
33+
cut.Find("#tock").Click();
34+
cut.WaitForAssertion(
35+
() => cut.Find("#state").TextContent.ShouldBe("Stopped")
36+
);
37+
}
38+
39+
[Fact(DisplayName = "WaitForAssertion throws exception after timeout")]
40+
public void Test011()
41+
{
42+
var cut = RenderComponent<Simple1>();
43+
44+
var expected = Should.Throw<WaitForFailedException>(() =>
45+
cut.WaitForAssertion(() => cut.Markup.ShouldBeEmpty(), TimeSpan.FromMilliseconds(10))
46+
);
47+
48+
expected.Message.ShouldBe(WaitForAssertionHelper.TIMEOUT_MESSAGE);
49+
expected.InnerException.ShouldBeOfType<ShouldAssertException>();
50+
}
51+
52+
[Fact(DisplayName = "WaitForState throws exception after timeout")]
53+
public void Test012()
54+
{
55+
var cut = RenderComponent<Simple1>();
56+
57+
var expected = Should.Throw<WaitForFailedException>(() =>
58+
cut.WaitForState(() => string.IsNullOrEmpty(cut.Markup), TimeSpan.FromMilliseconds(100))
59+
);
60+
61+
expected.Message.ShouldBe(WaitForStateHelper.TIMEOUT_BEFORE_PASS);
62+
}
63+
64+
[Fact(DisplayName = "WaitForState throws exception if statePredicate throws on a later render")]
65+
public void Test013()
66+
{
67+
const string expectedInnerMessage = "INNER MESSAGE";
68+
var cut = RenderComponent<TwoRendersTwoChanges>();
69+
cut.Find("#tick").Click();
70+
cut.Find("#tock").Click();
71+
72+
var expected = Should.Throw<WaitForFailedException>(() =>
73+
cut.WaitForState(() =>
74+
{
75+
if (cut.Find("#state").TextContent == "Stopped")
76+
throw new InvalidOperationException(expectedInnerMessage);
77+
return false;
78+
})
79+
);
80+
81+
expected.Message.ShouldBe(WaitForStateHelper.EXCEPTION_IN_PREDICATE);
82+
expected.InnerException.ShouldBeOfType<InvalidOperationException>()
83+
.Message.ShouldBe(expectedInnerMessage);
84+
}
85+
86+
[Fact(DisplayName = "WaitForState can wait for multiple renders and changes to occur")]
87+
public void Test100()
88+
{
89+
// Initial state is stopped
90+
var cut = RenderComponent<TwoRendersTwoChanges>();
91+
var stateElement = cut.Find("#state");
92+
stateElement.TextContent.ShouldBe("Stopped");
93+
94+
// Clicking 'tick' changes the state, and starts a task
95+
cut.Find("#tick").Click();
96+
cut.Find("#state").TextContent.ShouldBe("Started");
97+
98+
// Clicking 'tock' completes the task, which updates the state
99+
// This click causes two renders, thus something is needed to await here.
100+
cut.Find("#tock").Click();
101+
cut.WaitForState(() => cut.Find("#state").TextContent == "Stopped");
102+
103+
cut.Find("#state").TextContent.ShouldBe("Stopped");
104+
}
105+
106+
[Fact(DisplayName = "WaitForState can detect async changes to properties in the CUT")]
107+
public void Test200()
108+
{
109+
var cut = RenderComponent<AsyncRenderChangesProperty>();
110+
cut.Instance.Counter.ShouldBe(0);
111+
112+
// Clicking 'tick' changes the counter, and starts a task
113+
cut.Find("#tick").Click();
114+
cut.Instance.Counter.ShouldBe(1);
115+
116+
// Clicking 'tock' completes the task, which updates the counter
117+
// This click causes two renders, thus something is needed to await here.
118+
cut.Find("#tock").Click();
119+
cut.WaitForState(() => cut.Instance.Counter == 2);
120+
121+
cut.Instance.Counter.ShouldBe(2);
122+
}
123+
}
124+
}
Lines changed: 57 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,73 @@
11
using System;
22
using System.Collections.Generic;
3+
34
using Bunit.Rendering;
5+
46
using Shouldly;
7+
58
using Xunit;
69

710
namespace Bunit
811
{
912
public class ComponentParameterTest
10-
{
11-
public static IEnumerable<object[]> GetEqualsTestData()
12-
{
13-
var name = "foo";
14-
var value = "bar";
15-
var p1 = ComponentParameter.CreateParameter(name, value);
16-
var p2 = ComponentParameter.CreateParameter(name, value);
17-
var p3 = ComponentParameter.CreateCascadingValue(name, value);
18-
var p4 = ComponentParameter.CreateParameter(string.Empty, value);
19-
var p5 = ComponentParameter.CreateParameter(name, string.Empty);
13+
{
14+
public static IEnumerable<object[]> GetEqualsTestData()
15+
{
16+
var name = "foo";
17+
var value = "bar";
18+
var p1 = ComponentParameter.CreateParameter(name, value);
19+
var p2 = ComponentParameter.CreateParameter(name, value);
20+
var p3 = ComponentParameter.CreateCascadingValue(name, value);
21+
var p4 = ComponentParameter.CreateParameter(string.Empty, value);
22+
var p5 = ComponentParameter.CreateParameter(name, string.Empty);
2023

21-
yield return new object[] { p1, p1, true };
22-
yield return new object[] { p1, p2, true };
23-
yield return new object[] { p3, p3, true };
24-
yield return new object[] { p1, p3, false };
25-
yield return new object[] { p1, p4, false };
26-
yield return new object[] { p1, p5, false };
27-
}
24+
yield return new object[] { p1, p1, true };
25+
yield return new object[] { p1, p2, true };
26+
yield return new object[] { p3, p3, true };
27+
yield return new object[] { p1, p3, false };
28+
yield return new object[] { p1, p4, false };
29+
yield return new object[] { p1, p5, false };
30+
}
2831

29-
[Fact(DisplayName = "Creating a cascading value throws")]
30-
public void Test001()
31-
{
32-
Should.Throw<ArgumentNullException>(() => ComponentParameter.CreateCascadingValue(null, null!));
33-
Should.Throw<ArgumentNullException>(() => { ComponentParameter p = (null, null, true); });
34-
}
32+
[Fact(DisplayName = "Creating a cascading value throws")]
33+
public void Test001()
34+
{
35+
Should.Throw<ArgumentNullException>(() => ComponentParameter.CreateCascadingValue(null, null!));
36+
Should.Throw<ArgumentNullException>(() => { ComponentParameter p = (null, null, true); });
37+
}
3538

36-
[Fact(DisplayName = "Creating a regular parameter without a name throws")]
37-
public void Test002()
38-
{
39-
Should.Throw<ArgumentNullException>(() => ComponentParameter.CreateParameter(null!, null));
40-
Should.Throw<ArgumentNullException>(() => { ComponentParameter p = (null, null, false); });
41-
}
39+
[Fact(DisplayName = "Creating a regular parameter without a name throws")]
40+
public void Test002()
41+
{
42+
Should.Throw<ArgumentNullException>(() => ComponentParameter.CreateParameter(null!, null));
43+
Should.Throw<ArgumentNullException>(() => { ComponentParameter p = (null, null, false); });
44+
}
4245

43-
[Theory(DisplayName = "Equals compares correctly")]
44-
[MemberData(nameof(GetEqualsTestData))]
45-
public void Test003(ComponentParameter left, ComponentParameter right, bool expectedResult)
46-
{
47-
left.Equals(right).ShouldBe(expectedResult);
48-
right.Equals(left).ShouldBe(expectedResult);
49-
(left == right).ShouldBe(expectedResult);
50-
(left != right).ShouldNotBe(expectedResult);
51-
left.Equals((object)right).ShouldBe(expectedResult);
52-
right.Equals((object)left).ShouldBe(expectedResult);
53-
}
46+
[Theory(DisplayName = "Equals compares correctly")]
47+
[MemberData(nameof(GetEqualsTestData))]
48+
public void Test003(ComponentParameter left, ComponentParameter right, bool expectedResult)
49+
{
50+
left.Equals(right).ShouldBe(expectedResult);
51+
right.Equals(left).ShouldBe(expectedResult);
52+
(left == right).ShouldBe(expectedResult);
53+
(left != right).ShouldNotBe(expectedResult);
54+
left.Equals((object)right).ShouldBe(expectedResult);
55+
right.Equals((object)left).ShouldBe(expectedResult);
56+
}
5457

55-
[Fact(DisplayName = "Equals operator works as expected with non compatible types")]
56-
public void Test004()
57-
{
58-
ComponentParameter.CreateParameter(string.Empty, string.Empty)
59-
.Equals(new object())
60-
.ShouldBeFalse();
61-
}
58+
[Fact(DisplayName = "Equals operator works as expected with non compatible types")]
59+
public void Test004()
60+
{
61+
ComponentParameter.CreateParameter(string.Empty, string.Empty)
62+
.Equals(new object())
63+
.ShouldBeFalse();
64+
}
6265

63-
[Theory(DisplayName = "GetHashCode returns same result for equal ComponentParameter")]
64-
[MemberData(nameof(GetEqualsTestData))]
65-
public void Test005(ComponentParameter left, ComponentParameter right, bool expectedResult)
66-
{
67-
left.GetHashCode().Equals(right.GetHashCode()).ShouldBe(expectedResult);
68-
}
69-
}
66+
[Theory(DisplayName = "GetHashCode returns same result for equal ComponentParameter")]
67+
[MemberData(nameof(GetEqualsTestData))]
68+
public void Test005(ComponentParameter left, ComponentParameter right, bool expectedResult)
69+
{
70+
left.GetHashCode().Equals(right.GetHashCode()).ShouldBe(expectedResult);
71+
}
72+
}
7073
}

src/bunit.core.tests/Rendering/RenderEvents/RenderEventPubSubTest.cs

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)