Skip to content

Commit

Permalink
Add test for orderId
Browse files Browse the repository at this point in the history
  • Loading branch information
markmur committed Apr 23, 2024
1 parent 35383bc commit 167dfd2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
18 changes: 11 additions & 7 deletions Sources/ShopifyCheckoutSheetKit/CheckoutWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,18 @@ extension CheckoutWebView: WKNavigationDelegate {
}

private func isConfirmation(url: URL) -> Bool {
do {
let urlString = url.absoluteString
let regex = try NSRegularExpression(pattern: "thank[-_]you", options: .caseInsensitive)
let range = NSRange(urlString.startIndex..., in: urlString)
return regex.firstMatch(in: urlString, options: [], range: range) != nil
} catch {
return false
let pathComponents = url.pathComponents
let pattern = "^(thank[_-]you)$"
let regex = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive)

for component in pathComponents {
let range = NSRange(location: 0, length: component.utf16.count)
if regex?.firstMatch(in: component, options: [], range: range) != nil {
return true
}
}

return false
}

private func getOrderIdFromQuery(url: URL) -> String? {
Expand Down
31 changes: 28 additions & 3 deletions Tests/ShopifyCheckoutSheetKitTests/CheckoutWebViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@ class CheckoutWebViewTests: XCTestCase {
view.viewDelegate = mockDelegate
}

func testUsesRecoveryAgent() {
let backgroundColor: UIColor = .systemRed
ShopifyCheckoutSheetKit.configuration.backgroundColor = backgroundColor
private func createRecoveryAgent() -> CheckoutWebView {
recovery = CheckoutWebView.for(checkout: url, recovery: true)
mockDelegate = MockCheckoutWebViewDelegate()
recovery.viewDelegate = mockDelegate
return recovery
}

func testUsesRecoveryAgent() {
let backgroundColor: UIColor = .systemRed
ShopifyCheckoutSheetKit.configuration.backgroundColor = backgroundColor
ShopifyCheckoutSheetKit.configuration.colorScheme = .automatic
recovery = createRecoveryAgent()

XCTAssertTrue(recovery.isRecovery)
XCTAssertFalse(recovery.isBridgeAttached)
Expand Down Expand Up @@ -145,6 +151,25 @@ class CheckoutWebViewTests: XCTestCase {
}
}

func testObtainsOrderIDFromQuery() {
let url = URL(string: "http://shopify1.shopify.com/checkouts/c/12345/thank-you?orderId=1234")!
recovery = createRecoveryAgent()
recovery.load(checkout: url)
let didCompleteCheckoutExpectation = expectation(description: "checkoutViewDidCompleteCheckout was called")

mockDelegate.didEmitCheckoutCompletedEventExpectation = didCompleteCheckoutExpectation
recovery.viewDelegate = mockDelegate

let urlResponse = HTTPURLResponse(url: url, statusCode: 200, httpVersion: nil, headerFields: nil)!

let policy = recovery.handleResponse(urlResponse)
XCTAssertEqual(policy, .allow)

waitForExpectations(timeout: 5) { _ in
XCTAssertEqual(self.mockDelegate.completedEventReceived?.orderDetails.id, "1234")
}
}

func test404responseOnCheckoutURLCodeDelegation() {
view.load(checkout: URL(string: "http://shopify1.shopify.com/checkouts/cn/123")!)
let link = view.url!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import XCTest
@testable import ShopifyCheckoutSheetKit

class MockCheckoutWebViewDelegate: CheckoutWebViewDelegate {
var completedEventReceived: CheckoutCompletedEvent?

var errorReceived: CheckoutError?

var didStartNavigationExpectation: XCTestExpectation?
Expand Down Expand Up @@ -79,6 +81,7 @@ class MockCheckoutWebViewDelegate: CheckoutWebViewDelegate {
}

func checkoutViewDidCompleteCheckout(event: ShopifyCheckoutSheetKit.CheckoutCompletedEvent) {
completedEventReceived = event
didEmitCheckoutCompletedEventExpectation?.fulfill()
}
}

0 comments on commit 167dfd2

Please sign in to comment.