-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStoreSpec.cs
162 lines (141 loc) · 5.28 KB
/
StoreSpec.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AsyncRedux.Tests.Mock;
using AsyncRedux.Tests.Mock.Actions;
using Shouldly;
using Xunit;
// ReSharper disable AssignNullToNotNullAttribute
// ReSharper disable MemberCanBePrivate.Global
namespace AsyncRedux.Tests
{
public class StoreSpec
{
public static IEnumerable<object[]> DispatchExamples
{
get
{
yield return new object[]
{
new State(0, false),
new object[] { },
new State(0, false)
};
yield return new object[]
{
new State(1, false),
new object[] { new ChangeBool(true) },
new State(1, true)
};
yield return new object[]
{
new State(100, true),
new object[] { new ChangeInt(50), new ChangeBool(false) },
new State(50, false)
};
yield return new object[]
{
new State(5, false),
new object[] { new ChangeInt(10), new ChangeBool(false), new ChangeInt(20) },
new State(20, false)
};
yield return new object[]
{
new State(5, false),
new[] { new ChangeInt(10), new ChangeBool(false), new object() },
new State(10, false)
};
}
}
public static IEnumerable<object[]> StateExamples
{
get
{
yield return new object[] { new State() };
yield return new object[] { new State(1, false) };
yield return new object[] { new State(0, true) };
yield return new object[] { new State(1, true) };
}
}
[Fact]
internal void Store_Construction_Should_Throw_For_Null_Middleware_Collection()
{
Should.Throw<ArgumentNullException>(
() => StoreSetup.CreateStore<State>()
.FromReducer(Reducers.PassThrough)
.UsingMiddleware(null)
.Build());
}
[Fact]
internal void Store_Construction_Should_Throw_If_Any_Middleware_Is_Null()
{
Should.Throw<ArgumentException>(
() => StoreSetup.CreateStore<State>()
.FromReducer(Reducers.PassThrough)
.UsingMiddleware(Mock.Middleware.IncrementInt, null)
.Build());
}
[Fact]
internal void Store_Construction_Should_Throw_If_Reducer_Is_Not_Specified()
{
Should.Throw<InvalidOperationException>(() => StoreSetup.CreateStore<State>().Build());
}
[Theory]
[MemberData(nameof(DispatchExamples))]
internal async Task Store_Should_Apply_Actions_In_Order_Of_Dispatch(
State initialState,
object[] actions,
State expectedFinalState)
{
var store = StoreSetup.CreateStore<State>()
.FromReducer(Reducers.Replace)
.WithInitialState(initialState)
.Build();
foreach (var action in actions)
{
await store.Dispatch(action);
}
store.State.ShouldBe(expectedFinalState);
}
[Fact]
internal void Store_Should_Concatenate_Middleware_If_Multiple_Sets_Provided_During_Construction()
{
var initialState = new State(0, false);
var store = StoreSetup.CreateStore<State>()
.FromReducer(Reducers.Replace)
.WithInitialState(initialState)
.UsingMiddleware(Mock.Middleware.IncrementInt)
.UsingMiddleware(Mock.Middleware.NegateBool)
.Build();
store.Dispatch(new ChangeInt(1));
store.State.IntProperty.ShouldBe(2);
store.Dispatch(new ChangeBool(false));
store.State.BoolProperty.ShouldBe(true);
}
[Theory]
[MemberData(nameof(StateExamples))]
internal void Store_Should_Have_Provided_Initial_State_After_Construction(State initialState)
{
var store = StoreSetup.CreateStore<State>()
.FromReducer(Reducers.Replace)
.WithInitialState(initialState)
.Build();
store.State.ShouldBe(initialState);
}
[Fact]
internal void Store_Should_Pass_Dispatched_Actions_Through_Middleware_Before_Reducing()
{
var initialState = new State(0, false);
var middleware = new Middleware<State>[] { Mock.Middleware.IncrementInt, Mock.Middleware.NegateBool };
var store = StoreSetup.CreateStore<State>()
.FromReducer(Reducers.Replace)
.WithInitialState(initialState)
.UsingMiddleware(middleware)
.Build();
store.Dispatch(new ChangeInt(1));
store.State.IntProperty.ShouldBe(2);
store.Dispatch(new ChangeBool(false));
store.State.BoolProperty.ShouldBe(true);
}
}
}