-
Notifications
You must be signed in to change notification settings - Fork 1
/
RxTestExample.swift
176 lines (148 loc) · 5.97 KB
/
RxTestExample.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
164
165
166
167
168
169
170
171
172
173
174
175
176
import XCTest
import Tablier
import RxSwift
import RxTest
@testable import Example
/// RxTestExample
final class ViewModelTests: XCTestCase {
func testLogin() {
struct Input {
let rememberMe: Bool
let validatorResult: Bool
let apiResult: Bool
}
struct Output: Equatable {
let messageEvents: [Recorded<Event<String>>]
let validatorEvents: [Recorded<Event<Credential>>]
let apiClientEvents: [Recorded<Event<Credential>>]
let credentialStoreEvents: [Recorded<Event<Credential>>]
}
let testCredential = Credential(username: "username", password: "password")
let recipe = Recipe<Input, Output>(sync: { input in
let disposeBag = DisposeBag()
let scheduler = TestScheduler(initialClock: 0)
let validator = MockValidator(scheduler: scheduler, result: input.validatorResult)
let apiClient = MockAPIClient(scheduler: scheduler, result: input.apiResult)
let credentialStore = MockCredentialStore(scheduler: scheduler)
let messageObserver = scheduler.createObserver(String.self)
let viewModel = ViewModel(validator: validator, apiClient: apiClient, credentialStore: credentialStore)
viewModel.message.subscribe(messageObserver).disposed(by: disposeBag)
viewModel.username.accept(testCredential.username)
viewModel.password.accept(testCredential.password)
viewModel.rememberMe.accept(input.rememberMe)
scheduler.createHotObservable(Recorded.events(.next(100, ())))
.bind(to: viewModel.loginButtonTap)
.disposed(by: disposeBag)
scheduler.start()
return Output(
messageEvents: messageObserver.events,
validatorEvents: validator.observer.events,
apiClientEvents: apiClient.observer.events,
credentialStoreEvents: credentialStore.observer.events
)
})
recipe.assert(with: self) {
$0.when(.init(rememberMe: false, validatorResult: false, apiResult: false),
.init(rememberMe: false, validatorResult: false, apiResult: true))
.expect(.init(
messageEvents: Recorded.events(
.next(0, ""),
.next(100, "invalid")
),
validatorEvents: Recorded.events(
.next(0, testCredential)
),
apiClientEvents: Recorded.events(),
credentialStoreEvents: Recorded.events()
))
$0.when(.init(rememberMe: true, validatorResult: true, apiResult: false))
.expect(.init(
messageEvents: Recorded.events(
.next(0, ""),
.next(100, "loading"),
.next(200, "error")
),
validatorEvents: Recorded.events(
.next(0, testCredential)
),
apiClientEvents: Recorded.events(
.next(100, testCredential)
),
credentialStoreEvents: Recorded.events()
))
$0.when(.init(rememberMe: false, validatorResult: true, apiResult: true))
.expect(.init(
messageEvents: Recorded.events(
.next(0, ""),
.next(100, "loading"),
.next(200, "success")
),
validatorEvents: Recorded.events(
.next(0, testCredential)
),
apiClientEvents: Recorded.events(
.next(100, testCredential)
),
credentialStoreEvents: Recorded.events()
))
$0.when(.init(rememberMe: true, validatorResult: true, apiResult: true))
.expect(.init(
messageEvents: Recorded.events(
.next(0, ""),
.next(100, "loading"),
.next(200, "success")
),
validatorEvents: Recorded.events(
.next(0, testCredential)
),
apiClientEvents: Recorded.events(
.next(100, testCredential)
),
credentialStoreEvents: Recorded.events(
.next(200, testCredential)
)
))
}
}
}
struct RandomError: Error {}
struct MockValidator: ValidatorType {
let observer: TestableObserver<Credential>
let result: Bool
init(scheduler: TestScheduler, result: Bool) {
self.observer = scheduler.createObserver(Credential.self)
self.result = result
}
func validate(credential: Credential) -> Bool {
observer.onNext(credential)
return result
}
}
struct MockAPIClient: APIClientType {
let observer: TestableObserver<Credential>
let scheduler: TestScheduler
let result: Bool
init(scheduler: TestScheduler, result: Bool) {
self.observer = scheduler.createObserver(Credential.self)
self.scheduler = scheduler
self.result = result
}
func login(credential: Credential) -> Single<Void> {
observer.onNext(credential)
return Single
.just(())
.delay(.seconds(100), scheduler: scheduler)
.map { [result] in
if !result { throw RandomError() }
}
}
}
struct MockCredentialStore: CredentialStoreType {
let observer: TestableObserver<Credential>
init(scheduler: TestScheduler) {
self.observer = scheduler.createObserver(Credential.self)
}
func save(credential: Credential) {
observer.onNext(credential)
}
}