Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flag for checking if preload is called at least once. Else do not cac… #17

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion Sources/ShopifyCheckout/CheckoutView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CheckoutView: WKWebView {
private static var cache: CacheEntry?

static func `for`(checkout url: URL) -> CheckoutView {
guard ShopifyCheckout.configuration.preloading.enabled else {
guard ShopifyCheckout.configuration.preloading.enabled && ShopifyCheckout.configuration.preloading.preloadCalledAtLeastOnce else {
CheckoutView.cache = nil
return CheckoutView()
}
Expand Down
1 change: 1 addition & 0 deletions Sources/ShopifyCheckout/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@ extension Configuration {
extension Configuration {
public struct Preloading {
public var enabled: Bool = false
internal var preloadCalledAtLeastOnce: Bool = false
}
}
1 change: 1 addition & 0 deletions Sources/ShopifyCheckout/ShopifyCheckout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public func configure(_ block: (inout Configuration) -> Void) {

/// Preloads the checkout for faster presentation.
public func preload(checkout url: URL) {
ShopifyCheckout.configuration.preloading.preloadCalledAtLeastOnce = true
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think we need to reset this when the checkout completes or errors?

And/or clear the cache

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm it's a good question

guard configuration.preloading.enabled else { return }
CheckoutView.for(checkout: url).load(checkout: url)
}
Expand Down
56 changes: 56 additions & 0 deletions Tests/ShopifyCheckoutTests/CheckoutViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class CheckoutViewTests: XCTestCase {
view.viewDelegate = mockDelegate
}

override func tearDown() {
view.viewDelegate = nil
CheckoutView.invalidate()
}

func testEmailContactLinkDelegation() {
let link = URL(string: "mailto:[email protected]")!

Expand Down Expand Up @@ -117,4 +122,55 @@ class CheckoutViewTests: XCTestCase {

waitForExpectations(timeout: 0.5, handler: nil)
}

func testForReturnsNewViewInstanceIfPreloadingDisabled() {
ShopifyCheckout.configure {
$0.preloading.enabled = false
}

let viewOne = CheckoutView.for(checkout: URL(string: "http://shopify1.shopify.com/checkouts/cn/123")!)

let viewTwo = CheckoutView.for(checkout: URL(string: "http://shopify1.shopify.com/checkouts/cn/123")!)

XCTAssertNotEqual(viewOne, viewTwo)
}

func testForReturnsNewViewInstanceIfPreloadingEnabledButPreloadNeverCalled() {
ShopifyCheckout.configure {
$0.preloading.enabled = true
$0.preloading.preloadCalledAtLeastOnce = false
}

let viewOne = CheckoutView.for(checkout: URL(string: "http://shopify1.shopify.com/checkouts/cn/123")!)

let viewTwo = CheckoutView.for(checkout: URL(string: "http://shopify1.shopify.com/checkouts/cn/123")!)

XCTAssertNotEqual(viewOne, viewTwo)
}

func testForReturnsNewViewInstanceIfPreloadingEnabledAndCalledButUrlChanged() {
ShopifyCheckout.configure {
$0.preloading.enabled = true
$0.preloading.preloadCalledAtLeastOnce = true
}

let viewOne = CheckoutView.for(checkout: URL(string: "http://shopify1.shopify.com/checkouts/cn/123")!)

let viewTwo = CheckoutView.for(checkout: URL(string: "http://shopify1.shopify.com/checkouts/cn/124")!)

XCTAssertNotEqual(viewOne, viewTwo)
}

func testForReturnsCachedViewInstanceIfPreloadingEnabledAndCalledAndURLUnchanged() {
ShopifyCheckout.configure {
$0.preloading.enabled = true
$0.preloading.preloadCalledAtLeastOnce = true
}

let viewOne = CheckoutView.for(checkout: URL(string: "http://shopify1.shopify.com/checkouts/cn/123")!)

let viewTwo = CheckoutView.for(checkout: URL(string: "http://shopify1.shopify.com/checkouts/cn/123")!)

XCTAssertEqual(viewOne, viewTwo)
}
}