Skip to content

Commit

Permalink
Add missing tests for callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
hzalaz committed Jan 10, 2017
1 parent e74aaa0 commit 97aad7e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 19 deletions.
25 changes: 7 additions & 18 deletions Lock/ObserverStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,23 @@ struct ObserverStore: Dispatcher {
let closure: () -> ()
switch result {
case .auth(let credentials):
if let presentingController = controller?.presentingViewController {
closure = {
presentingController.dismiss(animated: true, completion: { _ in
self.onAuth(credentials)
})
}
} else {
closure = { self.onAuth(credentials) }
}
closure = dismiss(from: controller?.presentingViewController, completion: { self.onAuth(credentials) })
case .error(let error):
closure = { self.onFailure(error) }
case .cancel:
if let presentingController = controller?.presentingViewController {
closure = {
presentingController.dismiss(animated: true, completion: { _ in
self.onCancel()
})
}
} else {
closure = { self.onCancel() }
}
closure = dismiss(from: controller?.presentingViewController, completion: { self.onCancel() })
case .signUp(let email, let attributes):
closure = { self.onSignUp(email, attributes) }
default:
closure = {}
}
Queue.main.async(closure)
}

private func dismiss(from controller: UIViewController?, completion: @escaping () -> ()) -> () -> () {
guard let controller = controller else { return completion }
return { controller.dismiss(animated: true, completion: completion) }
}
}

enum Result {
Expand Down
37 changes: 37 additions & 0 deletions LockTests/LockSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,43 @@ class LockSpec: QuickSpec {
.present(from: controller)
}
}
}

describe("onAction") {

it("should register onAuth callback") {
var credentials: Credentials? = nil
let callback: (Credentials) -> () = { credentials = $0 }
let _ = lock.onAuth(callback: callback)
lock.observerStore.onAuth(mockCredentials())
expect(credentials).toNot(beNil())
}

it("should register onError callback") {
var error: Error? = nil
let callback: (Error) -> () = { error = $0 }
let _ = lock.onError(callback: callback)
lock.observerStore.onFailure(NSError(domain: "com.auth0", code: 0, userInfo: [:]))
expect(error).toNot(beNil())
}

it("should register onSignUp callback") {
var email: String? = nil
var attributes: [String: Any]? = nil
let callback: (String, [String: Any]) -> () = { email = $0; attributes = $1 }
let _ = lock.onSignUp(callback: callback)
lock.observerStore.onSignUp("mail@mail.com", [:])
expect(email).toNot(beNil())
expect(attributes).toNot(beNil())
}

it("should register onCancel callback") {
var executed = false
let callback: () -> () = { executed = true }
let _ = lock.onCancel(callback: callback)
lock.observerStore.onCancel()
expect(executed) == true
}

}

Expand Down
28 changes: 27 additions & 1 deletion LockTests/Models/ObserverStoreSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ObserverStoreSpec: QuickSpec {
var error: Error?
var credentials: Credentials?
var closed: Bool = false
var dispatcher: Dispatcher!
var dispatcher: ObserverStore!
var newEmail: String?
var newAttributes: [String: Any]?

Expand Down Expand Up @@ -73,6 +73,32 @@ class ObserverStoreSpec: QuickSpec {
expect(newEmail).toEventually(equal(email))
expect(newAttributes?["username"] as? String).toEventually(equal(username))
}

context("controller displayed") {

var controller: MockLockController!
var presenter: MockController!

beforeEach {
presenter = MockController()
controller = MockLockController(lock: Lock())
presenter.presented = controller
controller.presenting = presenter
dispatcher.controller = controller
}

it("should dismiss onAuth") {
let value = mockCredentials()
dispatcher.dispatch(result: .auth(value))
expect(presenter.presented).toEventually(beNil(), timeout: 2)
}

it("should dismiss onCancel") {
dispatcher.dispatch(result: .cancel)
expect(presenter.presented).toEventually(beNil(), timeout: 2)
}

}
}
}
}

0 comments on commit 97aad7e

Please sign in to comment.