-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAlertPageViewControllerSpec.swift
83 lines (62 loc) · 3.29 KB
/
AlertPageViewControllerSpec.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import XCTest
import Nimble
import Fleet
@testable import FleetExamples
class AlertPageViewControllerSpec: XCTestCase {
var subject: AlertPageViewController!
override func setUp() {
super.setUp()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
subject = storyboard.instantiateViewController(withIdentifier: "AlertPageViewController") as! AlertPageViewController
let _ = Fleet.setInAppWindowRootNavigation(subject)
}
func test_tappingTheShowAlertButton_showsAnAlert() {
try! subject.showAlertButton?.tap()
// Tests asserting that an alert displays look a little different
// because alerts don't usually come from the storyboard. Here,
// we'll want to capture the alert and examine its content as part of
// a good unit test. You have a lot of options available to you.
// Following are some examples that all thoroughly test the content
// of the alert.
expect((Fleet.getApplicationScreen()?.topmostViewController as? UIAlertController)?.title).toEventually(equal("Alert Title"))
expect((Fleet.getApplicationScreen()?.topmostViewController as? UIAlertController)?.message).toEventually(equal("An alert has appeared"))
// Many won't like the amount of casting that has to happen to finally
// get to testing the content of the alert itself. This next set of tests
// hides the fetching of the topmost alert behind a function defined
// right in the test.
let alertViewController = { () -> (UIAlertController?) in
return Fleet.getApplicationScreen()?.topmostViewController as? UIAlertController
}
expect(alertViewController()?.title).toEventually(equal("Alert Title"))
expect(alertViewController()?.message).toEventually(equal("An alert has appeared"))
// Finally, you can also just define a function that wraps the casting and
// content checks all into one predicate.
let assertAlertContent = { () -> Bool in
let alert = Fleet.getApplicationScreen()?.topmostViewController as? UIAlertController
return alert != nil &&
alert!.title == "Alert Title" &&
alert!.message == "An alert has appeared"
}
expect(assertAlertContent()).toEventually(beTrue())
}
func test_afterTheAlertIsShown_tappingTheOKButtonOnTheAlert_dismissesTheAlert() {
try! subject.showAlertButton?.tap()
// Normally, testing the behavior of a button on the alert is a pain, because
// UIKit does not really give you easy access to actions on an alert. Fleet takes
// that pain away.
var didTapOK = false
let assertOKTappedBehavior = { () -> Bool in
if didTapOK {
return Fleet.getApplicationScreen()?.topmostViewController === self.subject
}
if let alert = Fleet.getApplicationScreen()?.topmostViewController as? UIAlertController {
// Tapping an alert action with the text 'OK' takes one line once you
// have the alert in hand.
try! alert.tapAlertAction(withTitle: "OK")
didTapOK = true
}
return false
}
expect(assertOKTappedBehavior()).toEventually(beTrue())
}
}