Skip to content

Commit

Permalink
Merge pull request #52 from hyperoslo/fix/navigator
Browse files Browse the repository at this point in the history
Navigate using Navigator
  • Loading branch information
onmyway133 authored Aug 16, 2017
2 parents 110d789 + a850d7d commit 07dc8ff
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
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)
}
}

0 comments on commit 07dc8ff

Please sign in to comment.