-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathFakePersistentComponentStateTest.cs
More file actions
90 lines (71 loc) · 2.48 KB
/
FakePersistentComponentStateTest.cs
File metadata and controls
90 lines (71 loc) · 2.48 KB
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
#if NET6_0_OR_GREATER
using System.Threading.Tasks;
using AutoFixture.Xunit2;
using Bunit.TestAssets.SampleComponents;
using Microsoft.AspNetCore.Components;
using Shouldly;
using Xunit;
using Xunit.Abstractions;
namespace Bunit.TestDoubles;
public class FakePersistentComponentStateTest : TestContext
{
public FakePersistentComponentStateTest(ITestOutputHelper outputHelper)
{
Services.AddXunitLogger(outputHelper);
}
[Fact(DisplayName = "AddFakePersistentComponentState is registers PersistentComponentState in services")]
public void Test001()
{
_ = this.AddFakePersistentComponentState();
var actual = Services.GetService<PersistentComponentState>();
actual.ShouldNotBeNull();
}
[Fact(DisplayName = "AddFakePersistentComponentState enables PersistentComponentState injection into components")]
public void Test002()
{
this.AddFakePersistentComponentState();
var cut = RenderComponent<PersistentComponentStateSample>();
cut.Instance.State.ShouldNotBeNull();
}
[Theory(DisplayName = "Persist stores state in store for components to consume")]
[AutoData]
public void Test011(string key, string data)
{
var fakeState = this.AddFakePersistentComponentState();
fakeState.Persist(key, data);
var store = Services.GetService<PersistentComponentState>();
store.TryTakeFromJson<string>(key, out var actual).ShouldBeTrue();
actual.ShouldBe(data);
}
[Fact(DisplayName = "TryTake returns true if key contains data saved in store")]
public void Test012()
{
var fakeState = this.AddFakePersistentComponentState();
var cut = RenderComponent<PersistentComponentStateSample>();
fakeState.TriggerOnPersisting();
fakeState.TryTake<WeatherForecast[]>(PersistentComponentStateSample.PersistenceKey, out var actual).ShouldBeTrue();
actual.ShouldBeEquivalentTo(cut.Instance.Forecasts);
}
[Theory(DisplayName = "TryTake returns false if key is not in store")]
[AutoData]
public void Test013(string key)
{
var fakeState = this.AddFakePersistentComponentState();
fakeState.TryTake<string>(key, out _).ShouldBeFalse();
}
[Fact(DisplayName = "TriggerOnPersisting triggers OnPersisting callbacks added to store")]
public void Test014()
{
var onPersistingCalledTimes = 0;
var fakeState = this.AddFakePersistentComponentState();
var store = Services.GetService<PersistentComponentState>();
store.RegisterOnPersisting(() =>
{
onPersistingCalledTimes++;
return Task.CompletedTask;
});
fakeState.TriggerOnPersisting();
onPersistingCalledTimes.ShouldBe(1);
}
}
#endif