Skip to content

Commit

Permalink
fix a bunch of warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
ndurell committed Mar 22, 2024
1 parent de929fb commit c43a721
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Sources/KlaviyoSwift/KlaviyoState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ struct KlaviyoState: Equatable, Codable {
}
var attributes = profile.data.attributes
var location = profile.data.attributes.location ?? .init()
var properties = profile.data.attributes.properties.value as? [String: Any] ?? [:]
let properties = profile.data.attributes.properties.value as? [String: Any] ?? [:]
let updatedProfile = Profile.updateProfileWithProperties(dict: pendingProfile)

if let firstName = updatedProfile.firstName {
Expand Down
16 changes: 15 additions & 1 deletion Tests/KlaviyoSwiftTests/APIRequestErrorHandlingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import XCTest

let TIMEOUT_NANOSECONDS: UInt64 = 10_000_000_000 // 10 seconds

@MainActor
class APIRequestErrorHandlingTests: XCTestCase {
@MainActor
override func setUp() async throws {
environment = KlaviyoEnvironment.test()
}

// MARK: - http error

@MainActor
func testSendRequestHttpFailureDequesRequest() async throws {
var initialState = INITIALIZED_TEST_STATE()
let request = initialState.buildProfileRequest(apiKey: initialState.apiKey!, anonymousId: initialState.anonymousId!)
Expand All @@ -35,6 +36,7 @@ class APIRequestErrorHandlingTests: XCTestCase {
}
}

@MainActor
func testSendRequestHttpFailureForPhoneNumberResetsStateAndDequesRequest() async throws {
var initialState = INITIALIZED_TEST_STATE_INVALID_PHONE()
let request = initialState.buildProfileRequest(apiKey: initialState.apiKey!, anonymousId: initialState.anonymousId!)
Expand All @@ -57,6 +59,7 @@ class APIRequestErrorHandlingTests: XCTestCase {
}
}

@MainActor
func testSendRequestHttpFailureForEmailResetsStateAndDequesRequest() async throws {
var initialState = INITIALIZED_TEST_STATE_INVALID_EMAIL()
let request = initialState.buildProfileRequest(apiKey: initialState.apiKey!, anonymousId: initialState.anonymousId!)
Expand All @@ -81,6 +84,7 @@ class APIRequestErrorHandlingTests: XCTestCase {

// MARK: - network error

@MainActor
func testSendRequestFailureIncrementsRetryCount() async throws {
var initialState = INITIALIZED_TEST_STATE()
let request = initialState.buildProfileRequest(apiKey: initialState.apiKey!, anonymousId: initialState.anonymousId!)
Expand All @@ -100,6 +104,7 @@ class APIRequestErrorHandlingTests: XCTestCase {
}
}

@MainActor
func testSendRequestFailureWithBackoff() async throws {
var initialState = INITIALIZED_TEST_STATE()
initialState.retryInfo = .retryWithBackoff(requestCount: 1, totalRetryCount: 1, currentBackoff: 1)
Expand All @@ -120,6 +125,7 @@ class APIRequestErrorHandlingTests: XCTestCase {
}
}

@MainActor
func testSendRequestMaxRetries() async throws {
var initialState = INITIALIZED_TEST_STATE()
initialState.retryInfo = .retry(ErrorHandlingConstants.maxRetries)
Expand All @@ -144,6 +150,7 @@ class APIRequestErrorHandlingTests: XCTestCase {

// MARK: - internal error

@MainActor
func testSendRequestInternalError() async throws {
// NOTE: should really happen but putting this in for possible future cases and test coverage
var initialState = INITIALIZED_TEST_STATE()
Expand All @@ -166,6 +173,7 @@ class APIRequestErrorHandlingTests: XCTestCase {

// MARK: - internal request error

@MainActor
func testSendRequestInternalRequestError() async throws {
var initialState = INITIALIZED_TEST_STATE()

Expand All @@ -187,6 +195,7 @@ class APIRequestErrorHandlingTests: XCTestCase {

// MARK: - unknown error

@MainActor
func testSendRequestUnknownError() async throws {
var initialState = INITIALIZED_TEST_STATE()

Expand All @@ -208,6 +217,7 @@ class APIRequestErrorHandlingTests: XCTestCase {

// MARK: - data decoding error

@MainActor
func testSendRequestDataDecodingError() async throws {
var initialState = INITIALIZED_TEST_STATE()
let request = initialState.buildProfileRequest(apiKey: initialState.apiKey!, anonymousId: initialState.anonymousId!)
Expand All @@ -228,6 +238,7 @@ class APIRequestErrorHandlingTests: XCTestCase {

// MARK: - invalid data

@MainActor
func testSendRequestInvalidData() async throws {
var initialState = INITIALIZED_TEST_STATE()
let request = initialState.buildProfileRequest(apiKey: initialState.apiKey!, anonymousId: initialState.anonymousId!)
Expand All @@ -248,6 +259,7 @@ class APIRequestErrorHandlingTests: XCTestCase {

// MARK: - rate limit error

@MainActor
func testRateLimitErrorWithExistingRetry() async throws {
var initialState = INITIALIZED_TEST_STATE()
let request = initialState.buildProfileRequest(apiKey: initialState.apiKey!, anonymousId: initialState.anonymousId!)
Expand All @@ -266,6 +278,7 @@ class APIRequestErrorHandlingTests: XCTestCase {
}
}

@MainActor
func testRateLimitErrorWithExistingBackoffRetry() async throws {
var initialState = INITIALIZED_TEST_STATE()
initialState.retryInfo = .retryWithBackoff(requestCount: 2, totalRetryCount: 2, currentBackoff: 4)
Expand All @@ -287,6 +300,7 @@ class APIRequestErrorHandlingTests: XCTestCase {

// MARK: - Missing or invalid response

@MainActor
func testMissingOrInvalidResponse() async throws {
var initialState = INITIALIZED_TEST_STATE()
initialState.retryInfo = .retryWithBackoff(requestCount: 2, totalRetryCount: 2, currentBackoff: 4)
Expand Down
3 changes: 2 additions & 1 deletion Tests/KlaviyoSwiftTests/AppLifeCycleEventsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Combine
import Foundation
import XCTest

@MainActor
class AppLifeCycleEventsTests: XCTestCase {
let passThroughSubject = PassthroughSubject<Notification, Never>()

Expand All @@ -25,12 +24,14 @@ class AppLifeCycleEventsTests: XCTestCase {
}
}

@MainActor
override func setUp() {
environment = KlaviyoEnvironment.test()
}

// MARK: - App Terminate

@MainActor
func testAppTerminateStopsReachability() {
environment.notificationCenterPublisher = getFilteredNotificaitonPublished(name: UIApplication.willTerminateNotification)
let expection = XCTestExpectation(description: "Stop reachability is called.")
Expand Down
6 changes: 1 addition & 5 deletions Tests/KlaviyoSwiftTests/KlaviyoAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@
import SnapshotTesting
import XCTest

@MainActor
final class KlaviyoAPITests: XCTestCase {
@MainActor
override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.

environment = KlaviyoEnvironment.test()
}

override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

func testInvalidURL() async throws {
environment.analytics.apiURL = ""

Expand Down
1 change: 0 additions & 1 deletion Tests/KlaviyoSwiftTests/KlaviyoStateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import Foundation
import SnapshotTesting
import XCTest

@MainActor
final class KlaviyoStateTests: XCTestCase {
let TEST_EVENT = [
"event": "$opened_push",
Expand Down
2 changes: 1 addition & 1 deletion Tests/KlaviyoSwiftTests/NetworkSessionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import SnapshotTesting
import XCTest

@MainActor
class NetworkSessionTests: XCTestCase {
@MainActor
override func setUpWithError() throws {
environment = KlaviyoEnvironment.test()
}
Expand Down
4 changes: 3 additions & 1 deletion Tests/KlaviyoSwiftTests/StateChangePublisherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import Foundation
import XCTest
@_spi(KlaviyoPrivate) @testable import KlaviyoSwift

@MainActor
final class StateChangePublisherTests: XCTestCase {
@MainActor
override func setUpWithError() throws {
environment = KlaviyoEnvironment.test()
}

@MainActor
func testStateChangePublisher() throws {
let savedCalledExpectation = XCTestExpectation(description: "Save called on initialization")
// Third call set email should trigger again
Expand Down Expand Up @@ -76,6 +77,7 @@ final class StateChangePublisherTests: XCTestCase {
XCTAssertEqual(count, 2)
}

@MainActor
func testStateChangeDuplicateAreRemoved() throws {
let savedCalledExpectation = XCTestExpectation(description: "Save called on initialization")
savedCalledExpectation.assertForOverFulfill = true
Expand Down
22 changes: 21 additions & 1 deletion Tests/KlaviyoSwiftTests/StateManagementEdgeCaseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
import Foundation
import XCTest

@MainActor
class StateManagementEdgeCaseTests: XCTestCase {
@MainActor
override func setUp() async throws {
environment = KlaviyoEnvironment.test()
}

// MARK: - initialization

@MainActor
func testInitializeWhileInitializing() async throws {
let initialState = KlaviyoState(queue: [], requestsInFlight: [])
let store = TestStore(initialState: initialState, reducer: KlaviyoReducer())
Expand All @@ -39,6 +40,7 @@ class StateManagementEdgeCaseTests: XCTestCase {
_ = await store.send(.initialize(apiKey))
}

@MainActor
func testInitializeAfterInitialized() async throws {
let initialState = INITIALIZED_TEST_STATE()
let store = TestStore(initialState: initialState, reducer: KlaviyoReducer())
Expand All @@ -57,6 +59,7 @@ class StateManagementEdgeCaseTests: XCTestCase {

// MARK: - Send Request

@MainActor
func testSendRequestBeforeInitialization() async throws {
let apiKey = "fake-key"
let initialState = KlaviyoState(apiKey: apiKey,
Expand All @@ -71,6 +74,7 @@ class StateManagementEdgeCaseTests: XCTestCase {

// MARK: - Complete Initialization

@MainActor
func testCompleteInitializationWhileAlreadyInitialized() async throws {
let apiKey = "fake-key"
let initialState = KlaviyoState(apiKey: apiKey,
Expand All @@ -87,6 +91,7 @@ class StateManagementEdgeCaseTests: XCTestCase {
_ = await store.send(.completeInitialization(initialState))
}

@MainActor
func testCompleteInitializationWithExistingIdentifiers() async throws {
let apiKey = "fake-key"
let initialState = KlaviyoState(apiKey: apiKey,
Expand All @@ -110,6 +115,7 @@ class StateManagementEdgeCaseTests: XCTestCase {

// MARK: - Set Email

@MainActor
func testSetEmailUninitialized() async throws {
let expection = XCTestExpectation(description: "fatal error expected")
environment.emitDeveloperWarning = { _ in
Expand All @@ -130,6 +136,7 @@ class StateManagementEdgeCaseTests: XCTestCase {
await fulfillment(of: [expection])
}

@MainActor
func testSetEmailMissingAnonymousIdStillSetsEmail() async throws {
let apiKey = "fake-key"
let initialState = KlaviyoState(apiKey: apiKey,
Expand All @@ -146,6 +153,7 @@ class StateManagementEdgeCaseTests: XCTestCase {

// MARK: - Set External Id

@MainActor
func testSetExternalIdUninitialized() async throws {
let apiKey = "fake-key"
let initialState = KlaviyoState(apiKey: apiKey,
Expand All @@ -159,6 +167,7 @@ class StateManagementEdgeCaseTests: XCTestCase {
_ = await store.send(.setExternalId("external-blob-id"))
}

@MainActor
func testSetExternalIdMissingAnonymousIdStillSetsExternalId() async throws {
let apiKey = "fake-key"
let initialState = KlaviyoState(apiKey: apiKey,
Expand All @@ -175,6 +184,7 @@ class StateManagementEdgeCaseTests: XCTestCase {

// MARK: - Set Phone number

@MainActor
func testSetPhoneNumberUninitialized() async throws {
let apiKey = "fake-key"
let initialState = KlaviyoState(apiKey: apiKey,
Expand All @@ -188,6 +198,7 @@ class StateManagementEdgeCaseTests: XCTestCase {
_ = await store.send(.setPhoneNumber("1-800-Blobs4u"))
}

@MainActor
func testSetPhoneNumberMissingApiKeyStillSetsPhoneNumber() async throws {
let initialState = KlaviyoState(anonymousId: environment.analytics.uuid().uuidString,
queue: [],
Expand All @@ -203,6 +214,7 @@ class StateManagementEdgeCaseTests: XCTestCase {

// MARK: - Set Push Token

@MainActor
func testSetPushTokenUninitialized() async throws {
let apiKey = "fake-key"
let initialState = KlaviyoState(apiKey: apiKey,
Expand All @@ -216,6 +228,7 @@ class StateManagementEdgeCaseTests: XCTestCase {
_ = await store.send(.setPushToken("blob_token", .authorized))
}

@MainActor
func testSetPushTokenWithMissingAnonymousId() async throws {
let apiKey = "fake-key"
let initialState = KlaviyoState(apiKey: apiKey,
Expand All @@ -233,6 +246,7 @@ class StateManagementEdgeCaseTests: XCTestCase {

// MARK: - Stop

@MainActor
func testStopUninitialized() async {
let apiKey = "fake-key"
let initialState = KlaviyoState(apiKey: apiKey,
Expand All @@ -246,6 +260,7 @@ class StateManagementEdgeCaseTests: XCTestCase {
_ = await store.send(.stop)
}

@MainActor
func testStopInitializing() async {
let apiKey = "fake-key"
let initialState = KlaviyoState(apiKey: apiKey,
Expand All @@ -261,6 +276,7 @@ class StateManagementEdgeCaseTests: XCTestCase {

// MARK: - Start

@MainActor
func testStartUninitialized() async {
let apiKey = "fake-key"
let initialState = KlaviyoState(apiKey: apiKey,
Expand All @@ -276,6 +292,7 @@ class StateManagementEdgeCaseTests: XCTestCase {

// MARK: - Network Status Changed

@MainActor
func testNetworkStatusChangedUninitialized() async {
let apiKey = "fake-key"
let initialState = KlaviyoState(apiKey: apiKey,
Expand All @@ -291,6 +308,7 @@ class StateManagementEdgeCaseTests: XCTestCase {

// MARK: - Missing api key for token request

@MainActor
func testTokenRequestMissingApiKey() async {
let initialState = KlaviyoState(
anonymousId: environment.analytics.uuid().uuidString,
Expand All @@ -308,6 +326,7 @@ class StateManagementEdgeCaseTests: XCTestCase {

// MARK: - set enqueue event uninitialized

@MainActor
func testEnqueueEventUninitialized() async throws {
let expection = XCTestExpectation(description: "fatal error expected")
environment.emitDeveloperWarning = { _ in
Expand All @@ -322,6 +341,7 @@ class StateManagementEdgeCaseTests: XCTestCase {

// MARK: - set profile uninitialized

@MainActor
func testSetProfileUnitialized() async throws {
let expection = XCTestExpectation(description: "fatal error expected")
environment.emitDeveloperWarning = { _ in
Expand Down
Loading

0 comments on commit c43a721

Please sign in to comment.