Skip to content

Commit

Permalink
✅ Add push primer 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 7a093de commit bdd8e44
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 5 deletions.
67 changes: 67 additions & 0 deletions Tests/AppcuesKitTests/Actions/AppcuesRequestPushActionTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// AppcuesRequestPushActionTests.swift
// AppcuesKitTests
//
// Created by Matt on 2024-02-27.
// Copyright © 2024 Appcues. All rights reserved.
//

import XCTest
@testable import AppcuesKit

@available(iOS 13.0, *)
class AppcuesRequestPushActionTests: XCTestCase {

var appcues: MockAppcues!

override func setUpWithError() throws {
appcues = MockAppcues()
}

func testInit() throws {
// Act
let action = AppcuesRequestPushAction(configuration: AppcuesExperiencePluginConfiguration(nil, appcues: appcues))

// Assert
XCTAssertEqual(AppcuesRequestPushAction.type, "@appcues/request-push")
XCTAssertNotNil(action)
}

func testExecute() throws {
// Arrange
var eventCount = 0
var completionCount = 0
let action = AppcuesRequestPushAction(appcues: appcues)

appcues.analyticsPublisher.onPublish = { update in
XCTAssertEqual(update.type, .event(name: Events.Device.deviceUpdated.rawValue, interactive: false))
eventCount += 1
}

// Act
action?.execute(completion: { completionCount += 1 })

// Assert
XCTAssertEqual(eventCount, 1)
XCTAssertEqual(completionCount, 1)
}

func testExecuteCompletesWithoutAppcuesInstance() throws {
// Arrange
var completionCount = 0
let action = try XCTUnwrap(AppcuesRequestPushAction(appcues: nil))

// Act
action.execute(completion: { completionCount += 1 })

// Assert
XCTAssertEqual(completionCount, 1)
}
}

