Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing opened push event to be transmitted when SDK is not initialized #181

Merged
merged 5 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Sources/KlaviyoSwift/StateManagement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ enum KlaviyoAction: Equatable {

var requiresInitialization: Bool {
switch self {
case .setEmail, .setPhoneNumber, .setExternalId, .setPushToken, .enqueueEvent, .enqueueProfile, .setProfileProperty, .resetProfile, .resetStateAndDequeue:
// if event metric is opened push we DON'T require initilization in all other event metric cases we DO.
case let .enqueueEvent(event) where event.metric.name == .OpenedPush:
return false

case .setEmail, .setPhoneNumber, .setExternalId, .setPushToken, .enqueueProfile, .setProfileProperty, .resetProfile, .resetStateAndDequeue, .enqueueEvent:
return true

case .initialize, .completeInitialization, .deQueueCompletedResults, .networkConnectivityChanged, .flushQueue, .sendRequest, .stop, .start, .cancelInFlightRequests, .requestFailed:
Expand Down
35 changes: 28 additions & 7 deletions Tests/KlaviyoSwiftTests/StateManagementEdgeCaseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class StateManagementEdgeCaseTests: XCTestCase {
// MARK: - Set Email

@MainActor
func testSetEmailUninitialized() async throws {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just updated some test names to indicate the outcome we expect

func testSetEmailUninitializedDoesNotAddToPendingRequest() async throws {
let expection = XCTestExpectation(description: "fatal error expected")
environment.emitDeveloperWarning = { _ in
// Would really fatalError - not happening because we can't do that in tests so we fake it.
Expand Down Expand Up @@ -168,7 +168,7 @@ class StateManagementEdgeCaseTests: XCTestCase {
// MARK: - Set External Id

@MainActor
func testSetExternalIdUninitialized() async throws {
func testSetExternalIdUninitializedDoesNotAddToPendingRequest() async throws {
let apiKey = "fake-key"
let initialState = KlaviyoState(apiKey: apiKey,
anonymousId: environment.analytics.uuid().uuidString,
Expand Down Expand Up @@ -213,7 +213,7 @@ class StateManagementEdgeCaseTests: XCTestCase {
// MARK: - Set Phone number

@MainActor
func testSetPhoneNumberUninitialized() async throws {
func testSetPhoneNumberUninitializedDoesNotAddToPendingRequest() async throws {
let apiKey = "fake-key"
let initialState = KlaviyoState(apiKey: apiKey,
anonymousId: environment.analytics.uuid().uuidString,
Expand Down Expand Up @@ -257,7 +257,7 @@ class StateManagementEdgeCaseTests: XCTestCase {
// MARK: - Set Push Token

@MainActor
func testSetPushTokenUninitialized() async throws {
func testSetPushTokenUninitializedDoesNotAddToPendingRequest() async throws {
let apiKey = "fake-key"
let initialState = KlaviyoState(apiKey: apiKey,
anonymousId: environment.analytics.uuid().uuidString,
Expand Down Expand Up @@ -369,15 +369,30 @@ class StateManagementEdgeCaseTests: XCTestCase {
// MARK: - set enqueue event uninitialized

@MainActor
func testEnqueueEventUninitialized() async throws {
func testOpenedPushEventUninitializedAddsToPendingRequests() async throws {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests opened push event is added to pending requests when SDK isn't initialized.

let store = TestStore(initialState: .init(queue: []), reducer: KlaviyoReducer())
let event = Event(name: .OpenedPush)
_ = await store.send(.enqueueEvent(event)) {
$0.pendingRequests = [.event(event)]
}
}

@MainActor
func testEnqueueNonOpenedPushEventUninitializedDoesNotAddToPendingRequest() async throws {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test goes through all other event metrics other than opened push and makes sure that they don't get through the SDK when it's not initialized.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

let expection = XCTestExpectation(description: "fatal error expected")
environment.emitDeveloperWarning = { _ in
// Would really runTimeWarn - not happening because we can't do that in tests so we fake it.
expection.fulfill()
}
let store = TestStore(initialState: .init(queue: []), reducer: KlaviyoReducer())
let event = Event(name: .OpenedPush)
_ = await store.send(.enqueueEvent(event))

let nonOpenedPushEvents = Event.EventName.allCases.filter { $0 != .OpenedPush }

for event in nonOpenedPushEvents {
let event = Event(name: event)
_ = await store.send(.enqueueEvent(event))
}

await fulfillment(of: [expection])
}

Expand Down Expand Up @@ -423,3 +438,9 @@ class StateManagementEdgeCaseTests: XCTestCase {
}
}
}

extension Event.EventName: CaseIterable {
public static var allCases: [KlaviyoSwift.Event.EventName] {
[.OpenedPush, .OpenedAppMetric, .ViewedProductMetric, .AddedToCartMetric, .StartedCheckoutMetric, .CustomEvent("someEvent")]
}
}
Loading