From 950b2e038021623e0998942d71f5166efc0d3607 Mon Sep 17 00:00:00 2001 From: stechiu Date: Wed, 18 Dec 2024 17:04:23 -0800 Subject: [PATCH] Fixed failing tests --- .../Analytics/FPTIBatchData.swift | 5 +++ Sources/BraintreeCore/BTAPIClient.swift | 2 +- .../BTShopperInsightsClient_Tests.swift | 38 +++++++++++++++++-- .../BraintreeTestShared/MockAPIClient.swift | 12 +++--- 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/Sources/BraintreeCore/Analytics/FPTIBatchData.swift b/Sources/BraintreeCore/Analytics/FPTIBatchData.swift index de6186dba..f83186ca0 100644 --- a/Sources/BraintreeCore/Analytics/FPTIBatchData.swift +++ b/Sources/BraintreeCore/Analytics/FPTIBatchData.swift @@ -43,6 +43,8 @@ struct FPTIBatchData: Codable { let endTime: Int? let errorDescription: String? let eventName: String + /// The experiment type that is sent to analytics to help improve the Shopper Insights feature experience. + let experimentType: String? /// True if the `BTConfiguration` was retrieved from local cache after `tokenize()` call. /// False if the `BTConfiguration` was fetched remotely after `tokenize()` call. let isConfigFromCache: Bool? @@ -79,6 +81,7 @@ struct FPTIBatchData: Codable { endTime: Int? = nil, errorDescription: String? = nil, eventName: String, + experimentType: String? = nil, isConfigFromCache: Bool? = nil, isVaultRequest: Bool? = nil, linkType: String? = nil, @@ -99,6 +102,7 @@ struct FPTIBatchData: Codable { self.endTime = endTime self.errorDescription = errorDescription self.eventName = eventName + self.experimentType = experimentType self.isConfigFromCache = isConfigFromCache self.isVaultRequest = isVaultRequest self.linkType = linkType @@ -119,6 +123,7 @@ struct FPTIBatchData: Codable { case correlationID = "correlation_id" case errorDescription = "error_desc" case eventName = "event_name" + case experimentType = "experiment_type" case isConfigFromCache = "config_cached" case isVaultRequest = "is_vault" case linkType = "link_type" diff --git a/Sources/BraintreeCore/BTAPIClient.swift b/Sources/BraintreeCore/BTAPIClient.swift index 12f5ff794..7bb81b48f 100644 --- a/Sources/BraintreeCore/BTAPIClient.swift +++ b/Sources/BraintreeCore/BTAPIClient.swift @@ -308,10 +308,10 @@ import Foundation buttonType: String? = nil, correlationID: String? = nil, errorDescription: String? = nil, + merchantExperiment: String? = nil, isConfigFromCache: Bool? = nil, isVaultRequest: Bool? = nil, linkType: LinkType? = nil, - merchantExperiment: String? = nil, pageType: String? = nil, paymentMethodsDisplayed: String? = nil, payPalContextID: String? = nil, diff --git a/UnitTests/BraintreeShopperInsightsTests/BTShopperInsightsClient_Tests.swift b/UnitTests/BraintreeShopperInsightsTests/BTShopperInsightsClient_Tests.swift index d5583b21e..8440ac76f 100644 --- a/UnitTests/BraintreeShopperInsightsTests/BTShopperInsightsClient_Tests.swift +++ b/UnitTests/BraintreeShopperInsightsTests/BTShopperInsightsClient_Tests.swift @@ -206,7 +206,7 @@ class BTShopperInsightsClient_Tests: XCTestCase { // MARK: - Analytics - func testSendPayPalPresentedEvent_sendsAnalytic() { + func testSendPayPalPresentedEvent_whenExperimentTypeIsControl_sendsAnalytic() { let presentmentDetails = BTPresentmentDetails( buttonOrder: .first, experimentType: .control, @@ -214,7 +214,7 @@ class BTShopperInsightsClient_Tests: XCTestCase { ) sut.sendPresentedEvent(for: .payPal, presentmentDetails: presentmentDetails) XCTAssertEqual(mockAPIClient.postedAnalyticsEvents.first, "shopper-insights:button-presented") - XCTAssertEqual(mockAPIClient.postedButtonOrder, 0) + XCTAssertEqual(mockAPIClient.postedButtonOrder, "1") XCTAssertEqual(mockAPIClient.postedButtonType, "PayPal") XCTAssertEqual(mockAPIClient.postedMerchantExperiment, """ @@ -226,6 +226,22 @@ class BTShopperInsightsClient_Tests: XCTestCase { XCTAssertEqual(mockAPIClient.postedPageType, "about") } + func testSendPayPalPresentedEvent_whenExperimentTypeIsTest_sendsAnalytic() { + let presentmentDetails = BTPresentmentDetails( + buttonOrder: .first, + experimentType: .test, + pageType: .about + ) + sut.sendPresentedEvent(for: .payPal, presentmentDetails: presentmentDetails) + XCTAssertEqual(mockAPIClient.postedMerchantExperiment, + """ + [ + { "exp_name" : "PaymentReady" } + { "treatment_name" : "test" } + ] + """) + } + func testSendPayPalSelectedEvent_sendsAnalytic() { sut.sendPayPalSelectedEvent() XCTAssertEqual(mockAPIClient.postedAnalyticsEvents.first, "shopper-insights:paypal-selected") @@ -240,7 +256,7 @@ class BTShopperInsightsClient_Tests: XCTestCase { ) sut.sendPresentedEvent(for: .venmo, presentmentDetails: presentmentDetails) XCTAssertEqual(mockAPIClient.postedAnalyticsEvents.first, "shopper-insights:button-presented") - XCTAssertEqual(mockAPIClient.postedButtonOrder, 0) + XCTAssertEqual(mockAPIClient.postedButtonOrder, "1") XCTAssertEqual(mockAPIClient.postedButtonType, "Venmo") XCTAssertEqual(mockAPIClient.postedMerchantExperiment, """ @@ -251,6 +267,22 @@ class BTShopperInsightsClient_Tests: XCTestCase { """) XCTAssertEqual(mockAPIClient.postedPageType, "about") } + + func testSendVenmoPresentedEvent_whenExperimentTypeIsTest_sendsAnalytic() { + let presentmentDetails = BTPresentmentDetails( + buttonOrder: .first, + experimentType: .test, + pageType: .about + ) + sut.sendPresentedEvent(for: .venmo, presentmentDetails: presentmentDetails) + XCTAssertEqual(mockAPIClient.postedMerchantExperiment, + """ + [ + { "exp_name" : "PaymentReady" } + { "treatment_name" : "test" } + ] + """) + } func testSendVenmoSelectedEvent_sendsAnalytic() { sut.sendVenmoSelectedEvent() diff --git a/UnitTests/BraintreeTestShared/MockAPIClient.swift b/UnitTests/BraintreeTestShared/MockAPIClient.swift index 79c75c57b..ba9b05d81 100644 --- a/UnitTests/BraintreeTestShared/MockAPIClient.swift +++ b/UnitTests/BraintreeTestShared/MockAPIClient.swift @@ -13,7 +13,7 @@ public class MockAPIClient: BTAPIClient { public var postedAnalyticsEvents : [String] = [] public var postedAppSwitchURL: [String: String?] = [:] - public var postedButtonOrder: Int? = nil + public var postedButtonOrder: String? = nil public var postedButtonType: String? = nil public var postedIsVaultRequest = false public var postedLinkType: LinkType? = nil @@ -96,16 +96,16 @@ public class MockAPIClient: BTAPIClient { } public override func sendAnalyticsEvent( - _ name: String, + _ eventName: String, appSwitchURL: URL? = nil, - buttonOrder: Int? = nil, + buttonOrder: String? = nil, buttonType: String? = nil, correlationID: String? = nil, errorDescription: String? = nil, + merchantExperiment experiment: String? = nil, isConfigFromCache: Bool? = nil, isVaultRequest: Bool? = nil, linkType: LinkType? = nil, - merchantExperiment experiment: String? = nil, pageType: String? = nil, paymentMethodsDisplayed: String? = nil, payPalContextID: String? = nil, @@ -119,8 +119,8 @@ public class MockAPIClient: BTAPIClient { postedIsVaultRequest = isVaultRequest ?? false postedMerchantExperiment = experiment postedPaymentMethodsDisplayed = paymentMethodsDisplayed - postedAppSwitchURL[name] = appSwitchURL?.absoluteString - postedAnalyticsEvents.append(name) + postedAppSwitchURL[eventName] = appSwitchURL?.absoluteString + postedAnalyticsEvents.append(eventName) postedShopperSessionID = shopperSessionID }