Skip to content

Commit

Permalink
[ci skip] Add tests around the rest of the workflow example - TT
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler-Keith-Thompson committed Oct 5, 2019
1 parent 535fa62 commit fbd4586
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 45 deletions.
28 changes: 28 additions & 0 deletions Workflow.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
AD79EC4523491F2200F86101 /* TestFinder.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD79EC4423491F2200F86101 /* TestFinder.swift */; };
AD79EC472349210000F86101 /* MenuSelectionViewControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD79EC462349210000F86101 /* MenuSelectionViewControllerTests.swift */; };
AD79EC49234926D000F86101 /* FoodSelectionViewControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD79EC48234926D000F86101 /* FoodSelectionViewControllerTests.swift */; };
AD79EC4B23492B6F00F86101 /* ReviewOrderViewControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD79EC4A23492B6F00F86101 /* ReviewOrderViewControllerTests.swift */; };
AD79EC4D23492EDD00F86101 /* EnterAddressViewControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD79EC4C23492EDD00F86101 /* EnterAddressViewControllerTests.swift */; };
AD79EC4F2349306100F86101 /* SetupViewControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD79EC4E2349306100F86101 /* SetupViewControllerTests.swift */; };
AD79EC522349319300F86101 /* MockPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD79EC512349319300F86101 /* MockPresenter.swift */; };
AD79EC54234931A400F86101 /* WorkflowListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD79EC53234931A400F86101 /* WorkflowListener.swift */; };
AD8B6C772319E8B200781B01 /* Screen.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD8B6C762319E8B200781B01 /* Screen.swift */; };
AD8BEBE42318303A0071A83D /* AnyPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD8BEBE32318303A0071A83D /* AnyPresenter.swift */; };
AD8BEBE6231830860071A83D /* BasePresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD8BEBE5231830860071A83D /* BasePresenter.swift */; };
Expand Down Expand Up @@ -112,6 +117,11 @@
AD79EC4423491F2200F86101 /* TestFinder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestFinder.swift; sourceTree = "<group>"; };
AD79EC462349210000F86101 /* MenuSelectionViewControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuSelectionViewControllerTests.swift; sourceTree = "<group>"; };
AD79EC48234926D000F86101 /* FoodSelectionViewControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FoodSelectionViewControllerTests.swift; sourceTree = "<group>"; };
AD79EC4A23492B6F00F86101 /* ReviewOrderViewControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReviewOrderViewControllerTests.swift; sourceTree = "<group>"; };
AD79EC4C23492EDD00F86101 /* EnterAddressViewControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnterAddressViewControllerTests.swift; sourceTree = "<group>"; };
AD79EC4E2349306100F86101 /* SetupViewControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetupViewControllerTests.swift; sourceTree = "<group>"; };
AD79EC512349319300F86101 /* MockPresenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockPresenter.swift; sourceTree = "<group>"; };
AD79EC53234931A400F86101 /* WorkflowListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WorkflowListener.swift; sourceTree = "<group>"; };
AD8B6C762319E8B200781B01 /* Screen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Screen.swift; sourceTree = "<group>"; };
AD8BEBE32318303A0071A83D /* AnyPresenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnyPresenter.swift; sourceTree = "<group>"; };
AD8BEBE5231830860071A83D /* BasePresenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BasePresenter.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -242,6 +252,15 @@
path = WorkflowExample;
sourceTree = "<group>";
};
AD79EC502349317C00F86101 /* Mocks */ = {
isa = PBXGroup;
children = (
AD79EC512349319300F86101 /* MockPresenter.swift */,
AD79EC53234931A400F86101 /* WorkflowListener.swift */,
);
path = Mocks;
sourceTree = "<group>";
};
AD8BEBE72318308C0071A83D /* Models */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -364,6 +383,7 @@
ADEE7750233BAA6C007E9FD2 /* WorkflowExampleTests */ = {
isa = PBXGroup;
children = (
AD79EC502349317C00F86101 /* Mocks */,
ADEE7753233BAA6C007E9FD2 /* Info.plist */,
ADEE7759233BAB27007E9FD2 /* LocationsViewControllerTests.swift */,
ADEE775B233BC7F0007E9FD2 /* ViewControllerTest.swift */,
Expand All @@ -372,6 +392,9 @@
AD79EC4423491F2200F86101 /* TestFinder.swift */,
AD79EC462349210000F86101 /* MenuSelectionViewControllerTests.swift */,
AD79EC48234926D000F86101 /* FoodSelectionViewControllerTests.swift */,
AD79EC4A23492B6F00F86101 /* ReviewOrderViewControllerTests.swift */,
AD79EC4C23492EDD00F86101 /* EnterAddressViewControllerTests.swift */,
AD79EC4E2349306100F86101 /* SetupViewControllerTests.swift */,
);
path = WorkflowExampleTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -783,10 +806,15 @@
AD79EC4523491F2200F86101 /* TestFinder.swift in Sources */,
AD79EC49234926D000F86101 /* FoodSelectionViewControllerTests.swift in Sources */,
ADEE775A233BAB27007E9FD2 /* LocationsViewControllerTests.swift in Sources */,
AD79EC4D23492EDD00F86101 /* EnterAddressViewControllerTests.swift in Sources */,
AD79EC4F2349306100F86101 /* SetupViewControllerTests.swift in Sources */,
AD79EC472349210000F86101 /* MenuSelectionViewControllerTests.swift in Sources */,
AD79EC54234931A400F86101 /* WorkflowListener.swift in Sources */,
ADEE7760233BC960007E9FD2 /* Convenience.swift in Sources */,
ADEE775C233BC7F0007E9FD2 /* ViewControllerTest.swift in Sources */,
AD79EC522349319300F86101 /* MockPresenter.swift in Sources */,
ADEE775E233BC901007E9FD2 /* PickupOrDeliveryViewControllerTests.swift in Sources */,
AD79EC4B23492B6F00F86101 /* ReviewOrderViewControllerTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
2 changes: 1 addition & 1 deletion WorkflowExample/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<!--Setup View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="SetupViewController" customModule="WorkflowExample" customModuleProvider="target" sceneMemberID="viewController">
<viewController storyboardIdentifier="SetupViewController" id="BYZ-38-t0r" customClass="SetupViewController" customModule="WorkflowExample" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
Expand Down
2 changes: 1 addition & 1 deletion WorkflowExample/SetupViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation
import UIKit

class SetupViewController: UIViewController {
class SetupViewController: UIViewController, StoryboardLoadable {
@IBAction func launchMultiLocationWorkflow() {
let locations = [
Location(name: "Just Pickup w/ just catering",
Expand Down
34 changes: 34 additions & 0 deletions WorkflowExampleTests/EnterAddressViewControllerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// EnterAddressViewControllerTests.swift
// WorkflowExampleTests
//
// Created by Tyler Thompson on 10/5/19.
// Copyright © 2019 Tyler Thompson. All rights reserved.
//

import Foundation
import XCTest

@testable import WorkflowExample

class EnterAddressViewControllerTests: ViewControllerTest<EnterAddressViewController> {
func testViewShouldAlwaysLoad() {
let order = Order(location: nil)
XCTAssert(testViewController.shouldLoad(with: order))
}

func testSavingAddressProceedsInWorkflow() {
var proceedInWorkflowCalled = false
let order = Order(location: nil)
testViewController.order = order

testViewController.callback = { data in
proceedInWorkflowCalled = true
XCTAssertEqual((data as? Order)?.orderType, .delivery(Address()))
}

testViewController.saveAddress()

XCTAssert(proceedInWorkflowCalled)
}
}
23 changes: 23 additions & 0 deletions WorkflowExampleTests/Mocks/MockPresenter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// MockPresenter.swift
// WorkflowExampleTests
//
// Created by Tyler Thompson on 10/5/19.
// Copyright © 2019 Tyler Thompson. All rights reserved.
//

import Foundation
@testable import DynamicWorkflow

class MockPresenter: Presenter {
var abandonCalled = 0
var lastWorkflow:Workflow?
var lastAnimated:Bool?
func abandon(_ workflow: Workflow, animated: Bool, onFinish: (() -> Void)?) {
abandonCalled += 1
lastWorkflow = workflow
lastAnimated = animated
onFinish?()
}
required init() { }
}
42 changes: 42 additions & 0 deletions WorkflowExampleTests/Mocks/WorkflowListener.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// WorkflowListener.swift
// WorkflowExampleTests
//
// Created by Tyler Thompson on 10/5/19.
// Copyright © 2019 Tyler Thompson. All rights reserved.
//

import Foundation
import XCTest

@testable import DynamicWorkflow

class WorkflowListener {
var workflow:Workflow?
var launchStyle:PresentationType?
var args:Any?
var launchedFrom:AnyFlowRepresentable?
var onFinish:((Any?) -> Void)?
init() {
NotificationCenter.default.addObserver(self, selector: #selector(workflowLaunched(notification:)), name: .workflowLaunched, object: nil)
}

@objc func workflowLaunched(notification: Notification) {
let dict = notification.object as? [String:Any?]
workflow = dict?["workflow"] as? Workflow
launchStyle = dict?["style"] as? PresentationType
onFinish = dict?["onFinish"] as? ((Any?) -> Void)
launchedFrom = dict?["launchFrom"] as? AnyFlowRepresentable
args = dict?["args"] as Any?
}
}

func XCTAssertWorkflowLaunched(listener: WorkflowListener, expectedFlowRepresentables:[AnyFlowRepresentable.Type]) {
XCTAssertNotNil(listener.workflow, "No workflow found")
guard let workflow = listener.workflow, expectedFlowRepresentables.count == workflow.count else {
XCTFail("workflow does not contain correct representables: \(String(describing: listener.workflow?.compactMap { String(describing: $0.value) }) )")
return
}
XCTAssertEqual(workflow.compactMap { String(describing: $0.value) },
expectedFlowRepresentables.map { String(describing: $0) })
}
43 changes: 0 additions & 43 deletions WorkflowExampleTests/PickupOrDeliveryViewControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,46 +80,3 @@ class PickupOrDeliveryViewConrollerTests:ViewControllerTest<PickupOrDeliveryView
XCTAssert(proceedInWorkflowCalled)
}
}

func XCTAssertWorkflowLaunched(listener: WorkflowListener, expectedFlowRepresentables:[AnyFlowRepresentable.Type]) {
XCTAssertNotNil(listener.workflow, "No workflow found")
guard let workflow = listener.workflow, expectedFlowRepresentables.count == workflow.count else {
XCTFail("workflow does not contain correct representables: \(String(describing: listener.workflow?.compactMap { String(describing: $0.value) }) )")
return
}
XCTAssertEqual(workflow.compactMap { String(describing: $0.value) },
expectedFlowRepresentables.map { String(describing: $0) })
}

class MockPresenter: Presenter {
var abandonCalled = 0
var lastWorkflow:Workflow?
var lastAnimated:Bool?
func abandon(_ workflow: Workflow, animated: Bool, onFinish: (() -> Void)?) {
abandonCalled += 1
lastWorkflow = workflow
lastAnimated = animated
onFinish?()
}
required init() { }
}

class WorkflowListener {
var workflow:Workflow?
var launchStyle:PresentationType?
var args:Any?
var launchedFrom:AnyFlowRepresentable?
var onFinish:((Any?) -> Void)?
init() {
NotificationCenter.default.addObserver(self, selector: #selector(workflowLaunched(notification:)), name: .workflowLaunched, object: nil)
}

@objc func workflowLaunched(notification: Notification) {
let dict = notification.object as? [String:Any?]
workflow = dict?["workflow"] as? Workflow
launchStyle = dict?["style"] as? PresentationType
onFinish = dict?["onFinish"] as? ((Any?) -> Void)
launchedFrom = dict?["launchFrom"] as? AnyFlowRepresentable
args = dict?["args"] as Any?
}
}
49 changes: 49 additions & 0 deletions WorkflowExampleTests/ReviewOrderViewControllerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// ReviewOrderViewControllerTests.swift
// WorkflowExampleTests
//
// Created by Tyler Thompson on 10/5/19.
// Copyright © 2019 Tyler Thompson. All rights reserved.
//

import Foundation
import XCTest

@testable import WorkflowExample

class ReviewOrderViewControllerTests: ViewControllerTest<ReviewOrderViewController> {

var locationNameLabel:UILabel!
var menuLabel:UILabel!
var orderTypeLabel:UILabel!
var foodChoiceLabel:UILabel!

override func afterLoadFromStoryboard() {
locationNameLabel = testViewController.locationNameLabel
menuLabel = testViewController.menuLabel
orderTypeLabel = testViewController.orderTypeLabel
foodChoiceLabel = testViewController.foodChoiceLabel
}

func testShouldLoad() {
let order = Order(location: Location(name: "", address: Address(), orderTypes: [], menuTypes: []))
XCTAssert(testViewController.shouldLoad(with: order))
}

func testShowOrderInformation() {
let locationName = UUID().uuidString
var order = Order(location: Location(name: locationName, address: Address(), orderTypes: [.delivery(Address())], menuTypes: [.catering]))
order.menuType = .catering
order.shoppingCart.append(Food(name: "Combo #1"))
order.shoppingCart.append(Food(name: "Combo #2"))
loadFromStoryboard {
XCTAssert($0.shouldLoad(with: order))
}

XCTAssertEqual(locationNameLabel.text, locationName)
XCTAssertEqual(menuLabel.text, "Catering Menu")
XCTAssertEqual(orderTypeLabel.text, "Delivery")
XCTAssertEqual(foodChoiceLabel.text, "Combo #1, Combo #2")
}

}
28 changes: 28 additions & 0 deletions WorkflowExampleTests/SetupViewControllerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// SetupViewControllerTests.swift
// WorkflowExampleTests
//
// Created by Tyler Thompson on 10/5/19.
// Copyright © 2019 Tyler Thompson. All rights reserved.
//

import Foundation
import XCTest

@testable import WorkflowExample

class SetupViewControllerTests: ViewControllerTest<SetupViewController> {
func testLaunchingMultiLocationWorkflow() {
let listener = WorkflowListener()

testViewController.launchMultiLocationWorkflow()

XCTAssertWorkflowLaunched(listener: listener, expectedFlowRepresentables: [
LocationsViewController.self,
PickupOrDeliveryViewController.self,
MenuSelectionViewController.self,
FoodSelectionViewController.self,
ReviewOrderViewController.self,
])
}
}
3 changes: 3 additions & 0 deletions WorkflowExampleTests/TestFinder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class TestFinder: XCTestCase {
XCTestSuite(forTestCaseClass: PickupOrDeliveryViewConrollerTests.self).tests.forEach { suite.addTest($0) }
XCTestSuite(forTestCaseClass: MenuSelectionViewControllerTests.self).tests.forEach { suite.addTest($0) }
XCTestSuite(forTestCaseClass: FoodSelectionViewControllerTests.self).tests.forEach { suite.addTest($0) }
XCTestSuite(forTestCaseClass: ReviewOrderViewControllerTests.self).tests.forEach { suite.addTest($0) }
XCTestSuite(forTestCaseClass: EnterAddressViewControllerTests.self).tests.forEach { suite.addTest($0) }
XCTestSuite(forTestCaseClass: SetupViewControllerTests.self).tests.forEach { suite.addTest($0) }
return suite
}

Expand Down

0 comments on commit fbd4586

Please sign in to comment.