Skip to content

Commit

Permalink
Merge branch 'main' into igrigorik-sdk-ios-readme
Browse files Browse the repository at this point in the history
  • Loading branch information
markmur authored Oct 17, 2023
2 parents 61e2ee7 + 78aa698 commit 09de8ed
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ class SettingsViewController: UITableViewController {

switch Section.from(indexPath.section) {
case Section.preloading:
content.text = "Use Preloading"
content.text = "Preload checkout"
cell.accessoryView = preloadingSwitch
case Section.vaultedState:
content.text = "Use Vaulted State"
content.text = "Prefill buyer information"
cell.accessoryView = vaultedStateSwitch
case Section.colorScheme:
let scheme = colorScheme(at: indexPath)
Expand Down
5 changes: 4 additions & 1 deletion Sources/ShopifyCheckout/CheckoutViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ class CheckoutViewController: UIViewController {
}
}

@IBAction private func close() {
@IBAction internal func close() {
CheckoutView.invalidate()
delegate?.checkoutDidCancel()
}
}
Expand All @@ -128,10 +129,12 @@ extension CheckoutViewController: CheckoutViewDelegate {

func checkoutViewDidCompleteCheckout() {
ConfettiCannon.fire(in: view)
CheckoutView.invalidate()
delegate?.checkoutDidComplete()
}

func checkoutViewDidFailWithError(error: CheckoutError) {
CheckoutView.invalidate()
delegate?.checkoutDidFail(error: error)
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/ShopifyCheckout/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ extension Configuration {

extension Configuration {
public struct Preloading {
public var enabled: Bool = false
public var enabled: Bool = true
}
}
88 changes: 88 additions & 0 deletions Tests/ShopifyCheckoutTests/CheckoutViewControllerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
MIT License

Copyright 2023 - Present, Shopify Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import XCTest
import WebKit
@testable import ShopifyCheckout

class CheckoutViewDelegateTests: XCTestCase {

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

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

func testTitleIsSetToCheckout() {
XCTAssertEqual(viewController.title, "Checkout")
}

func testCheckoutViewDidCompleteCheckoutInvalidatesViewCache() {
let one = CheckoutView.for(checkout: checkoutURL)
let two = CheckoutView.for(checkout: checkoutURL)
XCTAssertEqual(one, two)

viewController.checkoutViewDidCompleteCheckout()

let three = CheckoutView.for(checkout: checkoutURL)
XCTAssertNotEqual(two, three)
}

func testCheckoutViewDidFailWithErrorInvalidatesViewCache() {
let one = CheckoutView.for(checkout: checkoutURL)
let two = CheckoutView.for(checkout: checkoutURL)
XCTAssertEqual(one, two)

viewController.checkoutViewDidFailWithError(error: .checkoutUnavailable(message: "error"))

let three = CheckoutView.for(checkout: checkoutURL)
XCTAssertNotEqual(two, three)
}

func testCloseInvalidatesViewCache() {
let one = CheckoutView.for(checkout: checkoutURL)
let two = CheckoutView.for(checkout: checkoutURL)
XCTAssertEqual(one, two)

viewController.close()

let three = CheckoutView.for(checkout: checkoutURL)
XCTAssertNotEqual(two, three)
}

func testCheckoutViewDidClickLinkDoesNotInvalidateViewCache() {
let one = CheckoutView.for(checkout: checkoutURL)
let two = CheckoutView.for(checkout: checkoutURL)
XCTAssertEqual(one, two)

viewController.checkoutViewDidClickLink(url: URL(string: "https://shopify.com/anything")!)

let three = CheckoutView.for(checkout: checkoutURL)
XCTAssertEqual(two, three)
}
}
42 changes: 42 additions & 0 deletions Tests/ShopifyCheckoutTests/Mocks/MockCheckoutDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
MIT License

Copyright 2023 - Present, Shopify Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import XCTest
@testable import ShopifyCheckout

class ExampleDelegate: CheckoutDelegate {
func checkoutDidComplete() {
}

func checkoutDidCancel() {
}

func checkoutDidFail(errors: [ShopifyCheckout.CheckoutError]) {
}

func checkoutDidFail(error: ShopifyCheckout.CheckoutError) {
}

func checkoutDidClickContactLink(url: URL) {
}
}

0 comments on commit 09de8ed

Please sign in to comment.