Skip to content

Commit

Permalink
Fixed failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stechiu committed Dec 19, 2024
1 parent 57b63f1 commit 950b2e0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
5 changes: 5 additions & 0 deletions Sources/BraintreeCore/Analytics/FPTIBatchData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion Sources/BraintreeCore/BTAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,15 @@ class BTShopperInsightsClient_Tests: XCTestCase {

// MARK: - Analytics

func testSendPayPalPresentedEvent_sendsAnalytic() {
func testSendPayPalPresentedEvent_whenExperimentTypeIsControl_sendsAnalytic() {
let presentmentDetails = BTPresentmentDetails(
buttonOrder: .first,
experimentType: .control,
pageType: .about
)
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,
"""
Expand All @@ -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")
Expand All @@ -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,
"""
Expand All @@ -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()
Expand Down
12 changes: 6 additions & 6 deletions UnitTests/BraintreeTestShared/MockAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
}

Expand Down

0 comments on commit 950b2e0

Please sign in to comment.