Skip to content

Commit

Permalink
update external link interceptor with optional param to override defa…
Browse files Browse the repository at this point in the history
…ult behaviour
  • Loading branch information
cianbuckley committed Oct 9, 2023
1 parent bed3e67 commit 2569581
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
9 changes: 8 additions & 1 deletion Sources/ShopifyCheckout/CheckoutView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,14 @@ extension CheckoutView: WKNavigationDelegate {
}

private func isExternalLink(_ action: WKNavigationAction) -> Bool {
return action.navigationType == .linkActivated && action.targetFrame == nil
if action.navigationType == .linkActivated && action.targetFrame == nil { return true }

guard let url = action.request.url else { return false }
guard let url = URLComponents(string: url.absoluteString) else { return false }

guard let openExternally = url.queryItems?.first(where: { $0.name == "open_externally"})?.value else { return false }

Check failure on line 178 in Sources/ShopifyCheckout/CheckoutView.swift

View workflow job for this annotation

GitHub Actions / lint

Closure Spacing Violation: Closure expressions should have a single space inside each brace (closure_spacing)

return openExternally == "true"
}

private func isMailOrTelLink(_ url: URL) -> Bool {
Expand Down
17 changes: 17 additions & 0 deletions Tests/ShopifyCheckoutTests/CheckoutViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@ class CheckoutViewTests: XCTestCase {
wait(for: [didClickLinkExpectation], timeout: 1)
}

func testURLLinkDelegationWithExternalParam() {
let link = URL(string: "https://www.shopify.com/legal/privacy/app-users?open_externally=true")!

let delegate = MockCheckoutViewDelegate()
let didClickLinkExpectation = expectation(
description: "checkoutViewDidClickLink was called"
)
delegate.didClickLinkExpectation = didClickLinkExpectation
view.viewDelegate = delegate

view.webView(view, decidePolicyFor: MockExternalNavigationAction(url: link, navigationType: .other)) { policy in
XCTAssertEqual(policy, .cancel)
}

wait(for: [didClickLinkExpectation], timeout: 1)
}

func test410responseOnCheckoutURLCodeDelegation() {
view.load(checkout: URL(string: "http://shopify1.shopify.com/checkouts/cn/123")!)
let link = view.url!
Expand Down
7 changes: 5 additions & 2 deletions Tests/ShopifyCheckoutTests/Mocks/MockNavigationAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,24 @@ class MockNavigationAction: WKNavigationAction {

class MockExternalNavigationAction: WKNavigationAction {
private let mockRequest: URLRequest
private let navType: WKNavigationType

override var request: URLRequest {
return mockRequest
}

override var navigationType: WKNavigationType {
return .linkActivated
return self.navType
}

override var targetFrame: WKFrameInfo? {
return nil
}

init(url: URL) {
init(url: URL, navigationType: WKNavigationType = .linkActivated) {
self.mockRequest = URLRequest(url: url)
self.navType = navigationType
super.init()
}
}

Check failure on line 61 in Tests/ShopifyCheckoutTests/Mocks/MockNavigationAction.swift

View workflow job for this annotation

GitHub Actions / lint

Trailing Newline Violation: Files should have a single trailing newline (trailing_newline)

0 comments on commit 2569581

Please sign in to comment.