From fd30c9d2d2199186f4870cdbbbeae260de29ff78 Mon Sep 17 00:00:00 2001 From: Matt Hayashida Date: Mon, 26 Feb 2024 12:34:11 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Add=20PushMonitor=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AutoPropertyDecoratorTests.swift | 2 +- Tests/AppcuesKitTests/MockAppcues.swift | 7 +++ Tests/AppcuesKitTests/PushMonitorTests.swift | 63 +++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 Tests/AppcuesKitTests/PushMonitorTests.swift diff --git a/Tests/AppcuesKitTests/Analytics/AutoPropertyDecoratorTests.swift b/Tests/AppcuesKitTests/Analytics/AutoPropertyDecoratorTests.swift index c67fe7397..cc7f9c847 100644 --- a/Tests/AppcuesKitTests/Analytics/AutoPropertyDecoratorTests.swift +++ b/Tests/AppcuesKitTests/Analytics/AutoPropertyDecoratorTests.swift @@ -56,7 +56,7 @@ class AutoPropertyDecoratorTests: XCTestCase { XCTAssertEqual([], Set(try XCTUnwrap(decorated.properties).keys).symmetricDifference(expectedPropertyKeys)) let expectedEventAutoPropertyKeys = ["userId", "_deviceModel", "_bundlePackageId", "_lastBrowserLanguage", "_localId", "_appName", "_lastSeenAt", "_updatedAt", "_sdkVersion", "_osVersion", "_operatingSystem", "_deviceType", "_appVersion", "_isAnonymous", "_appBuild", "_sdkName", "_appId", "_sessionPageviews", "_sessionRandomizer", "_sessionId"] XCTAssertEqual([], Set(try XCTUnwrap(decorated.identityAutoProperties).keys).symmetricDifference(expectedEventAutoPropertyKeys)) - let expectedEventDeviceAutoPropertyKeys = ["_deviceId", "_language", "_pushToken", "_deviceType", "_appBuild", "_appId", "_operatingSystem", "_bundlePackageId", "_deviceModel", "_appVersion", "_sdkVersion", "_osVersion", "_sdkName", "_appName"] + let expectedEventDeviceAutoPropertyKeys = ["_deviceId", "_language", "_pushToken", "_pushEnabledBackground", "_pushEnabled", "_deviceType", "_appBuild", "_appId", "_operatingSystem", "_bundlePackageId", "_deviceModel", "_appVersion", "_sdkVersion", "_osVersion", "_sdkName", "_appName"] XCTAssertEqual([], Set(try XCTUnwrap(decorated.deviceAutoProperties).keys).symmetricDifference(expectedEventDeviceAutoPropertyKeys)) } diff --git a/Tests/AppcuesKitTests/MockAppcues.swift b/Tests/AppcuesKitTests/MockAppcues.swift index 3f1a70f52..6c226d915 100644 --- a/Tests/AppcuesKitTests/MockAppcues.swift +++ b/Tests/AppcuesKitTests/MockAppcues.swift @@ -28,6 +28,7 @@ class MockAppcues: Appcues { container.register(ActivityProcessing.self, value: activityProcessor) container.register(ActivityStoring.self, value: activityStorage) container.register(AnalyticsTracking.self, value: analyticsTracker) + container.register(PushMonitoring.self, value: pushMonitor) if #available(iOS 13.0, *) { container.register(DeepLinkHandling.self, value: deepLinkHandler) @@ -62,6 +63,7 @@ class MockAppcues: Appcues { var activityStorage = MockActivityStorage() var networking = MockNetworking() var analyticsTracker = MockAnalyticsTracker() + var pushMonitor = MockPushMonitor() // must wrap in @available since MockExperienceRenderer has a stored property with // type ExperienceData in it, which is 13+ @@ -365,3 +367,8 @@ class MockAnalyticsTracker: AnalyticsTracking { onFlush?() } } + +class MockPushMonitor: PushMonitoring { + var pushEnabled: Bool = false + var pushBackgroundEnabled: Bool = false +} diff --git a/Tests/AppcuesKitTests/PushMonitorTests.swift b/Tests/AppcuesKitTests/PushMonitorTests.swift new file mode 100644 index 000000000..dd0a7f90f --- /dev/null +++ b/Tests/AppcuesKitTests/PushMonitorTests.swift @@ -0,0 +1,63 @@ +// +// PushMonitorTests.swift +// AppcuesKitTests +// +// Created by Matt on 2024-02-26. +// Copyright © 2024 Appcues. All rights reserved. +// + +import XCTest +@testable import AppcuesKit + +class PushMonitorTests: XCTestCase { + + var pushMonitor: PushMonitor! + var appcues: MockAppcues! + + override func setUp() { + let config = Appcues.Config(accountID: "00000", applicationID: "abc") + appcues = MockAppcues(config: config) + pushMonitor = PushMonitor(container: appcues.container) + } + + func testNoTokenNotAuthorized() throws { + // Arrange + appcues.storage.pushToken = nil + pushMonitor.mockPushStatus(.denied) + + // Assert + XCTAssertFalse(pushMonitor.pushEnabled) + XCTAssertFalse(pushMonitor.pushBackgroundEnabled) + } + + func testNoTokenAuthorized() throws { + // Arrange + appcues.storage.pushToken = nil + pushMonitor.mockPushStatus(.authorized) + + // Assert + XCTAssertFalse(pushMonitor.pushEnabled) + XCTAssertFalse(pushMonitor.pushBackgroundEnabled) + } + + func testTokenNotAuthorized() throws { + // Arrange + appcues.storage.pushToken = "" + pushMonitor.mockPushStatus(.denied) + + // Assert + XCTAssertFalse(pushMonitor.pushEnabled) + XCTAssertTrue(pushMonitor.pushBackgroundEnabled) + } + + func testTokenAuthorized() throws { + // Arrange + appcues.storage.pushToken = "" + pushMonitor.mockPushStatus(.authorized) + + // Assert + XCTAssertTrue(pushMonitor.pushEnabled) + XCTAssertTrue(pushMonitor.pushBackgroundEnabled) + } + +}