-
Notifications
You must be signed in to change notification settings - Fork 995
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add message handling for closing and mobile inputs.
- Loading branch information
1 parent
d6a21c6
commit 5da48e1
Showing
6 changed files
with
147 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
StripeConnect/StripeConnect/Source/Internal/Webview/MessageHandlers/CloseWebView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// CloseWebView.swift | ||
// StripeConnect | ||
// | ||
// Created by Chris Mays on 2/12/25. | ||
// | ||
|
||
/// Indicates to close the webview | ||
class CloseWebViewMessageHandler: ScriptMessageHandler<VoidPayload> { | ||
init(analyticsClient: ComponentAnalyticsClient, | ||
didReceiveMessage: @escaping (VoidPayload) -> Void) { | ||
super.init(name: "closeWebView", | ||
analyticsClient: analyticsClient, | ||
didReceiveMessage: didReceiveMessage) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...nect/StripeConnect/Source/Internal/Webview/MessageSenders/MobileInputReceivedSender.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// MobileInputReceivedSender.swift | ||
// StripeConnect | ||
// | ||
// Created by Chris Mays on 2/12/25. | ||
// | ||
|
||
enum Input: String, Equatable, Codable { | ||
case close = "closeButtonPressed" | ||
} | ||
|
||
struct MobileInputReceivedSender: MessageSender { | ||
struct Payload: Codable, Equatable { | ||
var input: Input | ||
} | ||
let name: String = "mobileInputReceived" | ||
let payload: Payload = .init(input: .close) | ||
} |
29 changes: 29 additions & 0 deletions
29
...StripeConnectTests/Internal/Webview/MessageHandlers/CloseWebViewMessageHandlerTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// CloseWebViewMessageHandlerTests.swift | ||
// StripeConnect | ||
// | ||
// Created by Chris Mays on 2/12/25. | ||
// | ||
|
||
@_spi(PrivateBetaConnect) @testable import StripeConnect | ||
import XCTest | ||
|
||
class CloseWebViewMessageHandlerTests: ScriptWebTestBase { | ||
|
||
@MainActor | ||
func testMessageSend() async throws { | ||
let expectation = self.expectation(description: "Message received") | ||
|
||
let messageHandler = CloseWebViewMessageHandler(analyticsClient: MockComponentAnalyticsClient(commonFields: .mock), didReceiveMessage: { _ in | ||
expectation.fulfill() | ||
}) | ||
|
||
webView.addMessageHandler(messageHandler: messageHandler) | ||
|
||
try await webView.evaluateMessage(name: "closeWebView", | ||
json: """ | ||
{} | ||
""") | ||
await fulfillment(of: [expectation], timeout: TestHelpers.defaultTimeout) | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...t/StripeConnectTests/Internal/Webview/MessageSenders/MobileInputReceivedSenderTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// MobileInputReceivedSenderTests.swift | ||
// StripeConnect | ||
// | ||
// Created by Chris Mays on 2/12/25. | ||
// | ||
|
||
// | ||
// ReturnedFromAuthenticatedWebViewSenderTests.swift | ||
// StripeConnectTests | ||
// | ||
// Created by Chris Mays on 8/14/24. | ||
// | ||
|
||
import Foundation | ||
@testable import StripeConnect | ||
import XCTest | ||
|
||
class MobileInputReceivedSenderTests: ScriptWebTestBase { | ||
func testSendMessage() throws { | ||
try validateMessageSent(sender: MobileInputReceivedSender()) | ||
} | ||
|
||
func testSenderSignature() { | ||
XCTAssertEqual( | ||
try MobileInputReceivedSender().javascriptMessage(), | ||
""" | ||
window.mobileInputReceived({"input":"closeButtonPressed"}); | ||
""" | ||
) | ||
} | ||
} |