-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStoreMiddlewareTests.swift
151 lines (141 loc) · 4.61 KB
/
StoreMiddlewareTests.swift
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
import Combine
import CombineExpectations
import Foundation
@testable import Recombine
import XCTest
class StoreMiddlewareTests: XCTestCase {
/**
it can decorate dispatch function
*/
func testDecorateMiddlewareDispatch() throws {
let store = Store(
state: TestFakes.StringTest.State(),
reducer: TestFakes.StringTest.reducer,
middleware: firstMiddleware.appending(secondMiddleware),
thunk: .init { _, _, _ in Empty().eraseToAnyPublisher() },
environment: (),
publishOn: ImmediateScheduler.shared
)
try nextEquals(
store,
actions: [
.sync(.string("OK")),
],
keyPath: \.value,
value: "OK First Middleware Second Middleware"
)
}
/**
it reruns for dispatching
*/
func testRedispatch() throws {
let middleware = Middleware<TestFakes.StringTest.State, TestFakes.SetAction.Async, TestFakes.SetAction.Sync, Void>({ _, action, dispatch, _ -> [TestFakes.SetAction.Sync] in
switch action {
case let .string(value):
if !value.contains("Middleware") {
dispatch(.sync(.string(value + " Middleware")))
return []
}
default:
break
}
return [action]
})
.debug("+++")
let store = Store(
state: TestFakes.StringTest.State(),
reducer: TestFakes.StringTest.reducer,
middleware: middleware,
thunk: .init { _, _, _ in Empty().eraseToAnyPublisher() },
environment: (),
publishOn: ImmediateScheduler.shared
)
try nextEquals(
store,
actions: [
.sync(.string("OK")),
],
keyPath: \.value,
value: "OK Middleware"
)
}
/**
it can decorate dispatch function
*/
func testDecorateThunkDispatch() throws {
let store = Store(
state: TestFakes.StringTest.State(),
reducer: TestFakes.StringTest.reducer,
middleware: .init(),
thunk: thunk,
environment: (),
publishOn: ImmediateScheduler.shared
)
try nextEquals(
store,
actions: [
.async(.first("OK")),
],
keyPath: \.value,
value: "OK First Thunk Second Thunk"
)
}
/**
it actions should be multiplied via the increase function
*/
func testMiddlewareMultiplies() throws {
let multiplexingMiddleware = Middleware<TestFakes.CounterTest.State, TestFakes.SetAction.Async, TestFakes.SetAction.Sync, Void>({ _, action, _, _ in
Array(repeating: action, count: 3)
})
let store = Store(
state: TestFakes.CounterTest.State(count: 0),
reducer: increaseByOneReducer,
middleware: multiplexingMiddleware,
thunk: .init { _, _, _ in Empty().eraseToAnyPublisher() },
environment: (),
publishOn: ImmediateScheduler.shared
)
try nextEquals(
store,
actions: [
.sync(.noop),
],
keyPath: \.count,
value: 3
)
}
/**
it actions should be multiplied via the increase function
*/
func testThunkMultiplies() throws {
let multiplexingThunk = Thunk<TestFakes.CounterTest.State, TestFakes.SetAction.Async, TestFakes.SetAction.Sync, Void> { _, action, _ -> AnyPublisher<EitherAction<TestFakes.SetAction.Async, TestFakes.SetAction.Sync>, Never> in
let transformed: TestFakes.SetAction.Sync
switch action {
case .noop:
transformed = .noop
case let .int(value):
transformed = .int(value)
case let .string(value):
transformed = .string(value)
}
return [transformed, transformed, transformed].publisher.map { .sync($0) }.eraseToAnyPublisher()
}
.debug(asyncAction: .self, syncAction: .self)
let store = Store(
state: TestFakes.CounterTest.State(count: 0),
reducer: increaseByOneReducer,
thunk: multiplexingThunk,
environment: (),
publishOn: ImmediateScheduler.shared
)
try prefixEquals(
store,
count: 3,
actions: [
.async(.noop),
],
keyPath: \.count,
values: [1, 2, 3]
)
}
}