@available(iOS 13.0, *)
extension AppcuesRequestPushAction {
convenience init?(appcues: Appcues?) {
self.init(configuration: AppcuesExperiencePluginConfiguration(nil, appcues: appcues))
}
}
10 changes: 5 additions & 5 deletions Tests/AppcuesKitTests/Analytics/AutoPropertyDecoratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class AutoPropertyDecoratorTests: XCTestCase {
XCTAssertEqual([], Set(try XCTUnwrap(decorated.context).keys).symmetricDifference(expectedContextKeys))
let expectedPropertyKeys = ["_identity", "CUSTOM"]
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", "_currentScreenTitle", "_sessionId"]
let expectedEventAutoPropertyKeys = ["userId", "_deviceModel", "_bundlePackageId", "_lastBrowserLanguage", "_localId", "_appName", "_lastSeenAt", "_updatedAt", "_sdkVersion", "_osVersion", "_operatingSystem", "_deviceType", "_appVersion", "_isAnonymous", "_appBuild", "_sdkName", "_appId", "_sessionPageviews", "_currentScreenTitle", "_sessionId", "_pushPrimerEligible"]
XCTAssertEqual([], Set(try XCTUnwrap(decorated.identityAutoProperties).keys).symmetricDifference(expectedEventAutoPropertyKeys))
XCTAssertNil(decorated.deviceAutoProperties)
}
Expand All @@ -54,7 +54,7 @@ class AutoPropertyDecoratorTests: XCTestCase {
XCTAssertEqual([], Set(try XCTUnwrap(decorated.context).keys).symmetricDifference(expectedContextKeys))
let expectedPropertyKeys = ["_identity", "_device", "CUSTOM"]
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"]
let expectedEventAutoPropertyKeys = ["userId", "_deviceModel", "_bundlePackageId", "_lastBrowserLanguage", "_localId", "_appName", "_lastSeenAt", "_updatedAt", "_sdkVersion", "_osVersion", "_operatingSystem", "_deviceType", "_appVersion", "_isAnonymous", "_appBuild", "_sdkName", "_appId", "_sessionPageviews", "_sessionRandomizer", "_sessionId", "_pushPrimerEligible"]
XCTAssertEqual([], Set(try XCTUnwrap(decorated.identityAutoProperties).keys).symmetricDifference(expectedEventAutoPropertyKeys))
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))
Expand All @@ -70,7 +70,7 @@ class AutoPropertyDecoratorTests: XCTestCase {
// Assert
let expectedContextKeys = ["app_id", "app_version"]
XCTAssertEqual([], Set(try XCTUnwrap(decorated.context).keys).symmetricDifference(expectedContextKeys))
let expectedPropertyKeys = ["CUSTOM", "userId", "_deviceModel", "_bundlePackageId", "_lastBrowserLanguage", "_localId", "_appName", "_lastSeenAt", "_updatedAt", "_sdkVersion", "_osVersion", "_operatingSystem", "_deviceType", "_appVersion", "_isAnonymous", "_appBuild", "_sdkName", "_appId", "_sessionPageviews", "_sessionId"]
let expectedPropertyKeys = ["CUSTOM", "userId", "_deviceModel", "_bundlePackageId", "_lastBrowserLanguage", "_localId", "_appName", "_lastSeenAt", "_updatedAt", "_sdkVersion", "_osVersion", "_operatingSystem", "_deviceType", "_appVersion", "_isAnonymous", "_appBuild", "_sdkName", "_appId", "_sessionPageviews", "_sessionId", "_pushPrimerEligible"]
XCTAssertEqual([], Set(try XCTUnwrap(decorated.properties).keys).symmetricDifference(expectedPropertyKeys))
XCTAssertNil(decorated.identityAutoProperties)
XCTAssertNil(decorated.deviceAutoProperties)
Expand Down Expand Up @@ -105,7 +105,7 @@ class AutoPropertyDecoratorTests: XCTestCase {
let decorated = decorator.decorate(update)

// Assert
let expectedEventAutoPropertyKeys = ["userId", "_deviceModel", "_bundlePackageId", "_lastBrowserLanguage", "_localId", "_appName", "_lastSeenAt", "_updatedAt", "_sdkVersion", "_osVersion", "_operatingSystem", "_deviceType", "_appVersion", "_isAnonymous", "_appBuild", "_sdkName", "_appId", "_sessionPageviews", "_currentScreenTitle", "_sessionId", "_myProp"]
let expectedEventAutoPropertyKeys = ["userId", "_deviceModel", "_bundlePackageId", "_lastBrowserLanguage", "_localId", "_appName", "_lastSeenAt", "_updatedAt", "_sdkVersion", "_osVersion", "_operatingSystem", "_deviceType", "_appVersion", "_isAnonymous", "_appBuild", "_sdkName", "_appId", "_sessionPageviews", "_currentScreenTitle", "_sessionId", "_pushPrimerEligible", "_myProp"]
XCTAssertEqual([], Set(try XCTUnwrap(decorated.identityAutoProperties).keys).symmetricDifference(expectedEventAutoPropertyKeys))
XCTAssertNil(decorated.deviceAutoProperties)

Expand All @@ -130,7 +130,7 @@ class AutoPropertyDecoratorTests: XCTestCase {
XCTAssertEqual([], Set(try XCTUnwrap(decorated.context).keys).symmetricDifference(expectedContextKeys))
let expectedPropertyKeys = ["_identity"]
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", "_sessionId", "PROFILE_PROPERTY"]
let expectedEventAutoPropertyKeys = ["userId", "_deviceModel", "_bundlePackageId", "_lastBrowserLanguage", "_localId", "_appName", "_lastSeenAt", "_updatedAt", "_sdkVersion", "_osVersion", "_operatingSystem", "_deviceType", "_appVersion", "_isAnonymous", "_appBuild", "_sdkName", "_appId", "_sessionPageviews", "_sessionId", "_pushPrimerEligible", "PROFILE_PROPERTY"]
XCTAssertEqual([], Set(try XCTUnwrap(decorated.identityAutoProperties).keys).symmetricDifference(expectedEventAutoPropertyKeys))
XCTAssertNil(decorated.deviceAutoProperties)
}
Expand Down
9 changes: 9 additions & 0 deletions Tests/AppcuesKitTests/MockAppcues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import UserNotifications
@testable import AppcuesKit

class MockAppcues: Appcues {
Expand Down Expand Up @@ -371,4 +372,12 @@ class MockAnalyticsTracker: AnalyticsTracking {
class MockPushMonitor: PushMonitoring {
var pushEnabled: Bool = false
var pushBackgroundEnabled: Bool = false
var pushPrimerEligible: Bool = false

var onRefreshPushStatus: (() -> Void)?
func refreshPushStatus(completion: ((UNAuthorizationStatus) -> Void)?) {
onRefreshPushStatus?()
completion?(.notDetermined)
}

}
14 changes: 14 additions & 0 deletions Tests/AppcuesKitTests/PushMonitorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class PushMonitorTests: XCTestCase {
// Assert
XCTAssertFalse(pushMonitor.pushEnabled)
XCTAssertFalse(pushMonitor.pushBackgroundEnabled)
XCTAssertFalse(pushMonitor.pushPrimerEligible)
}

func testNoTokenAuthorized() throws {
Expand All @@ -38,6 +39,7 @@ class PushMonitorTests: XCTestCase {
// Assert
XCTAssertFalse(pushMonitor.pushEnabled)
XCTAssertFalse(pushMonitor.pushBackgroundEnabled)
XCTAssertFalse(pushMonitor.pushPrimerEligible)
}

func testTokenNotAuthorized() throws {
Expand All @@ -48,6 +50,7 @@ class PushMonitorTests: XCTestCase {
// Assert
XCTAssertFalse(pushMonitor.pushEnabled)
XCTAssertTrue(pushMonitor.pushBackgroundEnabled)
XCTAssertFalse(pushMonitor.pushPrimerEligible)
}

func testTokenAuthorized() throws {
Expand All @@ -58,6 +61,17 @@ class PushMonitorTests: XCTestCase {
// Assert
XCTAssertTrue(pushMonitor.pushEnabled)
XCTAssertTrue(pushMonitor.pushBackgroundEnabled)
XCTAssertFalse(pushMonitor.pushPrimerEligible)
}

func testPrimerEligible() throws {
appcues.storage.pushToken = "<some-token>"
pushMonitor.mockPushStatus(.notDetermined)

// Assert
XCTAssertFalse(pushMonitor.pushEnabled)
XCTAssertTrue(pushMonitor.pushBackgroundEnabled)
XCTAssertTrue(pushMonitor.pushPrimerEligible)
}

}

0 comments on commit bdd8e44

Please sign in to comment.