Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 1 addition & 24 deletions Sources/ShopifyCheckoutSheetKit/CheckoutBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@

enum BridgeError: Swift.Error {
case invalidBridgeEvent(Swift.Error? = nil)
case unencodableInstrumentation(Swift.Error? = nil)
}

protocol CheckoutBridgeProtocol {
static func instrument(_ webView: WKWebView, _ instrumentation: InstrumentationPayload)
static func sendMessage(_ webView: WKWebView, messageName: String, messageBody: String?)
}

Expand Down Expand Up @@ -76,12 +74,7 @@
}
}

static func instrument(_ webView: WKWebView, _ instrumentation: InstrumentationPayload) {
if let payload = instrumentation.toBridgeEvent() {
sendMessage(webView, messageName: "instrumentation", messageBody: payload)
}
}

Check failure on line 77 in Sources/ShopifyCheckoutSheetKit/CheckoutBridge.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Vertical Whitespace Violation: Limit vertical whitespace to a single empty line; currently 2 (vertical_whitespace)

Check warning on line 77 in Sources/ShopifyCheckoutSheetKit/CheckoutBridge.swift

View workflow job for this annotation

GitHub Actions / call-workflow-passing-data / test

Vertical Whitespace Violation: Limit vertical whitespace to a single empty line; currently 2 (vertical_whitespace)

Check warning on line 77 in Sources/ShopifyCheckoutSheetKit/CheckoutBridge.swift

View workflow job for this annotation

GitHub Actions / call-workflow-passing-data / test

Vertical Whitespace Violation: Limit vertical whitespace to a single empty line; currently 2 (vertical_whitespace)
static func sendMessage(_ webView: WKWebView, messageName: String, messageBody: String?) {
let dispatchMessageBody: String
if let body = messageBody {
Expand Down Expand Up @@ -180,23 +173,7 @@
}
}

struct InstrumentationPayload: Codable {
var name: String
var value: Int
var type: InstrumentationType
var tags: [String: String] = [:]
}

enum InstrumentationType: String, Codable {
case histogram
}

extension InstrumentationPayload {
func toBridgeEvent() -> String? {
SdkToWebEvent(detail: self).toJson()
}
}

Check failure on line 176 in Sources/ShopifyCheckoutSheetKit/CheckoutBridge.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Vertical Whitespace Violation: Limit vertical whitespace to a single empty line; currently 2 (vertical_whitespace)
struct SdkToWebEvent<T: Codable>: Codable {
var detail: T
}
Expand All @@ -207,9 +184,9 @@
let jsonData = try JSONEncoder().encode(self)
return String(data: jsonData, encoding: .utf8)
} catch {
print(#function, BridgeError.unencodableInstrumentation(error))
return nil
}

return nil

Check warning on line 190 in Sources/ShopifyCheckoutSheetKit/CheckoutBridge.swift

View workflow job for this annotation

GitHub Actions / call-workflow-passing-data / test

will never be executed

Check warning on line 190 in Sources/ShopifyCheckoutSheetKit/CheckoutBridge.swift

View workflow job for this annotation

GitHub Actions / call-workflow-passing-data / test

will never be executed
}
}
13 changes: 0 additions & 13 deletions Sources/ShopifyCheckoutSheetKit/CheckoutWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,7 @@
}
}

func instrument(_ payload: InstrumentationPayload) {
OSLogger.shared.debug("Emitting instrumentation event with payload: \(payload)")
checkoutBridge.instrument(self, payload)
}

Check failure on line 225 in Sources/ShopifyCheckoutSheetKit/CheckoutWebView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Vertical Whitespace Violation: Limit vertical whitespace to a single empty line; currently 2 (vertical_whitespace)

Check warning on line 225 in Sources/ShopifyCheckoutSheetKit/CheckoutWebView.swift

View workflow job for this annotation

GitHub Actions / call-workflow-passing-data / test

Vertical Whitespace Violation: Limit vertical whitespace to a single empty line; currently 2 (vertical_whitespace)

Check warning on line 225 in Sources/ShopifyCheckoutSheetKit/CheckoutWebView.swift

View workflow job for this annotation

GitHub Actions / call-workflow-passing-data / test

Vertical Whitespace Violation: Limit vertical whitespace to a single empty line; currently 2 (vertical_whitespace)
// MARK: -

func load(checkout url: URL, isPreload: Bool = false) {
Expand Down Expand Up @@ -403,19 +399,10 @@
let endTime = Date()
let diff = endTime.timeIntervalSince(startTime)
let message = "Loaded checkout in \(String(format: "%.2f", diff))s"
let preload = String(isPreloadRequest)

Check warning on line 402 in Sources/ShopifyCheckoutSheetKit/CheckoutWebView.swift

View workflow job for this annotation

GitHub Actions / call-workflow-passing-data / test

initialization of immutable value 'preload' was never used; consider replacing with assignment to '_' or removing it

Check warning on line 402 in Sources/ShopifyCheckoutSheetKit/CheckoutWebView.swift

View workflow job for this annotation

GitHub Actions / call-workflow-passing-data / test

initialization of immutable value 'preload' was never used; consider replacing with assignment to '_' or removing it

ShopifyCheckoutSheetKit.configuration.logger.log(message)

if isBridgeAttached {
instrument(
InstrumentationPayload(
name: "checkout_finished_loading",
value: Int(diff * 1000),
type: .histogram,
tags: ["preloading": preload]
))
}
}
checkoutDidLoad = true
timer = nil
Expand Down
13 changes: 0 additions & 13 deletions Tests/ShopifyCheckoutSheetKitTests/CheckoutBridgeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -302,20 +302,7 @@
}
}

