-
Notifications
You must be signed in to change notification settings - Fork 11
/
DemoTests.swift
163 lines (126 loc) · 4.97 KB
/
DemoTests.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
// DemoTests.swift
// DemoTests
//
// Created by Masahiro Watanabe on 2018/10/23.
// Copyright © 2018 Masahiro Watanabe. All rights reserved.
//
import XCTest
@testable import Demo
class DemoTests: XCTestCase {
override func setUp() {
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
func testViewController() {
let expectation = self.expectation(description: "ALRT should be shown from view controller")
let test = TestViewController()
test.showALRT { (result) in
self.inspect(result)
expectation.fulfill()
}
self.waitForExpectations(timeout: 3.0, handler: nil)
}
func testPresentedViewController() {
let expectation = self.expectation(description: "ALRT should be shown from presented view controller")
let first = TestViewController()
let second = TestViewController()
first.present(second, animated: false)
second.showALRT { (result) in
self.inspect(result)
expectation.fulfill()
}
self.waitForExpectations(timeout: 3.0, handler: nil)
}
func testViewControllerEmbeddedInNavigationController() {
let expectation = self.expectation(description: "ALRT should be shown from view controller in UINavigationController")
let test = TestViewController()
_ = test.embedInNavigationController()
test.showALRT { (result) in
self.inspect(result)
expectation.fulfill()
}
self.waitForExpectations(timeout: 3.0, handler: nil)
}
func testPushedViewController() {
let expectation = self.expectation(description: "ALRT should be shown from pushed view controller")
let first = TestViewController().embedInNavigationController()
let second = TestViewController()
first.pushViewController(second, animated: true)
second.showALRT { (result) in
self.inspect(result)
expectation.fulfill()
}
self.waitForExpectations(timeout: 3.0, handler: nil)
}
func testTextField() {
let expectation = self.expectation(description: "addTextField should attach a textField to alert")
let expectedValue = "Test title"
ALRT.create(.alert, title: "Unit Test Alert")
.addTextField { (textField) in
textField.text = expectedValue
}
.fetch() { alert in
guard let textFields = alert?.textFields, textFields.count == 1 else {
XCTFail("Textfields.count should be 1")
return
}
guard let text = textFields.first?.text else {
XCTFail("Attached textField should have text")
return
}
XCTAssertEqual(text, expectedValue)
expectation.fulfill()
}
self.waitForExpectations(timeout: 3.0, handler: nil)
}
func testDefaultConfiguration() {
defer {
ALRT.defaultConfiguration = nil
}
let expectation = self.expectation(description: "Setting defaultConfiguration should change tintColor, okTitle, cancelTitle")
let expectedTintColor = UIColor.red
let expectedOKTitle = "AAA"
let expectedCancelTitle = "BBB"
ALRT.defaultConfiguration = .init(
tintColor: expectedTintColor,
okTitle: expectedOKTitle,
cancelTitle: expectedCancelTitle
)
ALRT.create(.alert)
.addOK()
.addCancel()
.fetch { alert in
let actualTintColor = alert?.view.tintColor
XCTAssertEqual(actualTintColor, expectedTintColor)
let actualOKTitle = alert?.actions.first?.title
XCTAssertEqual(actualOKTitle, expectedOKTitle)
let actualCancelTitle = alert?.actions.last?.title
XCTAssertEqual(actualCancelTitle, expectedCancelTitle)
expectation.fulfill()
}
.show()
self.waitForExpectations(timeout: 3.0, handler: nil)
}
func inspect(_ result: ALRT.Result) {
if case .success = result {
XCTAssertTrue(true)
} else {
XCTFail()
}
}
}
fileprivate class TestViewController: UIViewController {
func showALRT(_ completionHandler: @escaping (ALRT.Result) -> Void) {
ALRT.create(.alert, title: "Unit Test Alert").addOK().show() {
completionHandler($0)
}
}
}
extension TestViewController {
func embedInNavigationController() -> UINavigationController {
return UINavigationController(rootViewController: self)
}
}