Skip to content

Commit

Permalink
✅ Update PushMonitor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaatttt authored and iujames committed Sep 17, 2024
1 parent ec9c4b0 commit 8c35a9e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
6 changes: 3 additions & 3 deletions Tests/AppcuesKitTests/MockAppcues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,9 @@ class MockPushMonitor: PushMonitoring {
completion?(pushAuthorizationStatus)
}

var onDidReceiveNotification: (([AnyHashable: Any]) -> Bool)?
func didReceiveNotification(userInfo: [AnyHashable : Any], completionHandler: @escaping () -> Void) -> Bool {
let result = onDidReceiveNotification?(userInfo) ?? false
var onDidReceiveNotification: ((UNNotificationResponse) -> Bool)?
func didReceiveNotification(response: UNNotificationResponse, completionHandler: @escaping () -> Void) -> Bool {
let result = onDidReceiveNotification?(response) ?? false
if result {
completionHandler()
}
Expand Down
54 changes: 48 additions & 6 deletions Tests/AppcuesKitTests/Push/PushMonitorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class PushMonitorTests: XCTestCase {
func testReceiveActiveSession() throws {
// Arrange
let userInfo = Dictionary<AnyHashable, Any>.appcuesPush
let response = try XCTUnwrap(UNNotificationResponse.mock(userInfo: userInfo))

let analyticsExpectation = expectation(description: "push open event")
let completionExpectation = expectation(description: "completion called")
Expand All @@ -96,7 +97,7 @@ class PushMonitorTests: XCTestCase {
appcues.sessionID = UUID()

// Act
let result = pushMonitor.didReceiveNotification(userInfo: userInfo, completionHandler: completion)
let result = pushMonitor.didReceiveNotification(response: response, completionHandler: completion)

// Assert
waitForExpectations(timeout: 1.0)
Expand All @@ -108,6 +109,7 @@ class PushMonitorTests: XCTestCase {
var userInfo = Dictionary<AnyHashable, Any>.appcuesPush
userInfo["appcues_deep_link_url"] = "app://some-link"
userInfo["appcues_experience_id"] = "<some-experience>"
let response = try XCTUnwrap(UNNotificationResponse.mock(userInfo: userInfo))

let analyticsExpectation = expectation(description: "push open event")
let linkCompletionExpectation = expectation(description: "link opened")
Expand Down Expand Up @@ -142,7 +144,7 @@ class PushMonitorTests: XCTestCase {
appcues.sessionID = UUID()

// Act
let result = pushMonitor.didReceiveNotification(userInfo: userInfo, completionHandler: completion)
let result = pushMonitor.didReceiveNotification(response: response, completionHandler: completion)

// Assert
waitForExpectations(timeout: 1.0)
Expand All @@ -152,13 +154,14 @@ class PushMonitorTests: XCTestCase {
func testReceiveMalformed() throws {
// Arrange
let userInfo = Dictionary<AnyHashable, Any>.basicPush
let response = try XCTUnwrap(UNNotificationResponse.mock(userInfo: userInfo))

let completion = {
XCTFail("completion should not be called")
}

// Act
let result = pushMonitor.didReceiveNotification(userInfo: userInfo, completionHandler: completion)
let result = pushMonitor.didReceiveNotification(response: response, completionHandler: completion)

// Assert
XCTAssertFalse(result)
Expand All @@ -167,14 +170,15 @@ class PushMonitorTests: XCTestCase {
func testReceiveNoAppcues() throws {
// Arrange
let userInfo = Dictionary<AnyHashable, Any>.appcuesPush
let response = try XCTUnwrap(UNNotificationResponse.mock(userInfo: userInfo))

let completion = {
XCTFail("completion should not be called")
}
appcues = nil

// Act
let result = pushMonitor.didReceiveNotification(userInfo: userInfo, completionHandler: completion)
let result = pushMonitor.didReceiveNotification(response: response, completionHandler: completion)

// Assert
XCTAssertFalse(result)
Expand All @@ -183,6 +187,7 @@ class PushMonitorTests: XCTestCase {
func testReceiveUserIdMismatch() throws {
// Arrange
let userInfo = Dictionary<AnyHashable, Any>.appcuesPush
let response = try XCTUnwrap(UNNotificationResponse.mock(userInfo: userInfo))

let completionExpectation = expectation(description: "completion called")

Expand All @@ -198,7 +203,7 @@ class PushMonitorTests: XCTestCase {
appcues.sessionID = UUID()

// Act
let result = pushMonitor.didReceiveNotification(userInfo: userInfo, completionHandler: completion)
let result = pushMonitor.didReceiveNotification(response: response, completionHandler: completion)

// Assert
waitForExpectations(timeout: 1.0)
Expand All @@ -208,6 +213,7 @@ class PushMonitorTests: XCTestCase {
func testReceiveNoSession() throws {
// Arrange
let userInfo = Dictionary<AnyHashable, Any>.appcuesPush
let response = try XCTUnwrap(UNNotificationResponse.mock(userInfo: userInfo))

let completionExpectation = expectation(description: "completion called")

Expand All @@ -223,7 +229,7 @@ class PushMonitorTests: XCTestCase {
appcues.sessionID = nil

// Act
let result = pushMonitor.didReceiveNotification(userInfo: userInfo, completionHandler: completion)
let result = pushMonitor.didReceiveNotification(response: response, completionHandler: completion)

// Assert
waitForExpectations(timeout: 1.0)
Expand Down Expand Up @@ -270,3 +276,39 @@ private class MockNavigationDelegate: AppcuesNavigationDelegate {
completion(true)
}
}

private extension UNNotificationResponse {
final class KeyedArchiver: NSKeyedArchiver {
override func decodeObject(forKey _: String) -> Any { "" }

deinit {
// Avoid a console warning
finishEncoding()
}
}

static func mock(
userInfo: [AnyHashable: Any],
actionIdentifier: String = UNNotificationDefaultActionIdentifier
) -> UNNotificationResponse? {
guard let response = UNNotificationResponse(coder: KeyedArchiver()),
let notification = UNNotification(coder: KeyedArchiver()) else {
return nil
}

let content = UNMutableNotificationContent()
content.userInfo = userInfo

let request = UNNotificationRequest(
identifier: "",
content: content,
trigger: nil
)
notification.setValue(request, forKey: "request")

response.setValue(notification, forKey: "notification")
response.setValue(actionIdentifier, forKey: "actionIdentifier")

return response
}
}

0 comments on commit 8c35a9e

Please sign in to comment.