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

Navigate using Navigator #52

Merged
merged 2 commits into from
Aug 16, 2017
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
45 changes: 45 additions & 0 deletions Sources/Navigator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public struct Navigator {
/// A list of route strings
public static var routes = [String]()

/// Handle the location request
public static var handle: ((Location) -> Void)?

/// Parse Location from url
///
/// - Parameters:
Expand Down Expand Up @@ -116,3 +119,45 @@ public struct Navigator {
wildcardMatchCount: wildcardMatchCount)
}
}

public extension Navigator {

/// Navigate using urn
///
/// - Parameters:
/// - urn: The urn
/// - payload: Optional payload
/// - Throws: RouteError if the routing fails
public static func navigate(urn: String, payload: Any? = nil) throws {
let encodedUrn = PercentEncoder.encode(string: urn, allowedCharacters: delimiter)
guard let url = URL(string: "\(scheme)\(encodedUrn)") else {
throw RouteError.notFound
}

try navigate(url: url, payload: payload)
}

/// Navigate using url
///
/// - Parameters:
/// - urn: The url
/// - payload: Optional payload
/// - Throws: RouteError if the routing fails
public static func navigate(url: URL, payload: Any? = nil) throws {
guard let location = parse(url: url, payload: payload) else {
throw RouteError.notFound
}

try navigate(location: location)
}

/// Navigate using location
///
/// - Parameters:
/// - urn: The urn
/// - payload: Optional payload
/// - Throws: RouteError if the routing fails
public static func navigate(location: Location) throws {
handle?(location)
}
}
12 changes: 12 additions & 0 deletions Tests/Compass/CompassTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,16 @@ class CompassTests: XCTestCase {
XCTAssertEqual(location.arguments["expires_in"], "3600")
XCTAssertEqual(location.arguments["token_type"], "Bearer")
}

func testParseEncodedURL() {
let urn = "organization:hyper oslo:simply awesome"
Navigator.handle = { location in
XCTAssertEqual(location.arguments["name"], "hyper oslo")
XCTAssertEqual(location.arguments["type"], "simply awesome")
}

try? Navigator.navigate(urn: urn)

wait(for: 0.1)
}
}
15 changes: 15 additions & 0 deletions Tests/Compass/Helpers.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import XCTest
@testable import Compass

// MARK: - Routes
Expand Down Expand Up @@ -49,3 +50,17 @@ extension Array {
}
}

extension XCTestCase {
func wait(for duration: TimeInterval) {
let waitExpectation = expectation(description: "Waiting")

let when = DispatchTime.now() + duration
DispatchQueue.main.asyncAfter(deadline: when) {
waitExpectation.fulfill()
}

// We use a buffer here to avoid flakiness with Timer on CI
waitForExpectations(timeout: duration + 0.5)
}
}