Skip to content

Commit

Permalink
Add MockRouter library
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardiman committed Sep 26, 2019
1 parent caecf43 commit 14c7e35
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 23 deletions.
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ let package = Package(
name: "Router",
products: [
.library(name: "Router", targets: ["Router"]),
.library(name: "MockRouter", targets: ["MockRouter"])
],
dependencies: [],
targets: [
.target(name: "Router", dependencies: []),
.testTarget(name: "RouterTests", dependencies: ["Router"]),
.target(name: "MockRouter", dependencies: ["Router"])
],
swiftLanguageVersions: [.v5]
)
104 changes: 104 additions & 0 deletions Sources/MockRouter/MockRouter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//
// MockRouter.swift
// MockRouter
//
// Created by David Hardiman on 26/09/2019.
// Copyright © 2017 David Hardiman. All rights reserved.
//

import Foundation
import Hopoate
import Router

public class MockRouter: Router {
public var routeResponse: UIViewController = UIViewController()
public var receivedDestinations = [Destination]()
public var receivedTargets = [RouteTarget]()
public var vendRealController = false
public private(set) var vendedControllers: [UIViewController] = []
public var receivedDestination: Destination? {
return receivedDestinations.last
}

public func typedReceivedDestination<T: Destination>(_ type: T.Type) -> T? {
return receivedDestination as? T
}

public private(set) var receivedDismissMessage = false

public init() {}

@discardableResult
public func route(to destination: Destination) -> UIViewController? {
receivedDestinations.append(destination)
guard vendRealController else {
return routeResponse
}
let controller = destination.destination
vendedControllers.append(controller)
return controller
}

public var receivedURL: URL? {
guard let target = receivedTarget, case let .url(url) = target else {
return nil
}
return url
}

public var receivedModel: Any? {
guard let target = receivedTarget, case let .model(model) = target else {
return nil
}
return model
}

private var receivedTarget: RouteTarget? {
return receivedTargets.last
}

public func route(to target: RouteTarget) -> Bool {
receivedTargets.append(target)
return false
}

public var receivedCompletion: (() -> Void)?
public func route(to target: RouteTarget, completion: (() -> Void)?) -> Bool {
receivedTargets.append(target)
receivedCompletion = completion
return false
}

public var receivedExternalURL: URL?
public func routeFromExternalSource(to url: URL) -> Bool {
receivedExternalURL = url
return false
}

public func dismiss(animated: Bool, completion: (() -> Void)?) {
receivedDismissMessage = true
completion?()
}

public static func performTests(with testingClosure: (MockRouter) -> Void) {
try? performThrowingTests(with: testingClosure)
}

public static func performThrowingTests(with testingClosure: (MockRouter) throws -> Void) throws {
let (mockRouter, registration) = registeredMockRouter()
try testingClosure(mockRouter)
DependencyContainer.shared.remove(registration)
}

public static func registeredMockRouter() -> (MockRouter, ServiceRegistration<Router>) {
let mockRouter = MockRouter()
let mockRouterServiceRegistration = DependencyContainer.shared.register(service: Router.self) {
mockRouter
}
return (mockRouter, mockRouterServiceRegistration)
}

public static func unregister(routerRegistration: ServiceRegistration<Router>) {
DependencyContainer.shared.remove(routerRegistration)
}
}
22 changes: 0 additions & 22 deletions Tests/RouterTests/Info.plist

This file was deleted.

0 comments on commit 14c7e35

Please sign in to comment.