Skip to content

Commit

Permalink
add analytics tests; fix bug in sending analytics for failures
Browse files Browse the repository at this point in the history
  • Loading branch information
jaxdesmarais committed Dec 21, 2023
1 parent 7ee2ec5 commit dae20d6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
11 changes: 8 additions & 3 deletions Sources/BraintreePayPalMessaging/BTPayPalMessagingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ public class BTPayPalMessagingView: UIView {
apiClient.sendAnalyticsEvent(BTPayPalMessagingAnalytics.started)
apiClient.fetchOrReturnRemoteConfiguration { configuration, error in
if let error {
self.delegate?.onError(self, error: error)
self.notifyFailure(with: error)
return
}

guard let configuration else {
self.delegate?.onError(self, error: BTPayPalMessagingError.fetchConfigurationFailed)
self.notifyFailure(with: BTPayPalMessagingError.fetchConfigurationFailed)
return
}

guard let clientID = configuration.json?["paypal"]["clientId"].asString() else {
self.delegate?.onError(self, error: BTPayPalMessagingError.payPalClientIDNotFound)
self.notifyFailure(with: BTPayPalMessagingError.payPalClientIDNotFound)
return
}

Expand Down Expand Up @@ -84,6 +84,11 @@ public class BTPayPalMessagingView: UIView {
])
}
}

private func notifyFailure(with error: Error) {
apiClient.sendAnalyticsEvent(BTPayPalMessagingAnalytics.failed, errorDescription: error.localizedDescription)
delegate?.onError(self, error: error)
}
}

// MARK: - UIViewRepresentable protocol conformance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ final class BTPayPalMessagingView_Tests: XCTestCase {
XCTAssertEqual((mockDelegate.error as? NSError)?.code, 999)
}

func testStart_withNilConfiguration_callsDelegateWithError() {
func testStart_withNilConfiguration_callsDelegateWithErrorAndSendsAnalytics() {
let payPalMessageView = BTPayPalMessagingView(apiClient: mockAPIClient)
payPalMessageView.delegate = mockDelegate
payPalMessageView.start()

XCTAssertEqual(mockDelegate.error as? BTPayPalMessagingError, BTPayPalMessagingError.fetchConfigurationFailed)
XCTAssertEqual((mockDelegate.error as? BTPayPalMessagingError)?.errorCode, 0)
XCTAssertEqual((mockDelegate.error as? BTPayPalMessagingError)?.errorDescription, "Failed to fetch Braintree configuration.")
XCTAssertTrue(mockAPIClient.postedAnalyticsEvents.contains(BTPayPalMessagingAnalytics.started))
XCTAssertTrue(mockAPIClient.postedAnalyticsEvents.contains(BTPayPalMessagingAnalytics.failed))
}

func testStart_withNoClientID_callsDelegateWithError() {
Expand All @@ -46,7 +48,7 @@ final class BTPayPalMessagingView_Tests: XCTestCase {
XCTAssertEqual((mockDelegate.error as? BTPayPalMessagingError)?.errorDescription, "Could not find PayPal client ID in Braintree configuration.")
}

func testStart_withClientID_firesWillAppear() {
func testStart_withClientID_firesWillAppearAndSendsAnalytics() {
mockAPIClient.cannedConfigurationResponseBody = BTJSON(
value: [
"paypal": ["clientId": "a-fake-client-id"]
Expand All @@ -58,5 +60,6 @@ final class BTPayPalMessagingView_Tests: XCTestCase {
payPalMessageView.start()

XCTAssertTrue(mockDelegate.willAppear)
XCTAssertTrue(mockAPIClient.postedAnalyticsEvents.contains(BTPayPalMessagingAnalytics.started))
}
}

0 comments on commit dae20d6

Please sign in to comment.