Skip to content

Commit b4f86ae

Browse files
authored
[fix]: address some RenderTester limitations with optionals (#245)
1 parent 4dfe312 commit b4f86ae

File tree

4 files changed

+56
-24
lines changed

4 files changed

+56
-24
lines changed

WorkflowReactiveSwift/Testing/SignalProducerWorkflowTesting.swift

+5-21
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,27 @@ import XCTest
2121
@testable import WorkflowReactiveSwift
2222

2323
extension RenderTester {
24-
/// Expect a `SignalProducer`.
24+
/// Expect a `SignalProducer` with an optional output.
2525
///
2626
/// `SignalProducerWorkflow` is used to subscribe to `SignalProducer`s and `Signal`s.
2727
///
2828
/// ⚠️ N.B. If you are testing a case in which multiple `SignalProducerWorkflow`s are expected, **only one of them** may have a non-nil `producingOutput` parameter.
2929
///
3030
/// - Parameters:
31+
/// - outputType: The `OutputType` of the expected `SignalProducerWorkflow`. Typically this will be correctly inferred by the type system, but may need to be explicitly specified if particular optionality is desired.
3132
/// - producingOutput: An output that should be returned when this worker is requested, if any.
3233
/// - key: Key to expect this `Workflow` to be rendered with.
3334
public func expectSignalProducer<OutputType>(
34-
producingOutput output: OutputType? = nil,
35-
key: String = "",
36-
file: StaticString = #file, line: UInt = #line
37-
) -> RenderTester<WorkflowType> {
38-
expectWorkflow(
39-
type: SignalProducerWorkflow<OutputType>.self,
40-
key: key,
41-
producingRendering: (),
42-
producingOutput: output,
43-
assertions: { _ in }
44-
)
45-
}
46-
47-
/// Expect a `SignalProducer` with the specified `outputType` that produces no `Output`.
48-
///
49-
/// - Parameters:
50-
/// - outputType: The `OutputType` of the expected `SignalProducerWorkflow`.
51-
/// - key: Key to expect this `Workflow` to be rendered with.
52-
public func expectSignalProducer<OutputType>(
53-
outputType: OutputType.Type,
35+
outputType: OutputType.Type = OutputType.self,
36+
producingOutput: OutputType? = nil,
5437
key: String = "",
5538
file: StaticString = #file, line: UInt = #line
5639
) -> RenderTester<WorkflowType> {
5740
expectWorkflow(
5841
type: SignalProducerWorkflow<OutputType>.self,
5942
key: key,
6043
producingRendering: (),
44+
producingOutput: producingOutput,
6145
assertions: { _ in }
6246
)
6347
}

WorkflowReactiveSwift/TestingTests/SignalProducerTests.swift

+22
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ class SignalProducerTests: XCTestCase {
5050
.render {}
5151
}
5252

53+
func test_signalProducerWorkflow_optionalOutput() {
54+
OptionalOutputWorkflow()
55+
.renderTester()
56+
.expectSignalProducer(
57+
outputType: Int?.self, // comment this out & test fails
58+
producingOutput: nil as Int?
59+
)
60+
.render {}
61+
}
62+
5363
private struct TestWorkflow: Workflow {
5464
typealias State = Void
5565
typealias Rendering = Void
@@ -64,4 +74,16 @@ class SignalProducerTests: XCTestCase {
6474
}
6575
}
6676
}
77+
78+
private struct OptionalOutputWorkflow: Workflow {
79+
typealias State = Void
80+
typealias Rendering = Void
81+
typealias Output = Int?
82+
83+
func render(state: State, context: RenderContext<Self>) -> Rendering {
84+
SignalProducer(value: Int?.some(1))
85+
.mapOutput { _ in AnyWorkflowAction<Self>.noAction }
86+
.rendered(in: context)
87+
}
88+
}
6789
}

WorkflowRxSwift/Testing/ObservableTesting.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ extension RenderTester {
2424
/// Expect the given worker. It will be checked for `isEquivalent(to:)` with the requested worker.
2525

2626
/// - Parameters:
27-
/// - worker: The worker to be expected
28-
/// - output: An output that should be returned when this worker is requested, if any.
27+
/// - outputType: The `OutputType` of the expected `ObservableWorkflow`.
28+
/// - producingOutput: An output that should be returned when this worker is requested, if any.
29+
/// - key: Key to expect this `Workflow` to be rendered with.
2930
public func expectObservable<OutputType>(
31+
outputType: OutputType.Type = OutputType.self,
3032
producingOutput output: OutputType? = nil,
3133
key: String = "",
3234
file: StaticString = #file, line: UInt = #line

WorkflowRxSwift/TestingTests/ObservableTests.swift

+25-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,18 @@ class ObservableTests: XCTestCase {
2828
.render {}
2929
}
3030

31-
struct TestWorkflow: Workflow {
31+
func test_observableWorkflow_optionalOutputType() {
32+
OptionalOutputWorkflow()
33+
.renderTester()
34+
.expectObservable(
35+
outputType: Int?.self, // comment this out & test fails
36+
producingOutput: nil as Int?,
37+
key: "123"
38+
)
39+
.render {}
40+
}
41+
42+
private struct TestWorkflow: Workflow {
3243
typealias State = Void
3344
typealias Rendering = Void
3445

@@ -38,4 +49,17 @@ class ObservableTests: XCTestCase {
3849
.running(in: context, key: "123")
3950
}
4051
}
52+
53+
private struct OptionalOutputWorkflow: Workflow {
54+
typealias State = Void
55+
typealias Rendering = Void
56+
typealias Output = Int?
57+
58+
func render(state: State, context: RenderContext<Self>) -> Rendering {
59+
Observable.from([1])
60+
.map { Int?.some($0) }
61+
.mapOutput { _ in AnyWorkflowAction<Self>.noAction }
62+
.rendered(in: context, key: "123")
63+
}
64+
}
4165
}

0 commit comments

Comments
 (0)