Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,40 @@ final class StorefrontAPIMutationsTests: XCTestCase {
// MARK: - Mock URLProtocol for Network Mocking

class MockURLProtocol: URLProtocol {
static var mockResponseData: Data?
static var mockError: Error?
static var mockStatusCode: Int = 200
static var capturedRequest: URLRequest?
static var capturedRequestBody: Data?
private struct State {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicated in queries

var mockResponseData: Data?
var mockError: Error?
var mockStatusCode = 200
var capturedRequest: URLRequest?
var capturedRequestBody: Data?
}

private static let state = LockedTestValue(State())

static var mockResponseData: Data? {
get { state.get().mockResponseData }
set { state.update { $0.mockResponseData = newValue } }
}

static var mockError: Error? {
get { state.get().mockError }
set { state.update { $0.mockError = newValue } }
}

static var mockStatusCode: Int {
get { state.get().mockStatusCode }
set { state.update { $0.mockStatusCode = newValue } }
}

static var capturedRequest: URLRequest? {
get { state.get().capturedRequest }
set { state.update { $0.capturedRequest = newValue } }
}

static var capturedRequestBody: Data? {
get { state.get().capturedRequestBody }
set { state.update { $0.capturedRequestBody = newValue } }
}

override class func canInit(with _: URLRequest) -> Bool {
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,40 @@ final class StorefrontAPIQueriesTests: XCTestCase {
// MARK: - Mock URLProtocol for Network Mocking

class MockURLProtocol: URLProtocol {
static var mockResponseData: Data?
static var mockError: Error?
static var mockStatusCode: Int = 200
static var capturedRequest: URLRequest?
static var capturedRequestBody: Data?
private struct State {
var mockResponseData: Data?
var mockError: Error?
var mockStatusCode = 200
var capturedRequest: URLRequest?
var capturedRequestBody: Data?
}

private static let state = LockedTestValue(State())

static var mockResponseData: Data? {
get { state.get().mockResponseData }
set { state.update { $0.mockResponseData = newValue } }
}

static var mockError: Error? {
get { state.get().mockError }
set { state.update { $0.mockError = newValue } }
}

static var mockStatusCode: Int {
get { state.get().mockStatusCode }
set { state.update { $0.mockStatusCode = newValue } }
}

static var capturedRequest: URLRequest? {
get { state.get().capturedRequest }
set { state.update { $0.capturedRequest = newValue } }
}

static var capturedRequestBody: Data? {
get { state.get().capturedRequestBody }
set { state.update { $0.capturedRequestBody = newValue } }
}

override class func canInit(with _: URLRequest) -> Bool {
return true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@testable import ShopifyCheckoutKit

final class LockedTestValue<Value: Sendable>: Sendable {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems unnecessary

private let value: LockedValue<Value>

init(_ value: Value) {
self.value = LockedValue(value)
}

func get() -> Value {
value.get()
}

func set(_ newValue: Value) {
value.set(newValue)
}

func update(_ block: (inout Value) -> Void) {
value.update(block)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import os.log
import XCTest

@available(iOS 17.0, *)
@MainActor
class ShopifyAcceleratedCheckoutsTests: XCTestCase {
var originalLogLevel: LogLevel!

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import XCTest

// MARK: - Configuration Helpers

@MainActor
func XCTAssertThrowsErrorAsync(
_ expression: @autoclosure () async throws -> some Any,
_ errorHandler: (Error) -> Void,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import SwiftUI
import XCTest

@available(iOS 17.0, *)
@MainActor
final class AcceleratedCheckoutButtonsRenderStateTests: XCTestCase {
// MARK: - Render State Change Callback Tests

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -612,12 +612,46 @@ final class ApplePayAuthorizationDelegateControllerTests: XCTestCase {
"\"cost\":{\"totalAmount\":{\"amount\":\"0.00\",\"currencyCode\":\"USD\"}}," +
"\"discountCodes\":[],\"discountAllocations\":[]}"

static var failReplace = false
static var failDeliveryUpdate = false
static var failPrepareForCompletion = false
static var returnMappableCartUserError = false
static var returnInvalidCart = false
static var lastOperation: String?
private struct State {
var failReplace = false
var failDeliveryUpdate = false
var failPrepareForCompletion = false
var returnMappableCartUserError = false
var returnInvalidCart = false
var lastOperation: String?
}

private static let state = LockedTestValue(State())

static var failReplace: Bool {
get { state.get().failReplace }
set { state.update { $0.failReplace = newValue } }
}

static var failDeliveryUpdate: Bool {
get { state.get().failDeliveryUpdate }
set { state.update { $0.failDeliveryUpdate = newValue } }
}

static var failPrepareForCompletion: Bool {
get { state.get().failPrepareForCompletion }
set { state.update { $0.failPrepareForCompletion = newValue } }
}

static var returnMappableCartUserError: Bool {
get { state.get().returnMappableCartUserError }
set { state.update { $0.returnMappableCartUserError = newValue } }
}

static var returnInvalidCart: Bool {
get { state.get().returnInvalidCart }
set { state.update { $0.returnInvalidCart = newValue } }
}

static var lastOperation: String? {
get { state.get().lastOperation }
set { state.update { $0.lastOperation = newValue } }
}

static func response(for op: String) -> Data {
if op == "cartPrepareForCompletion" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ final class ApplePayCallbackTests: XCTestCase {

// MARK: - Setup

override func setUp() {
super.setUp()
override func setUp() async throws {
try await super.setUp()

// Create mock configuration
let commonConfig = ShopifyAcceleratedCheckouts.Configuration(
Expand Down Expand Up @@ -53,13 +53,13 @@ final class ApplePayCallbackTests: XCTestCase {
)
}

override func tearDown() {
override func tearDown() async throws {
viewController = nil
mockConfiguration = nil
mockIdentifier = nil
errorExpectation = nil
cancelExpectation = nil
super.tearDown()
try await super.tearDown()
}

// MARK: - Error Callback Tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import SwiftUI
import XCTest

@available(iOS 17.0, *)
@MainActor
final class ApplePayIntegrationTests: XCTestCase {
// MARK: - Properties

Expand All @@ -14,8 +15,8 @@ final class ApplePayIntegrationTests: XCTestCase {

// MARK: - Setup

override func setUp() {
super.setUp()
override func setUp() async throws {
try await super.setUp()

mockCommonConfiguration = ShopifyAcceleratedCheckouts.Configuration(
storefrontDomain: "test-shop.myshopify.com",
Expand Down Expand Up @@ -46,12 +47,12 @@ final class ApplePayIntegrationTests: XCTestCase {
)
}

override func tearDown() {
override func tearDown() async throws {
mockConfiguration = nil
mockCommonConfiguration = nil
mockApplePayConfiguration = nil
mockShopSettings = nil
super.tearDown()
try await super.tearDown()
}

// MARK: - Integration Tests
Expand Down Expand Up @@ -123,13 +124,13 @@ final class ApplePayIntegrationTests: XCTestCase {
// The view should essentially be empty/minimal due to invariant case
}

func testCallbackPersistenceAcrossViewUpdates() async {
func testCallbackPersistenceAcrossViewUpdates() {
var failCount = 0
let failHandler = { (_: CheckoutError) in
failCount += 1
}

let button = await ApplePayButton(
let button = ApplePayButton(
identifier: .cart(cartID: "gid://Shopify/Cart/test-cart"),
eventHandlers: EventHandlers(checkoutDidFail: failHandler),
cornerRadius: nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class ApplePayViewControllerTests: XCTestCase {
var mockStorefront: TestStorefrontAPI!
var mockAuthorizationDelegate: MockApplePayAuthorizationDelegate!

override func setUp() {
super.setUp()
override func setUp() async throws {
try await super.setUp()

// Create mock shop settings
let paymentSettings = PaymentSettings(
Expand Down Expand Up @@ -69,12 +69,12 @@ class ApplePayViewControllerTests: XCTestCase {
viewController.setMockAuthorizationDelegate(mockAuthorizationDelegate)
}

override func tearDown() {
override func tearDown() async throws {
viewController = nil
mockConfiguration = nil
mockStorefront = nil
mockAuthorizationDelegate = nil
super.tearDown()
try await super.tearDown()
}

class MockApplePayAuthorizationDelegate: ApplePayAuthorizationDelegate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import SwiftUI
import XCTest

@available(iOS 17.0, *)
@MainActor
final class ApplePayViewModifierTests: XCTestCase {
// MARK: - Properties

Expand All @@ -13,8 +14,8 @@ final class ApplePayViewModifierTests: XCTestCase {

// MARK: - Setup

override func setUp() {
super.setUp()
override func setUp() async throws {
try await super.setUp()

mockConfiguration = ShopifyAcceleratedCheckouts.Configuration(
storefrontDomain: "test-shop.myshopify.com",
Expand All @@ -36,11 +37,11 @@ final class ApplePayViewModifierTests: XCTestCase {
)
}

override func tearDown() {
override func tearDown() async throws {
mockConfiguration = nil
mockApplePayConfiguration = nil
mockShopSettings = nil
super.tearDown()
try await super.tearDown()
}

// MARK: - onCancel Modifier Tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ final class ShopPayCallbackTests: XCTestCase {

// MARK: - Setup

override func setUp() {
super.setUp()
override func setUp() async throws {
try await super.setUp()

mockConfiguration = ShopifyAcceleratedCheckouts.Configuration(
storefrontDomain: "test-shop.myshopify.com",
Expand All @@ -31,13 +31,13 @@ final class ShopPayCallbackTests: XCTestCase {
)
}

override func tearDown() {
override func tearDown() async throws {
viewController = nil
mockConfiguration = nil
mockIdentifier = nil
errorExpectation = nil
cancelExpectation = nil
super.tearDown()
try await super.tearDown()
}

// MARK: - Error Callback Tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ final class ShopPayViewControllerTests: XCTestCase {
var mockConfiguration: ShopifyAcceleratedCheckouts.Configuration!
var mockStorefront: TestStorefrontAPI!

override func setUp() {
super.setUp()
override func setUp() async throws {
try await super.setUp()

mockConfiguration = ShopifyAcceleratedCheckouts.Configuration(
storefrontDomain: "test-shop.myshopify.com",
Expand All @@ -19,11 +19,11 @@ final class ShopPayViewControllerTests: XCTestCase {
mockStorefront = TestStorefrontAPI()
}

override func tearDown() {
override func tearDown() async throws {
viewController = nil
mockConfiguration = nil
mockStorefront = nil
super.tearDown()
try await super.tearDown()
}

@available(iOS 16.0, *)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import XCTest
final class WalletControllerTests: XCTestCase {
var mockStorefront: TestStorefrontAPI!
var controller: MockWalletController!
override func setUp() {
super.setUp()
override func setUp() async throws {
try await super.setUp()
mockStorefront = TestStorefrontAPI()
}

override func tearDown() {
override func tearDown() async throws {
mockStorefront = nil
controller = nil
super.tearDown()
try await super.tearDown()
}

class MockWalletController: WalletController {
Expand Down
Loading
Loading