func testInstrumentationPayloadToBridgeEvent() {
let payload = InstrumentationPayload(name: "test", value: 1, type: .histogram)
let jsonString = payload.toBridgeEvent()
XCTAssertNotNil(jsonString)

if let jsonData = jsonString?.data(using: .utf8) {
let decodedPayload = try? JSONDecoder().decode(SdkToWebEvent<InstrumentationPayload>.self, from: jsonData)
XCTAssertNotNil(decodedPayload)
XCTAssertEqual(decodedPayload?.detail.name, "test")
XCTAssertEqual(decodedPayload?.detail.value, 1)
XCTAssertEqual(decodedPayload?.detail.type, .histogram)
}
}

Check failure on line 305 in Tests/ShopifyCheckoutSheetKitTests/CheckoutBridgeTests.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Vertical Whitespace Violation: Limit vertical whitespace to a single empty line; currently 2 (vertical_whitespace)

Check warning on line 305 in Tests/ShopifyCheckoutSheetKitTests/CheckoutBridgeTests.swift

View workflow job for this annotation

GitHub Actions / call-workflow-passing-data / test

Vertical Whitespace Violation: Limit vertical whitespace to a single empty line; currently 2 (vertical_whitespace)

Check warning on line 305 in Tests/ShopifyCheckoutSheetKitTests/CheckoutBridgeTests.swift

View workflow job for this annotation

GitHub Actions / call-workflow-passing-data / test

Vertical Whitespace Violation: Limit vertical whitespace to a single empty line; currently 2 (vertical_whitespace)
func testSendMessageShouldCallEvaluateJavaScriptPresented() {
let webView = MockWebView()
webView.expectedScript = expectedPresentedScript()
Expand Down
57 changes: 0 additions & 57 deletions Tests/ShopifyCheckoutSheetKitTests/CheckoutWebViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -375,56 +375,9 @@
XCTAssertFalse(webView.isPreloadRequest)
}

func testInstrumentRequestWithPreloadingTag() {
let webView = LoadedRequestObservableWebView()

webView.load(
checkout: URL(string: "https://checkout-sdk.myshopify.io")!,
isPreload: true
)

webView.timer = Date()
webView.webView(webView, didFinish: nil)

XCTAssertTrue(webView.isPreloadRequest)
XCTAssertEqual(webView.lastInstrumentationPayload?.name, "checkout_finished_loading")
XCTAssertEqual(webView.lastInstrumentationPayload?.type, .histogram)
XCTAssertEqual(webView.lastInstrumentationPayload?.tags, ["preloading": "true"])
}

func testDoesNotInstrumentRequestWithPreloadingTag() {
let webView = LoadedRequestObservableWebView()

webView.load(
checkout: URL(string: "https://checkout-sdk.myshopify.io")!,
isPreload: false
)

webView.timer = Date()
webView.webView(webView, didFinish: nil)

XCTAssertFalse(webView.isPreloadRequest)
XCTAssertEqual(webView.lastInstrumentationPayload?.name, "checkout_finished_loading")
XCTAssertEqual(webView.lastInstrumentationPayload?.type, .histogram)
XCTAssertEqual(webView.lastInstrumentationPayload?.tags, ["preloading": "false"])
}

func testDoesNotInstrumentPreloadingTagIfDisabled() {
let webView = LoadedRequestObservableWebView()
ShopifyCheckoutSheetKit.configuration.preloading.enabled = false

webView.load(
checkout: URL(string: "https://checkout-sdk.myshopify.io")!,
/// This is not respected if preloading is disabled at a config level
isPreload: true
)

webView.timer = Date()
webView.webView(webView, didFinish: nil)

XCTAssertEqual(webView.lastInstrumentationPayload?.tags, ["preloading": "false"])
}

Check failure on line 380 in Tests/ShopifyCheckoutSheetKitTests/CheckoutWebViewTests.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Vertical Whitespace Violation: Limit vertical whitespace to a single empty line; currently 4 (vertical_whitespace)

Check warning on line 380 in Tests/ShopifyCheckoutSheetKitTests/CheckoutWebViewTests.swift

View workflow job for this annotation

GitHub Actions / call-workflow-passing-data / test

Vertical Whitespace Violation: Limit vertical whitespace to a single empty line; currently 4 (vertical_whitespace)

Check warning on line 380 in Tests/ShopifyCheckoutSheetKitTests/CheckoutWebViewTests.swift

View workflow job for this annotation

GitHub Actions / call-workflow-passing-data / test

Vertical Whitespace Violation: Limit vertical whitespace to a single empty line; currently 4 (vertical_whitespace)
func testDetachBridgeCalledOnInit() {
ShopifyCheckoutSheetKit.configuration.preloading.enabled = false
let url = URL(string: "http://shopify1.shopify.com/checkouts/cn/123")
Expand Down Expand Up @@ -486,26 +439,16 @@

class LoadedRequestObservableWebView: CheckoutWebView {
var lastLoadedURLRequest: URLRequest?
var lastInstrumentationPayload: InstrumentationPayload?

override func load(_ request: URLRequest) -> WKNavigation? {
lastLoadedURLRequest = request
return nil
}

override func instrument(_ payload: InstrumentationPayload) {
lastInstrumentationPayload = payload
}
}

class MockCheckoutBridge: CheckoutBridgeProtocol {
static var instrumentCalled = false
static var sendMessageCalled = false

static func instrument(_: WKWebView, _: InstrumentationPayload) {
instrumentCalled = true
}

static func sendMessage(_: WKWebView, messageName _: String, messageBody _: String?) {
sendMessageCalled = true
}
Expand Down
Loading