Skip to content

Commit

Permalink
Revert "moved to callback to remove flakiness"
Browse files Browse the repository at this point in the history
This reverts commit 9078990.
  • Loading branch information
Shahroz16 committed Sep 13, 2024
1 parent 9078990 commit 6da6c92
Showing 1 changed file with 75 additions and 77 deletions.
152 changes: 75 additions & 77 deletions Tests/MessagingInApp/State/InAppMessageStateTests.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
@testable import CioMessagingInApp
import XCTest

extension InAppMessageManager {
func dispatchAsync(action: InAppMessageAction) async {
await dispatch(action: action).value
}
}

class InAppMessageStateTests: IntegrationTest {
var inAppMessageManager: InAppMessageManager!
private let engineWebMock = EngineWebInstanceMock()
Expand Down Expand Up @@ -29,14 +35,6 @@ class InAppMessageStateTests: IntegrationTest {
super.tearDown()
}

func dispatchAndWait(_ action: InAppMessageAction, timeout: TimeInterval = 3.0) async throws {
let expectation = XCTestExpectation(description: "Action completed: \(action)")
inAppMessageManager.dispatch(action: action) {
expectation.fulfill()
}
await fulfillment(of: [expectation], timeout: timeout)
}

// MARK: - State Tests

func test_initialState_expectDefaultValues() async {
Expand All @@ -52,65 +50,65 @@ class InAppMessageStateTests: IntegrationTest {
XCTAssertTrue(state.shownMessageQueueIds.isEmpty)
}

func test_initialize_expectCorrectStateUpdate() async throws {
try await dispatchAndWait(.initialize(siteId: "testSite", dataCenter: "testDC", environment: .development))
func test_initialize_expectCorrectStateUpdate() async {
await inAppMessageManager.dispatchAsync(action: .initialize(siteId: "testSite", dataCenter: "testDC", environment: .development))

let state = await inAppMessageManager.state
XCTAssertEqual(state.siteId, "testSite")
XCTAssertEqual(state.dataCenter, "testDC")
XCTAssertEqual(state.environment, .development)
}

func test_setUserIdentifier_expectUserIdUpdate() async throws {
try await dispatchAndWait(.setUserIdentifier(user: "testUser"))
func test_setUserIdentifier_expectUserIdUpdate() async {
await inAppMessageManager.dispatchAsync(action: .setUserIdentifier(user: "testUser"))

let state = await inAppMessageManager.state
XCTAssertEqual(state.userId, "testUser")
}

func test_setPageRoute_expectRouteUpdate() async throws {
try await dispatchAndWait(.setPageRoute(route: "testRoute"))
func test_setPageRoute_expectRouteUpdate() async {
await inAppMessageManager.dispatchAsync(action: .setPageRoute(route: "testRoute"))

let state = await inAppMessageManager.state
XCTAssertEqual(state.currentRoute, "testRoute")
}

func test_processMessageQueue_expectMessagesAddedToQueue() async throws {
func test_processMessageQueue_expectMessagesAddedToQueue() async {
let messages = [Message(queueId: "1"), Message(queueId: "2")]
try await dispatchAndWait(.setUserIdentifier(user: .random))
try await dispatchAndWait(.processMessageQueue(messages: messages))
await inAppMessageManager.dispatchAsync(action: .setUserIdentifier(user: .random))
await inAppMessageManager.dispatchAsync(action: .processMessageQueue(messages: messages))

let state = await inAppMessageManager.state
XCTAssertEqual(state.messagesInQueue.count, 2)
XCTAssertTrue(state.messagesInQueue.contains { $0.queueId == "1" })
XCTAssertTrue(state.messagesInQueue.contains { $0.queueId == "2" })
}

func test_displayMessage_expectMessageStateUpdated() async throws {
func test_displayMessage_expectMessageStateUpdated() async {
let message = Message(queueId: "1")

try await dispatchAndWait(.setUserIdentifier(user: .random))
try await dispatchAndWait(.displayMessage(message: message))
await inAppMessageManager.dispatchAsync(action: .setUserIdentifier(user: .random))
await inAppMessageManager.dispatchAsync(action: .displayMessage(message: message))

let state = await inAppMessageManager.state
XCTAssertEqual(state.currentMessageState, .displayed(message: message))
XCTAssertTrue(state.shownMessageQueueIds.contains("1"))
}

func test_dismissMessage_expectMessageStateDismissed() async throws {
func test_dismissMessage_expectMessageStateDismissed() async {
let message = Message(queueId: "1")

try await dispatchAndWait(.setUserIdentifier(user: .random))
try await dispatchAndWait(.dismissMessage(message: message))
await inAppMessageManager.dispatchAsync(action: .setUserIdentifier(user: .random))
await inAppMessageManager.dispatchAsync(action: .dismissMessage(message: message))

let state = await inAppMessageManager.state
XCTAssertEqual(state.currentMessageState, .dismissed(message: message))
}

func test_resetState_expectInitialStateRestored() async throws {
func test_resetState_expectInitialStateRestored() async {
// Setup user
try await dispatchAndWait(.setUserIdentifier(user: .random))
try await dispatchAndWait(.resetState)
await inAppMessageManager.dispatchAsync(action: .setUserIdentifier(user: .random))
await inAppMessageManager.dispatchAsync(action: .resetState)

let state = await inAppMessageManager.state
XCTAssertEqual(state.siteId, "")
Expand All @@ -125,15 +123,15 @@ class InAppMessageStateTests: IntegrationTest {

// MARK: - Message Processing Tests

func test_processMessageQueue_givenMessagePriorities_expectHighestPriorityLoaded() async throws {
func test_processMessageQueue_givenMessagePriorities_expectHighestPriorityLoaded() async {
let messages = [
Message(priority: 2, queueId: "1"),
Message(priority: 1, queueId: "2"),
Message(priority: 3, queueId: "3")
]

try await dispatchAndWait(.setUserIdentifier(user: .random))
try await dispatchAndWait(.processMessageQueue(messages: messages))
await inAppMessageManager.dispatchAsync(action: .setUserIdentifier(user: .random))
await inAppMessageManager.dispatchAsync(action: .processMessageQueue(messages: messages))

let state = await inAppMessageManager.state
XCTAssertEqual(state.messagesInQueue.count, 3)
Expand All @@ -145,28 +143,28 @@ class InAppMessageStateTests: IntegrationTest {
}
}

func test_processMessageQueue_givenDuplicateMessages_expectDuplicatesRemoved() async throws {
func test_processMessageQueue_givenDuplicateMessages_expectDuplicatesRemoved() async {
let messages = [
Message(queueId: "1"),
Message(queueId: "1"),
Message(queueId: "2")
]

try await dispatchAndWait(.setUserIdentifier(user: .random))
try await dispatchAndWait(.processMessageQueue(messages: messages))
await inAppMessageManager.dispatchAsync(action: .setUserIdentifier(user: .random))
await inAppMessageManager.dispatchAsync(action: .processMessageQueue(messages: messages))

let state = await inAppMessageManager.state
XCTAssertEqual(state.messagesInQueue.count, 2)
XCTAssertTrue(state.messagesInQueue.contains { $0.queueId == "1" })
XCTAssertTrue(state.messagesInQueue.contains { $0.queueId == "2" })
}

func test_routeChange_givenMessageWithPageRule_expectMessageStateUpdated() async throws {
func test_routeChange_givenMessageWithPageRule_expectMessageStateUpdated() async {
let message = Message(pageRule: "home", queueId: "1")

try await dispatchAndWait(.setUserIdentifier(user: .random))
try await dispatchAndWait(.processMessageQueue(messages: [message]))
try await dispatchAndWait(.setPageRoute(route: "home"))
await inAppMessageManager.dispatchAsync(action: .setUserIdentifier(user: .random))
await inAppMessageManager.dispatchAsync(action: .processMessageQueue(messages: [message]))
await inAppMessageManager.dispatchAsync(action: .setPageRoute(route: "home"))

var state = await inAppMessageManager.state
XCTAssertEqual(state.currentRoute, "home")
Expand All @@ -178,7 +176,7 @@ class InAppMessageStateTests: IntegrationTest {
}

// Change route to dismiss the message
try await dispatchAndWait(.setPageRoute(route: "profile"))
await inAppMessageManager.dispatchAsync(action: .setPageRoute(route: "profile"))

state = await inAppMessageManager.state
XCTAssertEqual(state.currentRoute, "profile")
Expand All @@ -190,12 +188,12 @@ class InAppMessageStateTests: IntegrationTest {
}
}

func test_embedMessage_expectNoStateChange() async throws {
func test_embedMessage_expectNoStateChange() async {
let message = Message(queueId: "1")
let elementId = "testElementId"

try await dispatchAndWait(.setUserIdentifier(user: .random))
try await dispatchAndWait(.embedMessage(message: message, elementId: elementId))
await inAppMessageManager.dispatchAsync(action: .setUserIdentifier(user: .random))
await inAppMessageManager.dispatchAsync(action: .embedMessage(message: message, elementId: elementId))

let state = await inAppMessageManager.state
XCTAssertEqual(state.currentMessageState, .initial)
Expand All @@ -204,26 +202,26 @@ class InAppMessageStateTests: IntegrationTest {

// MARK: - Engine Action Tests

func test_engineAction_givenTapAction_expectCallbackCalled() async throws {
func test_engineAction_givenTapAction_expectCallbackCalled() async {
let message = Message(queueId: "1")
let route = "testRoute"
let action = "testAction"
let name = "testName"

try await dispatchAndWait(.setUserIdentifier(user: .random))
try await dispatchAndWait(.engineAction(action: .tap(message: message, route: route, name: name, action: action)))
await inAppMessageManager.dispatchAsync(action: .setUserIdentifier(user: .random))
await inAppMessageManager.dispatchAsync(action: .engineAction(action: .tap(message: message, route: route, name: name, action: action)))

XCTAssertTrue(globalEventListener.messageActionTakenCalled)
XCTAssertEqual(globalEventListener.messageActionTakenReceivedArguments?.message.deliveryId, message.gistProperties.campaignId)
XCTAssertEqual(globalEventListener.messageActionTakenReceivedArguments?.actionValue, action)
XCTAssertEqual(globalEventListener.messageActionTakenReceivedArguments?.actionName, name)
}

func test_engineAction_givenMessageLoadingFailed_expectMessageDismissed() async throws {
func test_engineAction_givenMessageLoadingFailed_expectMessageDismissed() async {
let message = Message(queueId: "1")

try await dispatchAndWait(.setUserIdentifier(user: .random))
try await dispatchAndWait(.engineAction(action: .messageLoadingFailed(message: message)))
await inAppMessageManager.dispatchAsync(action: .setUserIdentifier(user: .random))
await inAppMessageManager.dispatchAsync(action: .engineAction(action: .messageLoadingFailed(message: message)))

let state = await inAppMessageManager.state
XCTAssertEqual(state.currentMessageState, .dismissed(message: message))
Expand All @@ -232,15 +230,15 @@ class InAppMessageStateTests: IntegrationTest {
XCTAssertEqual(globalEventListener.errorWithMessageReceivedArguments?.deliveryId, message.gistProperties.campaignId)
}

func test_processMessageQueue_givenDismissedMessage_expectMessageNotDisplayedAgain() async throws {
func test_processMessageQueue_givenDismissedMessage_expectMessageNotDisplayedAgain() async {
let message = Message(queueId: "1")

try await dispatchAndWait(.setUserIdentifier(user: "testUser"))
try await dispatchAndWait(.processMessageQueue(messages: [message]))
try await dispatchAndWait(.displayMessage(message: message))
try await dispatchAndWait(.dismissMessage(message: message))
await inAppMessageManager.dispatchAsync(action: .setUserIdentifier(user: "testUser"))
await inAppMessageManager.dispatchAsync(action: .processMessageQueue(messages: [message]))
await inAppMessageManager.dispatchAsync(action: .displayMessage(message: message))
await inAppMessageManager.dispatchAsync(action: .dismissMessage(message: message))

try await dispatchAndWait(.processMessageQueue(messages: [message]))
await inAppMessageManager.dispatchAsync(action: .processMessageQueue(messages: [message]))

let state = await inAppMessageManager.state
XCTAssertEqual(state.currentMessageState, .dismissed(message: message))
Expand All @@ -252,12 +250,12 @@ class InAppMessageStateTests: IntegrationTest {

// MARK: - Route Change Tests

func test_setPageRoute_givenNoUser_expectRouteUpdatedWithoutProcessingMessages() async throws {
func test_setPageRoute_givenNoUser_expectRouteUpdatedWithoutProcessingMessages() async {
var state = await inAppMessageManager.state
XCTAssertNil(state.userId)
XCTAssertNil(state.currentRoute)

try await dispatchAndWait(.setPageRoute(route: "home"))
await inAppMessageManager.dispatchAsync(action: .setPageRoute(route: "home"))

state = await inAppMessageManager.state
XCTAssertNil(state.userId)
Expand All @@ -271,15 +269,15 @@ class InAppMessageStateTests: IntegrationTest {
XCTAssertFalse(globalEventListener.messageActionTakenCalled)
}

func test_routeChange_expectMessageProcessing() async throws {
func test_routeChange_expectMessageProcessing() async {
let message1 = Message(pageRule: "home", queueId: "1")
let message2 = Message(pageRule: "profile", queueId: "2")

try await dispatchAndWait(.setUserIdentifier(user: .random))
try await dispatchAndWait(.processMessageQueue(messages: [message1, message2]))
await inAppMessageManager.dispatchAsync(action: .setUserIdentifier(user: .random))
await inAppMessageManager.dispatchAsync(action: .processMessageQueue(messages: [message1, message2]))

// Set route to "home"
try await dispatchAndWait(.setPageRoute(route: "home"))
await inAppMessageManager.dispatchAsync(action: .setPageRoute(route: "home"))

var state = await inAppMessageManager.state
XCTAssertEqual(state.currentRoute, "home")
Expand All @@ -291,7 +289,7 @@ class InAppMessageStateTests: IntegrationTest {
}

// Change route to "profile"
try await dispatchAndWait(.setPageRoute(route: "profile"))
await inAppMessageManager.dispatchAsync(action: .setPageRoute(route: "profile"))

state = await inAppMessageManager.state
XCTAssertEqual(state.currentRoute, "profile")
Expand All @@ -303,68 +301,68 @@ class InAppMessageStateTests: IntegrationTest {
}
}

func test_routeChange_givenMessageBeingProcessed_expectMessageHandledCorrectly() async throws {
func test_routeChange_givenMessageBeingProcessed_expectMessageHandledCorrectly() async {
let message = Message(pageRule: "home", queueId: "1")

try await dispatchAndWait(.setUserIdentifier(user: .random))
try await dispatchAndWait(.setPageRoute(route: "home"))
try await dispatchAndWait(.processMessageQueue(messages: [message]))
await inAppMessageManager.dispatchAsync(action: .setUserIdentifier(user: .random))
await inAppMessageManager.dispatchAsync(action: .setPageRoute(route: "home"))
await inAppMessageManager.dispatchAsync(action: .processMessageQueue(messages: [message]))

var state = await inAppMessageManager.state
XCTAssertEqual(state.currentMessageState, .loading(message: message))

try await dispatchAndWait(.setPageRoute(route: "profile"))
await inAppMessageManager.dispatchAsync(action: .setPageRoute(route: "profile"))

state = await inAppMessageManager.state
XCTAssertEqual(state.currentMessageState, .dismissed(message: message))
XCTAssertEqual(state.currentRoute, "profile")

try await dispatchAndWait(.setPageRoute(route: "home"))
await inAppMessageManager.dispatchAsync(action: .setPageRoute(route: "home"))

state = await inAppMessageManager.state
XCTAssertEqual(state.currentMessageState, .loading(message: message))
}

// MARK: - Callback Tests

func test_displayMessage_expectMessageShownCallbackCalled() async throws {
func test_displayMessage_expectMessageShownCallbackCalled() async {
let message = Message(queueId: "1")

try await dispatchAndWait(.setUserIdentifier(user: .random))
try await dispatchAndWait(.displayMessage(message: message))
await inAppMessageManager.dispatchAsync(action: .setUserIdentifier(user: .random))
await inAppMessageManager.dispatchAsync(action: .displayMessage(message: message))

XCTAssertTrue(globalEventListener.messageShownCalled)
XCTAssertEqual(globalEventListener.messageShownReceivedArguments?.deliveryId, message.gistProperties.campaignId)
}

func test_dismissMessage_expectMessageDismissedCallbackCalled() async throws {
func test_dismissMessage_expectMessageDismissedCallbackCalled() async {
let message = Message(queueId: "1")

try await dispatchAndWait(.setUserIdentifier(user: .random))
try await dispatchAndWait(.dismissMessage(message: message))
await inAppMessageManager.dispatchAsync(action: .setUserIdentifier(user: .random))
await inAppMessageManager.dispatchAsync(action: .dismissMessage(message: message))

XCTAssertTrue(globalEventListener.messageDismissedCalled)
XCTAssertEqual(globalEventListener.messageDismissedReceivedArguments?.deliveryId, message.gistProperties.campaignId)
}

func test_engineAction_givenMessageLoadingFailed_expectErrorCallbackCalled() async throws {
func test_engineAction_givenMessageLoadingFailed_expectErrorCallbackCalled() async {
let message = Message(queueId: "1")

try await dispatchAndWait(.setUserIdentifier(user: .random))
try await dispatchAndWait(.engineAction(action: .messageLoadingFailed(message: message)))
await inAppMessageManager.dispatchAsync(action: .setUserIdentifier(user: .random))
await inAppMessageManager.dispatchAsync(action: .engineAction(action: .messageLoadingFailed(message: message)))

XCTAssertTrue(globalEventListener.errorWithMessageCalled)
XCTAssertEqual(globalEventListener.errorWithMessageReceivedArguments?.deliveryId, message.gistProperties.campaignId)
}

func test_engineAction_givenTapAction_expectMessageActionTakenCallbackCalled() async throws {
func test_engineAction_givenTapAction_expectMessageActionTakenCallbackCalled() async {
let message = Message(queueId: "1")
let route = "testRoute"
let action = "testAction"
let name = "testName"

try await dispatchAndWait(.setUserIdentifier(user: .random))
try await dispatchAndWait(.engineAction(action: .tap(message: message, route: route, name: name, action: action)))
await inAppMessageManager.dispatchAsync(action: .setUserIdentifier(user: .random))
await inAppMessageManager.dispatchAsync(action: .engineAction(action: .tap(message: message, route: route, name: name, action: action)))

XCTAssertTrue(globalEventListener.messageActionTakenCalled)
XCTAssertEqual(globalEventListener.messageActionTakenReceivedArguments?.message.deliveryId, message.gistProperties.campaignId)
Expand Down

0 comments on commit 6da6c92

Please sign in to comment.