-
Notifications
You must be signed in to change notification settings - Fork 9
/
ExampleTests.swift
210 lines (186 loc) · 7.03 KB
/
ExampleTests.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
import ComposableArchitecture
import ComposableUserNotifications
import struct ComposableUserNotifications.Notification
import XCTest
@testable import Example
@MainActor
class ExampleTests: XCTestCase {
func testApplicationLaunchWithoutNotification() async throws {
let delegate = AsyncStream<UserNotificationClient.DelegateAction>.makeStream()
let requestedAuthorizationOptions = ActorIsolated<UNAuthorizationOptions?>(nil)
let store = TestStore(
initialState: App.State(count: nil),
reducer: { App() }
)
store.dependencies.userNotifications.delegate = { delegate.stream }
store.dependencies.userNotifications.requestAuthorization = { options in
await requestedAuthorizationOptions.setValue(options)
return true
}
let task = await store.send(.didFinishLaunching)
await store.receive(.requestAuthorizationResponse(.success(true)))
await requestedAuthorizationOptions.withValue {
XCTAssertNoDifference($0, [.alert, .badge, .sound])
}
await task.cancel()
}
func testNotificationPresentationHandling() async throws {
let delegate = AsyncStream<UserNotificationClient.DelegateAction>.makeStream()
let store = TestStore(
initialState: App.State(count: nil),
reducer: { App() }
)
store.dependencies.userNotifications.requestAuthorization = { _ in true }
store.dependencies.userNotifications.delegate = { delegate.stream }
let task = await store.send(.didFinishLaunching)
await store.receive(.requestAuthorizationResponse(.success(true)))
var notificationPresentationOptions: UNNotificationPresentationOptions?
let willPresentNotificationCompletionHandler = { notificationPresentationOptions = $0 }
let content = UNMutableNotificationContent()
content.userInfo = ["count": 5]
let notification = Notification(
date: Date(timeIntervalSince1970: 0),
request: Notification.Request(
identifier: "fixture",
content: content,
trigger: nil
)
)
delegate.continuation.yield(
.willPresentNotification(
notification,
completionHandler: { willPresentNotificationCompletionHandler($0) }
)
)
await store.receive(
.userNotifications(
.willPresentNotification(
notification,
completionHandler: willPresentNotificationCompletionHandler
)
)
)
XCTAssertNoDifference(notificationPresentationOptions, [.list, .banner, .sound])
await task.cancel()
}
func testReceivedNotification() async throws {
let delegate = AsyncStream<UserNotificationClient.DelegateAction>.makeStream()
let store = TestStore(
initialState: App.State(count: nil),
reducer: { App() }
)
store.dependencies.userNotifications.requestAuthorization = { _ in true }
store.dependencies.userNotifications.delegate = { delegate.stream }
let task = await store.send(.didFinishLaunching)
await store.receive(.requestAuthorizationResponse(.success(true)))
var didReceiveResponseCompletionHandlerCalled = false
let didReceiveResponseCompletionHandler = { didReceiveResponseCompletionHandlerCalled = true }
let content = UNMutableNotificationContent()
content.userInfo = ["count": 5]
let response = Notification.Response.user(
Notification.Response.UserAction(
actionIdentifier: "fixture",
notification: Notification(
date: Date(timeIntervalSince1970: 0),
request: Notification.Request(
identifier: "fixture",
content: content,
trigger: nil
)
)
)
)
delegate.continuation.yield(
.didReceiveResponse(response, completionHandler: { didReceiveResponseCompletionHandler() })
)
await store.receive(
.userNotifications(
.didReceiveResponse(
response,
completionHandler: didReceiveResponseCompletionHandler
)
)
) {
$0.count = 5
}
XCTAssert(didReceiveResponseCompletionHandlerCalled)
await task.cancel()
}
func testReceiveBackgroundNotification() async throws {
let store = TestStore(
initialState: App.State(count: nil),
reducer: { App() }
)
store.dependencies.remote.fetchRemoteCount = { 5 }
var backgroundFetchResult: UIBackgroundFetchResult?
let backgroundNotification = BackgroundNotification(
appState: .inactive,
content: .countAvailable,
fetchCompletionHandler: { backgroundFetchResult = $0 }
)
await store.send(.didReceiveBackgroundNotification(backgroundNotification))
await store.receive(.remoteCountResponse(.success(5))) {
$0.count = 5
}
XCTAssertNoDifference(backgroundFetchResult, .newData)
}
func testReceiveBackgroundNotificationFailure() async throws {
let store = TestStore(
initialState: App.State(count: nil),
reducer: { App() }
)
struct Error: Swift.Error, Equatable {}
store.dependencies.remote.fetchRemoteCount = { throw Error() }
var backgroundFetchResult: UIBackgroundFetchResult?
let backgroundNotification = BackgroundNotification(
appState: .inactive,
content: .countAvailable,
fetchCompletionHandler: { backgroundFetchResult = $0 }
)
await store.send(.didReceiveBackgroundNotification(backgroundNotification))
await store.receive(.remoteCountResponse(.failure(Error())))
XCTAssertNoDifference(backgroundFetchResult, .failed)
}
func testReceiveBackgroundNotificationWithoutContent() async throws {
let store = TestStore(
initialState: App.State(count: nil),
reducer: { App() }
)
var backgroundFetchResult: UIBackgroundFetchResult?
let backgroundNotification = BackgroundNotification(
appState: .inactive,
content: nil,
fetchCompletionHandler: { backgroundFetchResult = $0 }
)
await store.send(.didReceiveBackgroundNotification(backgroundNotification))
XCTAssertNoDifference(backgroundFetchResult, .noData)
}
func testTappedScheduleButton() async throws {
let store = TestStore(
initialState: App.State(count: nil),
reducer: { App() }
)
let notificationRequest = ActorIsolated<UNNotificationRequest?>(nil)
let removedPendingIdentifiers = ActorIsolated<[String]?>(nil)
store.dependencies.userNotifications.add = { request in
await notificationRequest.setValue(request)
}
store.dependencies.userNotifications.removePendingNotificationRequestsWithIdentifiers = { identifiers in
await removedPendingIdentifiers.setValue(identifiers)
}
await store.send(.tappedScheduleButton)
await store.receive(.addNotificationResponse(.success(Unit())))
await removedPendingIdentifiers.withValue {
XCTAssertNoDifference($0, ["example_notification"])
}
await notificationRequest.withValue {
XCTAssertEqual($0?.content.title, "Example title")
XCTAssertEqual($0?.content.body, "Example body")
XCTAssertTrue($0?.trigger is UNTimeIntervalNotificationTrigger)
XCTAssertEqual(
($0?.trigger as? UNTimeIntervalNotificationTrigger)?.timeInterval,
2
)
}
}
}