From 459d5125b8f490969e61212419c13f6fd25bbaa5 Mon Sep 17 00:00:00 2001 From: Niall Quinn Date: Tue, 6 Aug 2024 12:22:57 +0200 Subject: [PATCH] feat: session is active (#962) * Add checkout session active * Reset checkoutSessionId when Headless cleans up * add test for headless cleanup --- .../Classes/Core/Primer/Primer.swift | 7 +++++ .../Classes/Core/Primer/PrimerInternal.swift | 4 +++ .../PrimerHeadlessUniversalCheckout.swift | 1 + Tests/Primer/Utils/CheckoutSessionTests.swift | 26 +++++++++++++++++++ 4 files changed, 38 insertions(+) create mode 100644 Tests/Primer/Utils/CheckoutSessionTests.swift diff --git a/Sources/PrimerSDK/Classes/Core/Primer/Primer.swift b/Sources/PrimerSDK/Classes/Core/Primer/Primer.swift index d424d92c26..13c4d445fa 100644 --- a/Sources/PrimerSDK/Classes/Core/Primer/Primer.swift +++ b/Sources/PrimerSDK/Classes/Core/Primer/Primer.swift @@ -109,4 +109,11 @@ public class Primer { public func dismiss() { PrimerInternal.shared.dismiss() } + + /** + Checkout Session is active? + */ + public func checkoutSessionIsActive() -> Bool { + PrimerInternal.shared.checkoutSessionIsActive() + } } diff --git a/Sources/PrimerSDK/Classes/Core/Primer/PrimerInternal.swift b/Sources/PrimerSDK/Classes/Core/Primer/PrimerInternal.swift index c322249897..5a91c4d2c0 100644 --- a/Sources/PrimerSDK/Classes/Core/Primer/PrimerInternal.swift +++ b/Sources/PrimerSDK/Classes/Core/Primer/PrimerInternal.swift @@ -276,4 +276,8 @@ internal class PrimerInternal: LogReporter { } } } + + internal func checkoutSessionIsActive() -> Bool { + checkoutSessionId != nil + } } diff --git a/Sources/PrimerSDK/Classes/Core/PrimerHeadlessUniversalCheckout/PrimerHeadlessUniversalCheckout.swift b/Sources/PrimerSDK/Classes/Core/PrimerHeadlessUniversalCheckout/PrimerHeadlessUniversalCheckout.swift index 6baddd82d4..cdfe4e7aeb 100644 --- a/Sources/PrimerSDK/Classes/Core/PrimerHeadlessUniversalCheckout/PrimerHeadlessUniversalCheckout.swift +++ b/Sources/PrimerSDK/Classes/Core/PrimerHeadlessUniversalCheckout/PrimerHeadlessUniversalCheckout.swift @@ -132,6 +132,7 @@ public class PrimerHeadlessUniversalCheckout: LogReporter { public func cleanUp() { PrimerAPIConfigurationModule.resetSession() + PrimerInternal.shared.checkoutSessionId = nil } // MARK: - HELPERS diff --git a/Tests/Primer/Utils/CheckoutSessionTests.swift b/Tests/Primer/Utils/CheckoutSessionTests.swift new file mode 100644 index 0000000000..0ae493eae6 --- /dev/null +++ b/Tests/Primer/Utils/CheckoutSessionTests.swift @@ -0,0 +1,26 @@ +// +// File.swift +// +// +// Created by Niall Quinn on 06/08/24. +// + +import XCTest +@testable import PrimerSDK + +final class CheckoutSessionTests: XCTestCase { + func test_headless_cleanup() { + XCTAssertFalse(Primer.shared.checkoutSessionIsActive()) + + let expectation = self.expectation(description: "Wait for headless load") + + PrimerHeadlessUniversalCheckout.current.start(withClientToken: "") { paymentMethods, err in + XCTAssertTrue(Primer.shared.checkoutSessionIsActive()) + expectation.fulfill() + } + + waitForExpectations(timeout: 30) + PrimerHeadlessUniversalCheckout.current.cleanUp() + XCTAssertFalse(Primer.shared.checkoutSessionIsActive()) + } +}