Skip to content

Commit

Permalink
lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mludowise-stripe committed Sep 24, 2024
1 parent 3b2a624 commit a73fcc7
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class EmbeddedComponentManager {
let fetchClientSecret: () async -> String?
let fonts: [EmbeddedComponentManager.CustomFontSource]
private(set) var appearance: EmbeddedComponentManager.Appearance

// This should only be used for tests and determines if webview
// content should load.
var shouldLoadContent: Bool = true
Expand Down Expand Up @@ -59,7 +59,7 @@ public class EmbeddedComponentManager {
item.updateAppearance(appearance: appearance)
}
}

/// Creates a payouts component
/// - Seealso: https://docs.stripe.com/connect/supported-embedded-components/payouts
public func createPayoutsViewController() -> PayoutsViewController {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ enum StripeConnectConstants {
*/
static let allowedHosts: Set<String> = [
"connect-js.stripe.com",
"connect.stripe.com"
"connect.stripe.com",
]

static let connectJSBaseURL = URL(string: "https://connect-js.stripe.com/v1.0/ios_webview.html")!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class ConnectComponentWebView: ConnectWebView {

/// The current notification center instance
private let notificationCenter: NotificationCenter

private let setterMessageHandler: OnSetterFunctionCalledMessageHandler = .init()

let activityIndicator: ActivityIndicator = {
let activityIndicator = ActivityIndicator()
activityIndicator.hidesWhenStopped = true
Expand Down Expand Up @@ -62,7 +62,7 @@ class ConnectComponentWebView: ConnectWebView {
self.addSubview(activityIndicator)
NSLayoutConstraint.activate([
activityIndicator.centerXAnchor.constraint(equalTo: self.centerXAnchor),
activityIndicator.centerYAnchor.constraint(equalTo: self.centerYAnchor)
activityIndicator.centerYAnchor.constraint(equalTo: self.centerYAnchor),
])
componentManager.registerChild(self)
addMessageHandlers()
Expand Down Expand Up @@ -98,7 +98,7 @@ extension ConnectComponentWebView {
contentWorld: WKContentWorld = .page) {
contentController.add(messageHandler, contentWorld: contentWorld, name: messageHandler.name)
}

func addMessageHandler(_ handler: OnSetterFunctionCalledMessageHandler.Handler) {
setterMessageHandler.addHandler(handler: handler)
}
Expand Down Expand Up @@ -154,6 +154,7 @@ private extension ConnectComponentWebView {
object: nil,
queue: .main
) { [weak self] _ in
// swiftlint:disable:previous unused_capture_list
guard let self else { return }
sendMessage(UpdateConnectInstanceSender(payload: .init(locale: webLocale.webIdentifier, appearance: .init(appearance: componentManager.appearance, traitCollection: traitCollection))))
}
Expand Down
23 changes: 11 additions & 12 deletions StripeConnect/StripeConnectTests/AppearanceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
// Created by Chris Mays on 9/4/24.
//

import XCTest
import UIKit
@_spi(PrivateBetaConnect) @testable import StripeConnect
import UIKit
import XCTest

class AppearanceTests: XCTestCase {
typealias Appearance = EmbeddedComponentManager.Appearance

let testAppearance: Appearance = {
var appearance = EmbeddedComponentManager.Appearance()
appearance.typography.font = UIFont(name: "Helvetica", size: 16)
Expand Down Expand Up @@ -85,7 +85,7 @@ class AppearanceTests: XCTestCase {
appearance.cornerRadius.overlay = 12
return appearance
}()

func testAllFieldsAreFilledWithCorrectValuesInDictionary() {
let actualValues = testAppearance.asDictionary(traitCollection: .init())
let expectedValues: [String: String] = [
Expand Down Expand Up @@ -148,37 +148,36 @@ class AppearanceTests: XCTestCase {
"buttonBorderRadius": "8px",
"formBorderRadius": "6px",
"badgeBorderRadius": "10px",
"overlayBorderRadius": "12px"
"overlayBorderRadius": "12px",
]

XCTAssertEqual(actualValues, expectedValues)
}



func testDefaultAppearance() {
let appearance: Appearance = .default
XCTAssertEqual(appearance.asDictionary(traitCollection: .init()), [
"fontFamily": "-apple-system",
"fontSizeBase": "16px",
])
}

func testColorsChangeBasedOnTraitCollection() {
var appearance: Appearance = .default

appearance.colors.actionPrimaryText = UIColor { $0.userInterfaceStyle == .light ? .red : .green }
appearance.colors.background = UIColor { $0.userInterfaceStyle == .light ? .white : .black }
appearance.colors.text = UIColor { $0.userInterfaceStyle == .light ? .black : .white }

let lightModeTraits = UITraitCollection(userInterfaceStyle: .light)
let darkModeTraits = UITraitCollection(userInterfaceStyle: .dark)

XCTAssertEqual(appearance.asDictionary(traitCollection: lightModeTraits)["actionPrimaryColorText"], "rgb(255, 0, 0)")
XCTAssertEqual(appearance.asDictionary(traitCollection: darkModeTraits)["actionPrimaryColorText"], "rgb(0, 255, 0)")

XCTAssertEqual(appearance.asDictionary(traitCollection: lightModeTraits)["colorBackground"], "rgb(255, 255, 255)")
XCTAssertEqual(appearance.asDictionary(traitCollection: darkModeTraits)["colorBackground"], "rgb(0, 0, 0)")

XCTAssertEqual(appearance.asDictionary(traitCollection: lightModeTraits)["colorText"], "rgb(0, 0, 0)")
XCTAssertEqual(appearance.asDictionary(traitCollection: darkModeTraits)["colorText"], "rgb(255, 255, 255)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
//

import Foundation
@_spi(STP) import StripeCore
import SafariServices
@_spi(PrivateBetaConnect) @testable import StripeConnect
import XCTest
@_spi(STP) import StripeCore
import WebKit
import SafariServices
import XCTest

class ConnectComponentWebViewTests: XCTestCase {

Expand All @@ -22,12 +22,12 @@ class ConnectComponentWebViewTests: XCTestCase {
return "test"
})
let webView = ConnectComponentWebView(componentManager: componentManager, componentType: .payouts, loadContent: false)

try await webView.evaluateMessageWithReply(name: "fetchClientSecret",
json: "{}",
expectedResponse: "test")
}

@MainActor
func testFetchInitParams() async throws {
let message = FetchInitParamsMessageHandler.Reply(locale: "fr-FR", appearance: .default)
Expand All @@ -36,13 +36,12 @@ class ConnectComponentWebViewTests: XCTestCase {
componentType: .payouts,
webLocale: Locale(identifier: "fr_FR"),
loadContent: false)



try await webView.evaluateMessageWithReply(name: "fetchInitParams",
json: "{}",
expectedResponse: message)
}

@MainActor
func testUpdateAppearance() async throws {
let componentManager = componentManagerAssertingOnFetch()
Expand All @@ -54,41 +53,41 @@ class ConnectComponentWebViewTests: XCTestCase {
appearance.spacingUnit = 5
let expectation = try webView.expectationForMessageReceived(sender: UpdateConnectInstanceSender(payload: .init(locale: "fr-FR", appearance: .init(appearance: appearance, traitCollection: UITraitCollection()))))
componentManager.update(appearance: appearance)

// Ensures the appearance on component manager was set.
XCTAssertEqual(appearance.asDictionary(traitCollection: .init()), componentManager.appearance.asDictionary(traitCollection: .init()))

await fulfillment(of: [expectation], timeout: TestHelpers.defaultTimeout)
}

@MainActor
func testFetchInitParamsTraitCollection() async throws {
var appearance = EmbeddedComponentManager.Appearance()
appearance.colors.actionPrimaryText = UIColor { $0.userInterfaceStyle == .light ? .black : .white }

let componentManager = componentManagerAssertingOnFetch(appearance: appearance)

let webView = ConnectComponentWebView(componentManager: componentManager,
componentType: .payouts,
webLocale: Locale(identifier: "fr_FR"),
loadContent: false)

webView.triggerTraitCollectionChange(style: .dark)

try await webView.evaluateMessageWithReply(name: "fetchInitParams",
json: "{}",
expectedResponse: """
{"appearance":{"variables":{"actionPrimaryColorText":"rgb(255, 255, 255)","fontFamily":"-apple-system","fontSizeBase":"16px"}},"fonts":[],"locale":"fr-FR"}
""")
}

@MainActor
func testUpdateTraitCollection() async throws {
var appearance = EmbeddedComponentManager.Appearance()
appearance.colors.actionPrimaryText = UIColor { $0.userInterfaceStyle == .light ? .red : .green }

let componentManager = componentManagerAssertingOnFetch(appearance: appearance)

let webView = ConnectComponentWebView(componentManager: componentManager,
componentType: .payouts,
webLocale: Locale(identifier: "fr_FR"),
Expand All @@ -97,10 +96,10 @@ class ConnectComponentWebViewTests: XCTestCase {
let expectation = try webView.expectationForMessageReceived(sender: UpdateConnectInstanceSender(payload: .init(locale: "fr-FR", appearance: .init(appearance: appearance, traitCollection: UITraitCollection(userInterfaceStyle: .dark)))))

webView.triggerTraitCollectionChange(style: .dark)

await fulfillment(of: [expectation], timeout: TestHelpers.defaultTimeout)
}

@MainActor
func testLocale() async throws {
let componentManager = componentManagerAssertingOnFetch()
Expand All @@ -111,14 +110,14 @@ class ConnectComponentWebViewTests: XCTestCase {
notificationCenter: notificationCenter,
webLocale: Locale(identifier: "fr_FR"),
loadContent: false)

let expectation = try webView.expectationForMessageReceived(sender: UpdateConnectInstanceSender(payload: .init(locale: "fr-FR", appearance: .default)))

notificationCenter.post(name: NSLocale.currentLocaleDidChangeNotification, object: nil)

await fulfillment(of: [expectation], timeout: TestHelpers.defaultTimeout)
}

func componentManagerAssertingOnFetch(appearance: Appearance = .default, fonts: [EmbeddedComponentManager.CustomFontSource] = []) -> EmbeddedComponentManager {
EmbeddedComponentManager(apiClient: .init(publishableKey: "test"),
appearance: appearance,
Expand All @@ -128,7 +127,7 @@ class ConnectComponentWebViewTests: XCTestCase {
return ""
})
}

@MainActor
func testFetchInitParamsWithFontSource() async throws {
let testBundle = Bundle(for: type(of: self))
Expand All @@ -139,11 +138,10 @@ class ConnectComponentWebViewTests: XCTestCase {
componentType: .payouts,
webLocale: Locale(identifier: "fr_FR"),
loadContent: false)



try await webView.evaluateMessageWithReply(name: "fetchInitParams",
json: "{}",
expectedResponse:"""
expectedResponse: """
{"appearance":{"variables":{"fontFamily":"-apple-system","fontSizeBase":"16px"}},"fonts":[{"family":".AppleSystemUIFont","src":"url(data:font\\/txt;charset=utf-8;base64,dGVzdAo=)","weight":"400"}],"locale":"fr-FR"}
""")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class UpdateConnectInstanceSenderTests: ScriptWebTestBase {
func testSendMessage() throws {
try validateMessageSent(sender: UpdateConnectInstanceSender(payload: .init(locale: "en", appearance: .default)))
}

func testSenderSignature() {
XCTAssertEqual(
UpdateConnectInstanceSender(payload: .init(locale: "en", appearance: .default)).javascriptMessage,
Expand Down

0 comments on commit a73fcc7

Please sign in to comment.