Skip to content

Commit

Permalink
chore: dismiss message when close button pressed (#734)
Browse files Browse the repository at this point in the history
  • Loading branch information
levibostian authored Jun 28, 2024
1 parent 40fae32 commit edcd776
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Foundation
// Callbacks specific to inline message events.
protocol InlineMessageManagerDelegate: AnyObject {
func sizeChanged(width: CGFloat, height: CGFloat)
func onCloseAction()
}

/**
Expand Down Expand Up @@ -38,7 +39,7 @@ class InlineMessageManager: MessageManager {
}

override func onCloseAction() {
// Not yet implemented. Planned in future update.
inlineMessageDelegate?.onCloseAction()
}
}

Expand Down
1 change: 1 addition & 0 deletions Sources/MessagingInApp/Gist/Managers/MessageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class MessageManager {
// This means that the message begins the process of loading.
// Start a timer that helps us determine how long a message took to load/render.
elapsedTimer.start(title: "Loading message with id: \(currentMessage.templateId)")

self.engine = engineWebProvider.getEngineWebInstance(configuration: engineWebConfiguration)
engine.delegate = self
self.gistView = GistView(message: currentMessage, engineView: engine.view)
Expand Down
6 changes: 6 additions & 0 deletions Sources/MessagingInApp/Views/InAppMessageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,10 @@ extension InAppMessageView: InlineMessageManagerDelegate {
self.animateHeight(to: height)
}
}

func onCloseAction() {
Task { @MainActor in
self.dismissInAppMessage()
}
}
}
42 changes: 42 additions & 0 deletions Tests/MessagingInApp/Views/InAppMessageViewTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,39 @@ class InAppMessageViewTest: UnitTest {
XCTAssertFalse(isDisplayingInAppMessage(inlineView)) // expect ignore new message, stay dismissed.
}

// MARK: close action button

@MainActor
func test_onCloseAction_givenCloseActionClickedOnInAppMessage_expectDismissInlineView() async {
let givenInlineMessage = Message.randomInline
queueMock.getInlineMessagesReturnValue = [givenInlineMessage]

let inlineView = InAppMessageView(elementId: givenInlineMessage.elementId!)
await onDoneRenderingInAppMessage(givenInlineMessage)
XCTAssertTrue(isDisplayingInAppMessage(inlineView))

await onCloseActionButtonPressed()

XCTAssertFalse(isDisplayingInAppMessage(inlineView))
}

// Once an in-app message has been closed it will not be replaced with another message.
// We plan to change this behavior in the future. Test function can be modified to match the new behavior at that time.
@MainActor
func test_onCloseAction_givenMessageClosed_givenNewMessageFetched_expectIgnoreMessage() async {
let givenMessageThatGetsClosed = Message.randomInline
queueMock.getInlineMessagesReturnValue = [givenMessageThatGetsClosed]

let inlineView = InAppMessageView(elementId: givenMessageThatGetsClosed.elementId!)
await onDoneRenderingInAppMessage(givenMessageThatGetsClosed)
XCTAssertTrue(isDisplayingInAppMessage(inlineView))
await onCloseActionButtonPressed()
XCTAssertFalse(isDisplayingInAppMessage(inlineView))

await simulateSdkFetchedMessages([Message.randomInline]) // simulate new message fetched
XCTAssertFalse(isDisplayingInAppMessage(inlineView)) // expect ignore new message, stay dismissed.
}

// MARK: height and width constriants

// When the View is constructed, the SDK will add a constraint or it will modify the existing height constraint.
Expand Down Expand Up @@ -291,6 +324,15 @@ extension InAppMessageViewTest {
await waitForMainThreadToFinishPendingTasks()
}

func onCloseActionButtonPressed() async {
// Triggering the close button from the web engine simulates the user tapping the close button on the in-app WebView.
// This behaves more like an integration test because we are also able to test the message manager, too.
engineWebMock.delegate?.tap(name: "", action: GistMessageActions.close.rawValue, system: false)

// When onCloseAction() is called on the inline View, it adds a task to the main thread queue. Our test wants to wait until this task is done running.
await waitForMainThreadToFinishPendingTasks()
}

func isDisplayingInAppMessage(_ view: InAppMessageView) -> Bool {
guard let viewHeightConstraint = view.heightConstraint else {
return false
Expand Down

0 comments on commit edcd776

Please sign in to comment.