-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathWorkflowRenderTester.swift
300 lines (282 loc) · 10.8 KB
/
WorkflowRenderTester.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
* Copyright 2020 Square Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// WorkflowTesting only available in Debug mode.
//
// `@testable import Workflow` will fail compilation in Release mode.
#if DEBUG
import XCTest
@testable import Workflow
extension Workflow {
/// Returns a `RenderTester` with a specified initial state.
public func renderTester(initialState: Self.State) -> RenderTester<Self> {
return RenderTester(workflow: self, state: initialState)
}
/// Returns a `RenderTester` with an initial state provided by `self.makeInitialState()`
public func renderTester() -> RenderTester<Self> {
return renderTester(initialState: makeInitialState())
}
}
/// Testing helper for validating the behavior of calls to `render`.
///
/// Usage: `expect` workflows and side effects then validate with a call to `render` and
/// the resulting `RenderTesterResult`.
/// Side-effects may be performed against the rendering to validate the behavior of actions.
///
/// To directly test actions and their effects, use the `WorkflowActionTester`.
///
/// ```
/// workflow
/// .renderTester(initialState: TestWorkflow.State())
/// .expect(
/// worker: TestWorker(),
/// producingOutput: TestWorker.Output.success
/// )
/// .expectWorkflow(
/// type: ChildWorkflow.self,
/// key: "key",
/// rendering: "rendering",
/// // ⚠️ N.B. Only one output per call to `render` may be produced,
/// // even if multiple child Workflows are expected in a call
/// // to `render`. This is an invariant enforced by `RenderTester`
/// // and the real runtime.
/// producingOutput: nil
/// )
/// .render { rendering in
/// XCTAssertEqual("expected text on rendering", rendering.text)
/// }
/// .assert(state: TestWorkflow.State())
/// .assert(output: TestWorkflow.Output.finished)
/// ```
///
/// Validating the rendering only from the initial state provided by the workflow:
/// ```
/// workflow
/// .renderTester()
/// .render { rendering in
/// XCTAssertEqual("expected text on rendering", rendering.text)
/// }
/// ```
///
/// Validate the state was updated from a callback on the rendering:
/// ```
/// workflow
/// .renderTester()
/// .render { rendering in
/// XCTAssertEqual("expected text on rendering", rendering.text)
/// rendering.updateText("updated")
/// }
/// .assert(
/// state: TestWorkflow.State(text: "updated")
/// )
/// ```
///
/// Validate an output was received from the workflow. The `action()` on the rendering will cause an action that will return an output.
/// ```
/// workflow
/// .renderTester()
/// .render { rendering in
/// rendering.action()
/// }
/// .assert(
/// output: .success
/// )
/// ```
///
/// Validate a worker is running, and simulate the effect of its output:
/// ```
/// workflow
/// .renderTester(initialState: TestWorkflow.State(loadingState: .loading))
/// .expect(
/// worker: TestWorker(),
/// output: TestWorker.Output.success
/// )
/// .render { _ in }
/// ```
///
/// Validate a child workflow is run, and simulate the effect of its output:
/// ```
/// workflow
/// .renderTester(initialState: TestWorkflow.State(loadingState: .loading))
/// .expectWorkflow(
/// type: ChildWorkflow.self,
/// rendering: "rendering",
/// producingOutput: ChildWorkflow.Output.success
/// )
/// .render { _ in }
/// ```
public struct RenderTester<WorkflowType: Workflow> {
let workflow: WorkflowType
let state: WorkflowType.State
private let expectedWorkflows: [AnyExpectedWorkflow]
private let expectedSideEffects: [AnyHashable: ExpectedSideEffect]
init(
workflow: WorkflowType,
state: WorkflowType.State,
expectedWorkflows: [AnyExpectedWorkflow] = [],
expectedSideEffects: [AnyHashable: ExpectedSideEffect] = [:]
) {
self.workflow = workflow
self.state = state
self.expectedWorkflows = expectedWorkflows
self.expectedSideEffects = expectedSideEffects
}
/// Expect the given workflow type in the next rendering.
///
/// - Parameters:
/// - type: The type of the expected workflow.
/// - key: The key of the expected workflow (if specified).
/// - rendering: The rendering result that should be returned when the workflow of this type is rendered.
/// - output: An output that should be returned after the workflow of this type is rendered, if any.
/// - assertions: Additional assertions for the given workflow, if any. You may use this to assert the properties of the requested workflow are as expected.
public func expectWorkflow<ExpectedWorkflowType: Workflow>(
type: ExpectedWorkflowType.Type,
key: String = "",
producingRendering rendering: ExpectedWorkflowType.Rendering,
producingOutput output: ExpectedWorkflowType.Output? = nil,
file: StaticString = #file, line: UInt = #line,
assertions: @escaping (ExpectedWorkflowType) -> Void = { _ in }
) -> RenderTester<WorkflowType> {
return RenderTester(
workflow: workflow,
state: state,
expectedWorkflows: expectedWorkflows.appending(
ExpectedWorkflow<ExpectedWorkflowType>(
key: key,
rendering: rendering,
output: output,
assertions: assertions,
file: file,
line: line
)
),
expectedSideEffects: expectedSideEffects
)
}
/// Expect the given workflow type in the next rendering, with its output being ignored by a call to `ignoringOutput()`.
///
/// - Parameters:
/// - type: The type of the expected workflow.
/// - key: The key of the expected workflow (if specified).
/// - rendering: The rendering result that should be returned when the workflow of this type is rendered.
/// - assertions: Additional assertions for the given workflow, if any. You may use this to assert the properties of the requested workflow are as expected.
public func expectWorkflowIgnoringOutput<ExpectedWorkflowType: Workflow>(
type: ExpectedWorkflowType.Type,
key: String = "",
producingRendering rendering: ExpectedWorkflowType.Rendering,
file: StaticString = #file,
line: UInt = #line,
assertions: @escaping (ExpectedWorkflowType) -> Void = { _ in }
) -> RenderTester<WorkflowType> {
return expectWorkflow(
type: OutputBlockingWorkflow<ExpectedWorkflowType>.self,
key: key,
producingRendering: rendering,
file: file,
line: line,
assertions: { assertions($0.child) }
)
}
/// Expect a side-effect for the given key.
///
/// - Parameter key: The key to expect.
public func expectSideEffect(
key: AnyHashable,
file: StaticString = #file, line: UInt = #line
) -> RenderTester<WorkflowType> {
return RenderTester(
workflow: workflow,
state: state,
expectedWorkflows: expectedWorkflows,
expectedSideEffects: expectedSideEffects.setting(
key: key,
value: ExpectedSideEffect(
key: key,
file: file,
line: line
)
)
)
}
/// Expect a side-effect for the given key, and produce the given action when it is requested.
///
/// - Parameters:
/// - key: The key to expect.
/// - action: The action to produce when this side-effect is requested.
public func expectSideEffect<ActionType>(
key: AnyHashable,
producingAction action: ActionType,
file: StaticString = #file, line: UInt = #line
) -> RenderTester<WorkflowType> where ActionType: WorkflowAction, ActionType.WorkflowType == WorkflowType {
return RenderTester(
workflow: workflow,
state: state,
expectedWorkflows: expectedWorkflows,
expectedSideEffects: expectedSideEffects.setting(
key: key,
value: ExpectedSideEffectWithAction(
key: key,
action: action,
file: file,
line: line
)
)
)
}
/// Render the workflow under test. At this point, you should have set up all expectations.
///
/// The given `assertions` closure will be called with the produced rendering, allowing you to assert its properties or
/// perform actions on it (such as closures that are wired up to a `Sink` inside the workflow.
///
/// - Parameters:
/// - assertions: A closure called with the produced rendering for verification
/// - Returns: A `RenderTesterResult` that can be used to verify expected resulting state or outputs.
@discardableResult
public func render(
file: StaticString = #file, line: UInt = #line,
assertions: (WorkflowType.Rendering) throws -> Void
) rethrows -> RenderTesterResult<WorkflowType> {
let contextImplementation = TestContext(
state: state,
expectedWorkflows: expectedWorkflows,
expectedSideEffects: expectedSideEffects,
file: file,
line: line
)
let context = RenderContext.make(implementation: contextImplementation)
let rendering = workflow.render(state: contextImplementation.state, context: context)
contextImplementation.assertNoLeftOverExpectations()
try assertions(rendering)
return RenderTesterResult<WorkflowType>(
initialState: state,
state: contextImplementation.state,
appliedAction: contextImplementation.appliedAction,
output: contextImplementation.producedOutput
)
}
}
extension Collection {
fileprivate func appending(_ element: Element) -> [Element] {
return self + [element]
}
}
extension Dictionary {
fileprivate func setting(key: Key, value: Value) -> [Key: Value] {
var newDictionary = self
newDictionary[key] = value
return newDictionary
}
}
#endif