forked from bUnit-dev/bUnit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComponentTestFixtureTest.cs
110 lines (98 loc) · 6.23 KB
/
ComponentTestFixtureTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using Xunit;
using Shouldly;
using Bunit.SampleComponents;
using System.Diagnostics.CodeAnalysis;
using Bunit.Mocking.JSInterop;
namespace Bunit
{
[SuppressMessage("Reliability", "CA2007:Consider calling ConfigureAwait on the awaited task", Justification = "<Pending>")]
public class ComponentTestFixtureTest : ComponentTestFixture
{
[Fact(DisplayName = "All types of parameters are correctly assigned to component on render")]
public void Test001()
{
Services.AddMockJsRuntime();
var cut = RenderComponent<AllTypesOfParams<string>>(
("some-unmatched-attribute", "unmatched value"),
(nameof(AllTypesOfParams<string>.RegularParam), "some value"),
CascadingValue(42),
CascadingValue(nameof(AllTypesOfParams<string>.NamedCascadingValue), 1337),
EventCallback(nameof(AllTypesOfParams<string>.NonGenericCallback), () => throw new Exception("NonGenericCallback")),
EventCallback(nameof(AllTypesOfParams<string>.GenericCallback), (EventArgs args) => throw new Exception("GenericCallback")),
ChildContent(nameof(ChildContent)),
RenderFragment(nameof(AllTypesOfParams<string>.OtherContent), nameof(AllTypesOfParams<string>.OtherContent)),
Template<string>(nameof(AllTypesOfParams<string>.ItemTemplate), (item) => (builder) => throw new Exception("ItemTemplate"))
);
// assert that all parameters have been set correctly
var instance = cut.Instance;
instance.Attributes["some-unmatched-attribute"].ShouldBe("unmatched value");
instance.RegularParam.ShouldBe("some value");
instance.UnnamedCascadingValue.ShouldBe(42);
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!).Markup.ShouldBe(nameof(ChildContent));
new RenderedFragment(this, instance.OtherContent!).Markup.ShouldBe(nameof(AllTypesOfParams<string>.OtherContent));
Should.Throw<Exception>(() => instance.ItemTemplate!("")(null)).Message.ShouldBe("ItemTemplate");
}
[Fact(DisplayName = "All types of parameters are correctly assigned to component on re-render")]
public void Test002()
{
// arrange
Services.AddMockJsRuntime();
var cut = RenderComponent<AllTypesOfParams<string>>();
// assert that no parameters have been set initially
var instance = cut.Instance;
instance.Attributes.ShouldBeNull();
instance.RegularParam.ShouldBeNull();
instance.UnnamedCascadingValue.ShouldBeNull();
instance.NamedCascadingValue.ShouldBeNull();
instance.NonGenericCallback.HasDelegate.ShouldBeFalse();
instance.GenericCallback.HasDelegate.ShouldBeFalse();
instance.ChildContent.ShouldBeNull();
instance.OtherContent.ShouldBeNull();
instance.ItemTemplate.ShouldBeNull();
// act - set components params and render
cut.SetParametersAndRender(
("some-unmatched-attribute", "unmatched value"),
(nameof(AllTypesOfParams<string>.RegularParam), "some value"),
EventCallback(nameof(AllTypesOfParams<string>.NonGenericCallback), () => throw new Exception("NonGenericCallback")),
EventCallback(nameof(AllTypesOfParams<string>.GenericCallback), (EventArgs args) => throw new Exception("GenericCallback")),
ChildContent<Wrapper>(ChildContent(nameof(ChildContent))),
RenderFragment<Wrapper>(nameof(AllTypesOfParams<string>.OtherContent), ChildContent(nameof(AllTypesOfParams<string>.OtherContent))),
Template<string>(nameof(AllTypesOfParams<string>.ItemTemplate), (item) => (builder) => throw new Exception("ItemTemplate"))
);
instance.Attributes["some-unmatched-attribute"].ShouldBe("unmatched value");
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!).Markup.ShouldBe(nameof(ChildContent));
new RenderedFragment(this, instance.OtherContent!).Markup.ShouldBe(nameof(AllTypesOfParams<string>.OtherContent));
Should.Throw<Exception>(() => instance.ItemTemplate!("")(null)).Message.ShouldBe("ItemTemplate");
}
[Fact(DisplayName = "Trying to set CascadingValue during SetParametersAndRender throws")]
public void Test003()
{
// arrange
Services.AddMockJsRuntime();
var cut = RenderComponent<AllTypesOfParams<string>>();
// assert
Should.Throw<InvalidOperationException>(() => cut.SetParametersAndRender(CascadingValue(42)));
Should.Throw<InvalidOperationException>(() => cut.SetParametersAndRender(CascadingValue(nameof(AllTypesOfParams<string>.NamedCascadingValue), 1337)));
}
[Fact(DisplayName = "Template(name, markupFactory) helper correctly renders markup template")]
public void Test100()
{
var cut = RenderComponent<SimpleWithTemplate<int>>(
(nameof(SimpleWithTemplate<int>.Data), new int[] { 1, 2 }),
Template<int>(nameof(SimpleWithTemplate<int>.Template), num => $"<p>{num}</p>")
);
var expected = RenderComponent<SimpleWithTemplate<int>>(
(nameof(SimpleWithTemplate<int>.Data), new int[] { 1, 2 }),
Template<int>(nameof(SimpleWithTemplate<int>.Template), num => builder => builder.AddMarkupContent(0, $"<p>{num}</p>"))
);
cut.MarkupMatches(expected);
}
}
}