diff --git a/Source/BridgeComponent.swift b/Source/BridgeComponent.swift index 148242d..c73fd3d 100644 --- a/Source/BridgeComponent.swift +++ b/Source/BridgeComponent.swift @@ -37,13 +37,7 @@ open class BridgeComponent: BridgingComponent { /// - Parameter message: The message to be replied with. /// - Returns: `true` if the reply was successful, `false` if the bridge is not available. public func reply(with message: Message) -> Bool { - guard let bridge = delegate.bridge else { - logger.warning("bridgeMessageFailedToReply: bridge is not available") - return false - } - - bridge.reply(with: message) - return true + return delegate.reply(with: message) } @discardableResult diff --git a/Source/BridgeDelegate.swift b/Source/BridgeDelegate.swift index 8f60b84..60d5b6b 100644 --- a/Source/BridgeDelegate.swift +++ b/Source/BridgeDelegate.swift @@ -34,6 +34,21 @@ public final class BridgeDelegate { bridge = nil } + @discardableResult + /// Replies to the web with a received message, optionally replacing its `event` or `jsonData`. + /// + /// - Parameter message: The message to be replied with. + /// - Returns: `true` if the reply was successful, `false` if the bridge is not available. + public func reply(with message: Message) -> Bool { + guard let bridge else { + logger.warning("bridgeMessageFailedToReply: bridge is not available") + return false + } + + bridge.reply(with: message) + return true + } + // MARK: - Destination lifecycle public func onViewDidLoad() { diff --git a/Tests/BridgeDelegateTests.swift b/Tests/BridgeDelegateTests.swift index 3612f61..b16940f 100644 --- a/Tests/BridgeDelegateTests.swift +++ b/Tests/BridgeDelegateTests.swift @@ -141,6 +141,28 @@ class BridgeDelegateTests: XCTestCase { XCTAssertTrue(delegate.bridgeDidReceiveMessage(testMessage())) } + // MARK: reply(with:) + + func test_replyWithSucceedsWhenBridgeIsSet() { + let message = testMessage() + let success = delegate.reply(with: message) + + XCTAssertTrue(success) + XCTAssertTrue(bridge.replyWithMessageWasCalled) + XCTAssertEqual(bridge.replyWithMessageArg, message) + } + + func test_replyWithFailsWhenBridgeNotSet() { + delegate.bridge = nil + + let message = testMessage() + let success = delegate.reply(with: message) + + XCTAssertFalse(success) + XCTAssertFalse(bridge.replyWithMessageWasCalled) + XCTAssertNil(bridge.replyWithMessageArg) + } + private func testMessage() -> Message { return Message(id: "1", component: "two",