Skip to content

Commit

Permalink
listen for checkout blocking events to change header visibility when …
Browse files Browse the repository at this point in the history
…web modals are shown
  • Loading branch information
kiftio committed Oct 20, 2023
1 parent c199773 commit faeda12
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Sources/ShopifyCheckout/CheckoutBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ extension CheckoutBridge {
case checkoutCanceled
case checkoutExpired
case checkoutUnavailable
case checkoutModalToggled(modalVisible: Bool)
case unsupported(String)

enum CodingKeys: String, CodingKey {
Expand All @@ -78,6 +79,9 @@ extension CheckoutBridge {
case "error":
// needs to support .checkoutUnavailable by parsing error payload on body
self = .checkoutExpired
case "checkoutBlockingEvent":
let modalVisible = try container.decode(String.self, forKey: .body)
self = .checkoutModalToggled(modalVisible: Bool(modalVisible)!)
default:
self = .unsupported(name)
}
Expand Down
3 changes: 3 additions & 0 deletions Sources/ShopifyCheckout/CheckoutView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ protocol CheckoutViewDelegate: AnyObject {
func checkoutViewDidFinishNavigation()
func checkoutViewDidClickLink(url: URL)
func checkoutViewDidFailWithError(error: CheckoutError)
func checkoutViewDidToggleModal(modalVisible: Bool)
}

class CheckoutView: WKWebView {
Expand Down Expand Up @@ -104,6 +105,8 @@ extension CheckoutView: WKScriptMessageHandler {
case .checkoutUnavailable:
CheckoutView.cache = nil
viewDelegate?.checkoutViewDidFailWithError(error: .checkoutUnavailable(message: "Checkout unavailable."))
case let .checkoutModalToggled(modalVisible):
viewDelegate?.checkoutViewDidToggleModal(modalVisible: modalVisible)
default:
()
}
Expand Down
5 changes: 5 additions & 0 deletions Sources/ShopifyCheckout/CheckoutViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,9 @@ extension CheckoutViewController: CheckoutViewDelegate {
func checkoutViewDidClickLink(url: URL) {
delegate?.checkoutDidClickLink(url: url)
}

func checkoutViewDidToggleModal(modalVisible: Bool) {
guard let navigationController = self.navigationController else { return }
navigationController.setNavigationBarHidden(modalVisible, animated: true)
}
}
15 changes: 15 additions & 0 deletions Tests/ShopifyCheckoutTests/CheckoutBridgeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,19 @@ class CheckoutBridgeTests: XCTestCase {
return XCTFail("expected CheckoutScriptMessage.checkoutExpired, got \(result)")
}
}

func testDecodeSupportsCheckoutBlockingEvent() throws {
let mock = WKScriptMessageMock(body: """
{
"name": "checkoutBlockingEvent",
"body": "true"
}
""")

let result = try CheckoutBridge.decode(mock)

guard case CheckoutBridge.WebEvent.checkoutModalToggled = result else {
return XCTFail("expected CheckoutScriptMessage.checkoutModalToggled, got \(result)")
}
}
}
13 changes: 13 additions & 0 deletions Tests/ShopifyCheckoutTests/CheckoutViewControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ class CheckoutViewDelegateTests: XCTestCase {

private let checkoutURL = URL(string: "https://checkout-sdk.myshopify.com")!
private var viewController: CheckoutViewController!
private var navigationController: UINavigationController!

override func setUp() {
ShopifyCheckout.configure {
$0.preloading.enabled = true
}
viewController = CheckoutViewController(
checkoutURL: checkoutURL, delegate: ExampleDelegate())

navigationController = UINavigationController(rootViewController: viewController)
}

func testTitleIsSetToCheckout() {
Expand Down Expand Up @@ -97,4 +100,14 @@ class CheckoutViewDelegateTests: XCTestCase {
let three = CheckoutView.for(checkout: checkoutURL)
XCTAssertEqual(two, three)
}

func testCheckoutViewDidToggleModalAddsAndRemovesNavigationBar() {
XCTAssertFalse(viewController.navigationController!.isNavigationBarHidden)

viewController.checkoutViewDidToggleModal(modalVisible: true)
XCTAssertTrue(viewController.navigationController!.isNavigationBarHidden)

viewController.checkoutViewDidToggleModal(modalVisible: false)
XCTAssertFalse(viewController.navigationController!.isNavigationBarHidden)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class MockCheckoutViewDelegate: CheckoutViewDelegate {

var didFailWithErrorExpectation: XCTestExpectation?

var didToggleModalExpectation: XCTestExpectation?

func checkoutViewDidStartNavigation() {
didStartNavigationExpectation?.fulfill()
}
Expand All @@ -60,4 +62,8 @@ class MockCheckoutViewDelegate: CheckoutViewDelegate {
func checkoutViewDidFailWithError(error: CheckoutError) {
didFailWithErrorExpectation?.fulfill()
}

func checkoutViewDidToggleModal() {
didToggleModalExpectation?.fulfill()
}
}

0 comments on commit faeda12

Please sign in to comment.