Skip to content

[QoL] Allow RenderTester to recover and continue from missing AnyScreen-producing child workflows #150

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,14 @@ let package = Package(
),
.target(
name: "WorkflowTesting",
dependencies: ["Workflow"],
dependencies: ["Workflow", "WorkflowUI"],
path: "WorkflowTesting/Sources"
),
.testTarget(
name: "WorkflowTestingTests",
dependencies: ["Workflow", "WorkflowTesting", "WorkflowUI"],
path: "WorkflowTesting/Tests"
),
.target(
name: "WorkflowReactiveSwiftTesting",
dependencies: ["WorkflowReactiveSwift", "WorkflowTesting"],
Expand Down
1 change: 1 addition & 0 deletions WorkflowTesting.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Pod::Spec.new do |s|
s.source_files = 'WorkflowTesting/Sources/**/*.swift'

s.dependency 'Workflow', "#{s.version}"
s.dependency 'WorkflowUI', "#{s.version}"
s.framework = 'XCTest'

s.test_spec 'Tests' do |test_spec|
Expand Down
16 changes: 14 additions & 2 deletions WorkflowTesting/Sources/Internal/RenderTester+TestContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import XCTest
@testable import Workflow

#if canImport(UIKit) && canImport(WorkflowUI)
import WorkflowUI
#endif

extension RenderTester {
internal final class TestContext: RenderContextType {
var state: WorkflowType.State
Expand Down Expand Up @@ -68,10 +72,18 @@

// We can “recover” from missing Void-rendering workflows since there’s only one possible value to return
if Child.Rendering.self == Void.self {
// Couldn’t find a nicer way to do this polymorphically
return () as! Child.Rendering
}
fatalError("Unable to continue.")

#if canImport(UIKit) && canImport(WorkflowUI)
// We can "recover" from missing AnyScreen-rendering workflows since they render an opaque type that we can construct a value of
if Child.Rendering.self == AnyScreen.self {
return RenderTesterPlaceholderScreen().asAnyScreen() as! Child.Rendering
}
#endif

// At this point, without an return value from an expectation, we have nothing to return from render and are unable to continue
fatalError("Unable to continue; no expectation has given RenderTester a value to return from `render` (and cannot construct an arbitrary value of type \"\(Child.Rendering.self)\").")
}
let (inserted, _) = usedWorkflowKeys.insert(WorkflowKey(type: ObjectIdentifier(Child.self), key: key))
if !inserted {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.
*/

#if DEBUG && canImport(UIKit) && canImport(WorkflowUI)

import WorkflowUI
import XCTest

/// Used as the stand-in value returned by RenderTester when an AnyScreen is expected but not provided
struct RenderTesterPlaceholderScreen: Screen {
func viewControllerDescription(environment: ViewEnvironment) -> ViewControllerDescription {
ViewControllerDescription(
type: UIViewController.self,
build: {
XCTFail("Unexpected construction of screen in RenderTester")
return UIViewController()
},
update: { _ in }
)
}
}

#endif
41 changes: 41 additions & 0 deletions WorkflowTesting/Tests/WorkflowRenderTesterFailureTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@

import Workflow
import WorkflowTesting
import WorkflowUI
import XCTest

#if canImport(UIKit)
import UIKit
#endif

/// Who tests the tester?
///
/// WorkflowRenderTesterFailureTests does.
Expand Down Expand Up @@ -132,6 +137,42 @@ final class WorkflowRenderTesterFailureTests: XCTestCase {
}
}

#if canImport(UIKit)
func test_childWorkflow_unexpected_anyScreenRendering() {
struct MyScreen: Screen {
func viewControllerDescription(environment: ViewEnvironment) -> ViewControllerDescription {
return ViewControllerDescription(
type: UIViewController.self,
build: { UIViewController() },
update: { _ in }
)
}
}

struct MyChildWorkflow: Workflow {
typealias State = Void
func render(state: State, context: RenderContext<Self>) -> AnyScreen {
return MyScreen().asAnyScreen()
}
}

struct MyWorkflow: Workflow {
typealias State = Void
func render(state: State, context: RenderContext<Self>) -> AnyScreen {
let childRendering = MyChildWorkflow().rendered(in: context)
return childRendering
}
}

let tester = MyWorkflow()
.renderTester()

expectingFailure(#"Unexpected workflow of type MyChildWorkflow with key """#) {
tester.render { _ in }
}
}
#endif

func test_childWorkflowMultipleRenders_sameKey() {
let tester = ParentWorkflow()
.renderTester()
Expand Down