-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathProjectNotificationCellViewModelTests.swift
156 lines (123 loc) · 6.62 KB
/
ProjectNotificationCellViewModelTests.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
import Prelude
import ReactiveSwift
import Result
import XCTest
@testable import KsApi
@testable import Library
@testable import ReactiveExtensions_TestHelpers
internal final class ProjectNotificationCellViewModelTests: TestCase {
internal let vm = ProjectNotificationCellViewModel()
internal let projectName = TestObserver<String, NoError>()
internal let notificationOn = TestObserver<Bool, NoError>()
internal let notifyDelegateOfSaveError = TestObserver<String, NoError>()
internal override func setUp() {
super.setUp()
self.vm.outputs.name.observe(projectName.observer)
self.vm.outputs.notificationOn.observe(notificationOn.observer)
self.vm.outputs.notifyDelegateOfSaveError.observe(notifyDelegateOfSaveError.observer)
}
internal func testCellReuse() {
let notification = ProjectNotification.template
self.vm.inputs.configureWith(notification: notification)
self.notificationOn.assertValues([false], "Notification off by default.")
self.vm.inputs.notificationTapped(on: true)
self.scheduler.advance()
self.notificationOn.assertValues([false, true], "Notification turned on.")
self.vm.inputs.configureWith(notification: notification)
self.notificationOn.assertValues([false, true], "Notification stays on despite cell reuse.")
}
internal func testProjectNotificationEmits() {
let notification = ProjectNotification.template
|> ProjectNotification.lens.email .~ true
|> ProjectNotification.lens.mobile .~ true
|> ProjectNotification.lens.project.name .~ "My cool project"
self.vm.inputs.configureWith(notification: notification)
self.projectName.assertValues(["My cool project"], "Notification name emitted.")
self.notificationOn.assertValues([true], "Notification state emitted.")
}
internal func testTappingNotification() {
let notification = ProjectNotification.template
self.vm.inputs.configureWith(notification: notification)
self.notificationOn.assertValues([false], "Notification off by default.")
self.vm.inputs.notificationTapped(on: true)
self.scheduler.advance()
self.notificationOn.assertValues([false, true], "Notification turned on.")
self.notifyDelegateOfSaveError.assertDidNotEmitValue("Notification saved.")
self.vm.inputs.notificationTapped(on: true)
self.notificationOn.assertValues([false, true], "Notification unchanged.")
self.scheduler.advance()
self.notificationOn.assertValues([false, true], "Notification unchanged.")
self.vm.inputs.notificationTapped(on: false)
self.scheduler.advance()
self.notificationOn.assertValues([false, true, false], "Notification turned off.")
self.notifyDelegateOfSaveError.assertDidNotEmitValue("Notification preference saved successfully.")
XCTAssertEqual(["Changed Project Notifications", "Changed Project Notifications",
"Changed Project Notifications"], self.trackingClient.events)
}
internal func testUpdateError() {
let error = ErrorEnvelope(
errorMessages: ["Unable to save."],
ksrCode: .UnknownCode,
httpCode: 400,
exception: nil
)
withEnvironment(apiService: MockService(updateProjectNotificationError: error)) {
let notification = ProjectNotification.template
self.vm.inputs.configureWith(notification: notification)
self.notificationOn.assertValues([false], "Notification off by default.")
self.vm.inputs.notificationTapped(on: true)
self.notificationOn.assertValues([false, true], "Notification immediately turned on on tap.")
self.scheduler.advance()
self.notifyDelegateOfSaveError.assertValueCount(1, "Updating notification errored.")
self.notificationOn.assertValues([false, true, false], "Notification was not successfully saved.")
self.vm.inputs.notificationTapped(on: true)
self.notificationOn.assertValues([false, true, false, true],
"Notification immediately turned on on tap.")
self.scheduler.advance()
self.notifyDelegateOfSaveError.assertValueCount(2, "Updating notification errored.")
self.notificationOn.assertValues([false, true, false, true, false],
"Notification was not successfully saved.")
self.vm.inputs.notificationTapped(on: true)
self.notificationOn.assertValues([false, true, false, true, false, true],
"Notification immediately turned on on tap.")
self.scheduler.advance()
self.notifyDelegateOfSaveError.assertValueCount(3, "Updating notification errored.")
self.notificationOn.assertValues([false, true, false, true, false, true, false],
"Notification still not saved.")
}
}
internal func testErroringWithCellReuse() {
let error = ErrorEnvelope(
errorMessages: ["Unable to save."],
ksrCode: .UnknownCode,
httpCode: 400,
exception: nil
)
withEnvironment(apiService: MockService(updateProjectNotificationError: error)) {
let notification = ProjectNotification.template
self.vm.inputs.configureWith(notification: notification)
self.notificationOn.assertValues([false], "Notification off by default.")
self.vm.inputs.notificationTapped(on: true)
self.notificationOn.assertValues([false, true], "Notification immediately turned on on tap.")
self.scheduler.advance()
self.notifyDelegateOfSaveError.assertValueCount(1, "Updating notification errored.")
self.notificationOn.assertValues([false, true, false], "Notification was not successfully saved.")
self.vm.inputs.configureWith(notification: notification)
self.notificationOn.assertValues([false, true, false], "Notification still off after cell reuse.")
self.vm.inputs.notificationTapped(on: true)
self.notificationOn.assertValues([false, true, false, true], "Notification immediately turned on.")
self.scheduler.advance()
self.notificationOn.assertValues([false, true, false, true, false],
"Notification reverted to off after failure.")
self.vm.inputs.configureWith(notification: notification)
self.notificationOn.assertValues([false, true, false, true, false],
"Notification still off after cell reuse.")
}
self.vm.inputs.notificationTapped(on: true)
self.notificationOn.assertValues([false, true, false, true, false, true],
"Notification turned on on tap.")
self.scheduler.advance()
self.notificationOn.assertValues([false, true, false, true, false, true],
"Notification remains on after API request.")
}
}