From 8c35a9e431e9d9e782c043fd007eb70b7eccb5f7 Mon Sep 17 00:00:00 2001 From: Matt Hayashida Date: Thu, 14 Mar 2024 13:36:22 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Update=20PushMonitor=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tests/AppcuesKitTests/MockAppcues.swift | 6 +-- .../Push/PushMonitorTests.swift | 54 ++++++++++++++++--- 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/Tests/AppcuesKitTests/MockAppcues.swift b/Tests/AppcuesKitTests/MockAppcues.swift index 207016593..0666f224c 100644 --- a/Tests/AppcuesKitTests/MockAppcues.swift +++ b/Tests/AppcuesKitTests/MockAppcues.swift @@ -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() } diff --git a/Tests/AppcuesKitTests/Push/PushMonitorTests.swift b/Tests/AppcuesKitTests/Push/PushMonitorTests.swift index 8cca50914..7cd2b19e2 100644 --- a/Tests/AppcuesKitTests/Push/PushMonitorTests.swift +++ b/Tests/AppcuesKitTests/Push/PushMonitorTests.swift @@ -79,6 +79,7 @@ class PushMonitorTests: XCTestCase { func testReceiveActiveSession() throws { // Arrange let userInfo = Dictionary.appcuesPush + let response = try XCTUnwrap(UNNotificationResponse.mock(userInfo: userInfo)) let analyticsExpectation = expectation(description: "push open event") let completionExpectation = expectation(description: "completion called") @@ -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) @@ -108,6 +109,7 @@ class PushMonitorTests: XCTestCase { var userInfo = Dictionary.appcuesPush userInfo["appcues_deep_link_url"] = "app://some-link" userInfo["appcues_experience_id"] = "" + let response = try XCTUnwrap(UNNotificationResponse.mock(userInfo: userInfo)) let analyticsExpectation = expectation(description: "push open event") let linkCompletionExpectation = expectation(description: "link opened") @@ -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) @@ -152,13 +154,14 @@ class PushMonitorTests: XCTestCase { func testReceiveMalformed() throws { // Arrange let userInfo = Dictionary.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) @@ -167,6 +170,7 @@ class PushMonitorTests: XCTestCase { func testReceiveNoAppcues() throws { // Arrange let userInfo = Dictionary.appcuesPush + let response = try XCTUnwrap(UNNotificationResponse.mock(userInfo: userInfo)) let completion = { XCTFail("completion should not be called") @@ -174,7 +178,7 @@ class PushMonitorTests: XCTestCase { appcues = nil // Act - let result = pushMonitor.didReceiveNotification(userInfo: userInfo, completionHandler: completion) + let result = pushMonitor.didReceiveNotification(response: response, completionHandler: completion) // Assert XCTAssertFalse(result) @@ -183,6 +187,7 @@ class PushMonitorTests: XCTestCase { func testReceiveUserIdMismatch() throws { // Arrange let userInfo = Dictionary.appcuesPush + let response = try XCTUnwrap(UNNotificationResponse.mock(userInfo: userInfo)) let completionExpectation = expectation(description: "completion called") @@ -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) @@ -208,6 +213,7 @@ class PushMonitorTests: XCTestCase { func testReceiveNoSession() throws { // Arrange let userInfo = Dictionary.appcuesPush + let response = try XCTUnwrap(UNNotificationResponse.mock(userInfo: userInfo)) let completionExpectation = expectation(description: "completion called") @@ -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) @@ -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 + } +}