Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix]: address some RenderTester limitations with optionals #245

Merged
merged 1 commit into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,27 @@ import XCTest
@testable import WorkflowReactiveSwift

extension RenderTester {
/// Expect a `SignalProducer`.
/// Expect a `SignalProducer` with an optional output.
///
/// `SignalProducerWorkflow` is used to subscribe to `SignalProducer`s and `Signal`s.
///
/// ⚠️ 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.
///
/// - Parameters:
/// - 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.
/// - producingOutput: An output that should be returned when this worker is requested, if any.
/// - key: Key to expect this `Workflow` to be rendered with.
public func expectSignalProducer<OutputType>(
producingOutput output: OutputType? = nil,
key: String = "",
file: StaticString = #file, line: UInt = #line
) -> RenderTester<WorkflowType> {
expectWorkflow(
type: SignalProducerWorkflow<OutputType>.self,
key: key,
producingRendering: (),
producingOutput: output,
assertions: { _ in }
)
}

/// Expect a `SignalProducer` with the specified `outputType` that produces no `Output`.
///
/// - Parameters:
/// - outputType: The `OutputType` of the expected `SignalProducerWorkflow`.
/// - key: Key to expect this `Workflow` to be rendered with.
public func expectSignalProducer<OutputType>(
outputType: OutputType.Type,
outputType: OutputType.Type = OutputType.self,
producingOutput: OutputType? = nil,
key: String = "",
file: StaticString = #file, line: UInt = #line
) -> RenderTester<WorkflowType> {
expectWorkflow(
type: SignalProducerWorkflow<OutputType>.self,
key: key,
producingRendering: (),
producingOutput: producingOutput,
assertions: { _ in }
)
}
Expand Down
22 changes: 22 additions & 0 deletions WorkflowReactiveSwift/TestingTests/SignalProducerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ class SignalProducerTests: XCTestCase {
.render {}
}

func test_signalProducerWorkflow_optionalOutput() {
OptionalOutputWorkflow()
.renderTester()
.expectSignalProducer(
outputType: Int?.self, // comment this out & test fails
producingOutput: nil as Int?
)
.render {}
}

private struct TestWorkflow: Workflow {
typealias State = Void
typealias Rendering = Void
Expand All @@ -64,4 +74,16 @@ class SignalProducerTests: XCTestCase {
}
}
}

private struct OptionalOutputWorkflow: Workflow {
typealias State = Void
typealias Rendering = Void
typealias Output = Int?

func render(state: State, context: RenderContext<Self>) -> Rendering {
SignalProducer(value: Int?.some(1))
.mapOutput { _ in AnyWorkflowAction<Self>.noAction }
.rendered(in: context)
}
}
}
6 changes: 4 additions & 2 deletions WorkflowRxSwift/Testing/ObservableTesting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ extension RenderTester {
/// Expect the given worker. It will be checked for `isEquivalent(to:)` with the requested worker.

/// - Parameters:
/// - worker: The worker to be expected
/// - output: An output that should be returned when this worker is requested, if any.
/// - outputType: The `OutputType` of the expected `ObservableWorkflow`.
/// - producingOutput: An output that should be returned when this worker is requested, if any.
/// - key: Key to expect this `Workflow` to be rendered with.
public func expectObservable<OutputType>(
outputType: OutputType.Type = OutputType.self,
producingOutput output: OutputType? = nil,
key: String = "",
file: StaticString = #file, line: UInt = #line
Expand Down
26 changes: 25 additions & 1 deletion WorkflowRxSwift/TestingTests/ObservableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,18 @@ class ObservableTests: XCTestCase {
.render {}
}

struct TestWorkflow: Workflow {
func test_observableWorkflow_optionalOutputType() {
OptionalOutputWorkflow()
.renderTester()
.expectObservable(
outputType: Int?.self, // comment this out & test fails
producingOutput: nil as Int?,
key: "123"
)
.render {}
}

private struct TestWorkflow: Workflow {
typealias State = Void
typealias Rendering = Void

Expand All @@ -38,4 +49,17 @@ class ObservableTests: XCTestCase {
.running(in: context, key: "123")
}
}

private struct OptionalOutputWorkflow: Workflow {
typealias State = Void
typealias Rendering = Void
typealias Output = Int?

func render(state: State, context: RenderContext<Self>) -> Rendering {
Observable.from([1])
.map { Int?.some($0) }
.mapOutput { _ in AnyWorkflowAction<Self>.noAction }
.rendered(in: context, key: "123")
}
}
}