-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
FindFriendsViewModelTests.swift
383 lines (268 loc) · 13.6 KB
/
FindFriendsViewModelTests.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// swiftlint:disable force_cast
import XCTest
import ReactiveSwift
import UIKit.UIActivity
@testable import ReactiveExtensions
@testable import ReactiveExtensions_TestHelpers
@testable import Result
@testable import KsApi
@testable import Library
import Prelude
final class FindFriendsViewModelTests: TestCase {
let vm: FindFriendsViewModelType = FindFriendsViewModel()
let friends = TestObserver<[User], NoError>()
let goToDiscovery = TestObserver<DiscoveryParams, NoError>()
let showErrorAlert = TestObserver<AlertError, NoError>()
let showFacebookConnect = TestObserver<Bool, NoError>()
let showFollowAllFriendsAlert = TestObserver<Int, NoError>()
let showLoadingIndicatorView = TestObserver<Bool, NoError>()
let stats = TestObserver<FriendStatsEnvelope, NoError>()
let statsSource = TestObserver<FriendsSource, NoError>()
override func setUp() {
super.setUp()
vm.outputs.friends.map { $0.0 }.observe(friends.observer)
vm.outputs.goToDiscovery.observe(goToDiscovery.observer)
vm.outputs.showErrorAlert.observe(showErrorAlert.observer)
vm.outputs.showFacebookConnect.map { $0.1 }.observe(showFacebookConnect.observer)
vm.outputs.showFollowAllFriendsAlert.observe(showFollowAllFriendsAlert.observer)
vm.outputs.showLoadingIndicatorView.observe(showLoadingIndicatorView.observer)
vm.outputs.stats.map { env, _ in env }.observe(stats.observer)
vm.outputs.stats.map { _, source in source }.observe(statsSource.observer)
}
func testSource() {
vm.inputs.configureWith(source: FriendsSource.findFriends)
vm.inputs.viewDidLoad()
XCTAssertEqual(["Find Friends View", "Viewed Find Friends"], self.trackingClient.events)
XCTAssertEqual(["find-friends", "find-friends"],
self.trackingClient.properties.map { $0["source"] as! String? })
XCTAssertEqual([true, nil], self.trackingClient.properties.map { $0[Koala.DeprecatedKey] as! Bool? })
vm.inputs.configureWith(source: FriendsSource.activity)
vm.inputs.viewDidLoad()
XCTAssertEqual(["Find Friends View", "Viewed Find Friends",
"Find Friends View", "Viewed Find Friends"], self.trackingClient.events)
XCTAssertEqual(["find-friends", "find-friends",
"activity", "activity"],
self.trackingClient.properties.map { $0["source"] as! String? })
vm.inputs.configureWith(source: FriendsSource.discovery)
vm.inputs.viewDidLoad()
XCTAssertEqual(["Find Friends View", "Viewed Find Friends",
"Find Friends View", "Viewed Find Friends",
"Find Friends View", "Viewed Find Friends"], self.trackingClient.events)
XCTAssertEqual(["find-friends", "find-friends",
"activity", "activity",
"discovery", "discovery"],
self.trackingClient.properties.map { $0["source"] as! String? })
vm.inputs.configureWith(source: FriendsSource.settings)
vm.inputs.viewDidLoad()
XCTAssertEqual(["Find Friends View", "Viewed Find Friends",
"Find Friends View", "Viewed Find Friends",
"Find Friends View", "Viewed Find Friends",
"Find Friends View", "Viewed Find Friends"], self.trackingClient.events)
XCTAssertEqual(["find-friends", "find-friends",
"activity", "activity",
"discovery", "discovery",
"settings", "settings"], self.trackingClient.properties.map { $0["source"] as! String? })
}
func testGoToDiscovery() {
let params = DiscoveryParams.defaults
|> DiscoveryParams.lens.social .~ true
|> DiscoveryParams.lens.sort .~ .magic
withEnvironment(currentUser: User.template) {
vm.inputs.configureWith(source: FriendsSource.findFriends)
vm.inputs.viewDidLoad()
goToDiscovery.assertValueCount(0)
vm.inputs.discoverButtonTapped()
goToDiscovery.assertValues([params], "Go to Discovery emits with 'Friends Backed' params")
}
}
func testFriends_WithFacebookConnectedUser() {
withEnvironment(currentUser: User.template |> User.lens.facebookConnected .~ true) {
vm.inputs.configureWith(source: FriendsSource.activity)
friends.assertValueCount(0, "Friends does not emit")
showFacebookConnect.assertValueCount(0, "Facebook connect does not emit")
vm.inputs.viewDidLoad()
showLoadingIndicatorView.assertValues([true])
self.scheduler.advance()
showLoadingIndicatorView.assertValues([true, false])
friends.assertValueCount(1, "Friends emit")
showFacebookConnect.assertValues([false])
vm.inputs.willDisplayRow(30, outOf: 10)
showLoadingIndicatorView.assertValues([true, false])
friends.assertValueCount(1, "Friends value has not changed")
showFacebookConnect.assertValues([false], "Show Facebook Connect value has not changed")
self.scheduler.advance()
showLoadingIndicatorView.assertValues([true, false])
friends.assertValueCount(2, "Friends emits again")
showFacebookConnect.assertValues([false], "Show Facebook Connect value has not changed")
}
}
func testFriends_WithNonFacebookConnectedUser() {
withEnvironment(currentUser: User.template) {
friends.assertValueCount(0, "Friends does not emit")
showFacebookConnect.assertValueCount(0, "Facebook connect does not emit")
vm.inputs.configureWith(source: FriendsSource.discovery)
showFacebookConnect.assertValueCount(0, "Show Facebook Connect does not emit")
vm.inputs.viewDidLoad()
self.scheduler.advance()
friends.assertValueCount(0, "Friends does not emit")
showFacebookConnect.assertValues([true], "Show Facebook Connect")
AppEnvironment.updateCurrentUser(User.template |> User.lens.facebookConnected .~ true)
vm.inputs.findFriendsFacebookConnectCellDidFacebookConnectUser()
showLoadingIndicatorView.assertValues([true])
self.scheduler.advance()
showLoadingIndicatorView.assertValues([true, false])
friends.assertValueCount(1, "Friends emit after Facebook Connected")
showFacebookConnect.assertValues([true, false], "Hide Facebook Connect")
vm.inputs.willDisplayRow(20, outOf: 10)
showLoadingIndicatorView.assertValues([true, false])
friends.assertValueCount(1, "Friends value has not changed")
showFacebookConnect.assertValues([true, false], "Show Facebook Connect value has not changed")
self.scheduler.advance()
showLoadingIndicatorView.assertValues([true, false])
friends.assertValueCount(2, "Friends emits again")
showFacebookConnect.assertValues([true, false], "Show Facebook Connect value has not changed")
}
}
func testStats_WithFacebookConnectedUser() {
withEnvironment(currentUser: User.template |> User.lens.facebookConnected .~ true) {
vm.inputs.configureWith(source: FriendsSource.activity)
stats.assertValueCount(0)
statsSource.assertValueCount(0)
vm.inputs.viewDidLoad()
self.scheduler.advance()
stats.assertValueCount(1)
statsSource.assertValues([FriendsSource.activity])
}
}
func testStats_WithNonFacebookConnectedUser() {
withEnvironment(currentUser: User.template) {
vm.inputs.configureWith(source: FriendsSource.activity)
stats.assertValueCount(0)
statsSource.assertValueCount(0)
vm.inputs.viewDidLoad()
self.scheduler.advance()
stats.assertValueCount(0, "Stats should not emit if user isn't FB Connected")
statsSource.assertValueCount(0)
}
}
func testFollowAllFriendsFlow() {
let friendsResponse = FindFriendsEnvelope.template
let user = .template
|> User.lens.facebookConnected .~ true
withEnvironment(apiService: MockService(fetchFriendsResponse: friendsResponse), currentUser: user) {
self.vm.inputs.configureWith(source: FriendsSource.activity)
self.vm.inputs.viewDidLoad()
self.vm.inputs.willDisplayRow(0, outOf: 2)
self.scheduler.advance()
self.friends.assertValues([friendsResponse.users], "Initial friends load.")
XCTAssertEqual(["Find Friends View", "Viewed Find Friends"], self.trackingClient.events)
XCTAssertEqual(["activity", "activity"],
self.trackingClient.properties.map { $0["source"] as! String? })
self.vm.inputs.findFriendsStatsCellShowFollowAllFriendsAlert(friendCount: 1000)
self.showFollowAllFriendsAlert.assertValues([1000], "Show Follow All Friends alert with friend count")
self.vm.inputs.declineFollowAllFriends()
XCTAssertEqual(["Find Friends View", "Viewed Find Friends",
"Facebook Friend Decline Follow All", "Declined Follow All Facebook Friends"],
self.trackingClient.events)
XCTAssertEqual(["activity", "activity",
"activity", "activity"],
self.trackingClient.properties.map { $0["source"] as! String? })
self.vm.inputs.findFriendsStatsCellShowFollowAllFriendsAlert(friendCount: 1000)
self.showFollowAllFriendsAlert.assertValues([1000, 1000])
self.vm.inputs.confirmFollowAllFriends()
XCTAssertEqual(
["Find Friends View", "Viewed Find Friends",
"Facebook Friend Decline Follow All", "Declined Follow All Facebook Friends",
"Facebook Friend Follow All", "Followed All Facebook Friends"], self.trackingClient.events
)
XCTAssertEqual(["activity", "activity",
"activity", "activity",
"activity", "activity"],
self.trackingClient.properties.map { $0["source"] as! String? })
// Test the 2 second "Follow all" debounce.
self.scheduler.advance(by: .seconds(1))
self.friends.assertValues([friendsResponse.users, []], "Friend list clears.")
self.scheduler.advance(by: .seconds(1))
self.friends.assertValues([friendsResponse.users, [], friendsResponse.users], "Updated friends emit.")
}
}
func testLoaderIsAnimating_WithFacebookConnectedUser() {
withEnvironment(currentUser: User.template |> User.lens.facebookConnected .~ true) {
vm.inputs.configureWith(source: FriendsSource.activity)
showLoadingIndicatorView.assertValueCount(0, "Loader is hidden")
vm.inputs.viewDidLoad()
showLoadingIndicatorView.assertValues([true])
self.scheduler.advance()
showLoadingIndicatorView.assertValues([true, false])
vm.inputs.willDisplayRow(30, outOf: 10)
showLoadingIndicatorView.assertValues([true, false])
self.scheduler.advance()
showLoadingIndicatorView.assertValues([true, false])
}
}
func testLoadedMoreFriendsReporting() {
withEnvironment(currentUser: .template |> User.lens.facebookConnected .~ true) {
self.vm.inputs.configureWith(source: FriendsSource.activity)
self.vm.inputs.viewDidLoad()
self.scheduler.advance()
//This call should NOT trigger next page request and reporting
self.vm.inputs.willDisplayRow(5, outOf: 10)
self.scheduler.advance()
XCTAssertEqual(
["Find Friends View", "Viewed Find Friends"], self.trackingClient.events)
XCTAssertEqual(["activity", "activity"],
self.trackingClient.properties.map { $0["source"] as! String? })
XCTAssertEqual([nil, nil],
self.trackingClient.properties.map { $0["page_count"] as! String? })
//This call should trigger next page request and reporting
self.vm.inputs.willDisplayRow(8, outOf: 10)
self.scheduler.advance()
XCTAssertEqual(
["Find Friends View", "Viewed Find Friends", "Loaded More Friends"], self.trackingClient.events)
XCTAssertEqual(["activity", "activity", "activity"],
self.trackingClient.properties.map { $0["source"] as! String? })
XCTAssertEqual([nil, nil, 2],
self.trackingClient.properties.map { $0["page_count"] as! Int? })
}
}
func testLoaderIsAnimating_WithNonFacebookConnectedUser() {
withEnvironment(currentUser: User.template) {
vm.inputs.configureWith(source: FriendsSource.activity)
showLoadingIndicatorView.assertValueCount(0, "Loader is hidden")
vm.inputs.viewDidLoad()
showLoadingIndicatorView.assertDidNotEmitValue()
self.scheduler.advance()
showLoadingIndicatorView.assertDidNotEmitValue()
}
}
func testFacebookErrorAlerts() {
vm.inputs.configureWith(source: FriendsSource.discovery)
showFacebookConnect.assertValueCount(0, "Show Facebook Connect does not emit")
vm.inputs.viewDidLoad()
self.scheduler.advance()
showFacebookConnect.assertValues([true], "Show Facebook Connect")
let alertTokenFail = AlertError.facebookTokenFail
self.vm.inputs.findFriendsFacebookConnectCellShowErrorAlert(alertTokenFail)
self.showErrorAlert.assertValues([alertTokenFail])
let errorAccountTaken = ErrorEnvelope(
errorMessages: ["This Facebook account is already linked to another Kickstarter user."],
ksrCode: .FacebookConnectAccountTaken,
httpCode: 403,
exception: nil
)
let alertAccountTaken = AlertError.facebookConnectAccountTaken(envelope: errorAccountTaken)
self.vm.inputs.findFriendsFacebookConnectCellShowErrorAlert(alertAccountTaken)
self.showErrorAlert.assertValues([alertTokenFail, alertAccountTaken])
let errorEmailTaken = ErrorEnvelope(
errorMessages: [
"The email associated with this Facebook account is already registered to another Kickstarter user."
],
ksrCode: .FacebookConnectEmailTaken,
httpCode: 403,
exception: nil
)
let alertEmailTaken = AlertError.facebookConnectEmailTaken(envelope: errorEmailTaken)
self.vm.inputs.findFriendsFacebookConnectCellShowErrorAlert(alertEmailTaken)
self.showErrorAlert.assertValues([alertTokenFail, alertAccountTaken, alertEmailTaken])
}
}