Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: added tests for messaging inapp implementation #812

Merged
merged 20 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,73 +1,73 @@
@testable import CioInternalCommon
@testable import CioMessagingInApp
import SharedTests
import XCTest

class MessageQueueManagerIntegrationTests: IntegrationTest {
var manager: MessageQueueManager!

var sampleFetchResponseBody: String {
readSampleDataFile(subdirectory: "InAppUserQueue", fileName: "fetch_response.json")
}

override func setUp() {
super.setUp()

initializeManager()

UserManager().setUserToken(userToken: .random) // Set a user token so manager can perform user queue fetches.
}

// MARK: fetch user messages from backend services

func test_fetch_givenHTTPResponse200_expectSetLocalMessageStoreFromFetchResponse() {
XCTAssertTrue(manager.localMessageStore.isEmpty)

setupHttpResponse(code: 200, body: sampleFetchResponseBody.data)
manager.fetchUserMessages()

XCTAssertEqual(manager.localMessageStore.count, 2)
}

func test_fetch_givenMessageCacheSaved_given304AfterSdkInitialized_expectPopulateLocalMessageStoreFromCache() {
XCTAssertTrue(manager.localMessageStore.isEmpty)

setupHttpResponse(code: 200, body: sampleFetchResponseBody.data)
manager.fetchUserMessages()
XCTAssertEqual(manager.localMessageStore.count, 2)

let localMessageStoreBefore304: [Message] = manager.localMessageStore.values.compactMap { $0 }

initializeManager()
XCTAssertTrue(manager.localMessageStore.isEmpty)

setupHttpResponse(code: 304, body: "".data)
manager.fetchUserMessages()

XCTAssertEqual(manager.localMessageStore.count, 2)
let localMessageStoreAfter304 = manager.localMessageStore.values

localMessageStoreBefore304.forEach { message in
XCTAssertTrue(localMessageStoreAfter304.contains(message))
}
}

// The SDK could receive a 304 and the SDK does not have a previous fetch response cached. Example use cases when this could happen:
// 1. The user logs out of the SDK and logs in again with same or different profile.
// 2. Reinstalls the app and first fetch response is a 304
func test_fetch_givenNoPreviousCacheSaved_given304AfterSdkInitialized_expectPopulateLocalMessageStoreFromCache() {
XCTAssertTrue(manager.localMessageStore.isEmpty)

setupHttpResponse(code: 304, body: "".data)
manager.fetchUserMessages()

XCTAssertTrue(manager.localMessageStore.isEmpty)
}
}

extension MessageQueueManagerIntegrationTests {
// Convenient function for test functions that need to test when a new instance of manager is created (clearing in-memory stores).
func initializeManager() {
manager = MessageQueueManager()
}
}
// @testable import CioInternalCommon
// @testable import CioMessagingInApp
// import SharedTests
// import XCTest
//
// class MessageQueueManagerIntegrationTests: IntegrationTest {
// var manager: MessageQueueManager!
//
// var sampleFetchResponseBody: String {
// readSampleDataFile(subdirectory: "InAppUserQueue", fileName: "fetch_response.json")
// }
//
// override func setUp() {
// super.setUp()
//
// initializeManager()
//
// UserManager().setUserToken(userToken: .random) // Set a user token so manager can perform user queue fetches.
// }
//
// // MARK: fetch user messages from backend services
//
// func test_fetch_givenHTTPResponse200_expectSetLocalMessageStoreFromFetchResponse() {
// XCTAssertTrue(manager.localMessageStore.isEmpty)
//
// setupHttpResponse(code: 200, body: sampleFetchResponseBody.data)
// manager.fetchUserMessages()
//
// XCTAssertEqual(manager.localMessageStore.count, 2)
// }
//
// func test_fetch_givenMessageCacheSaved_given304AfterSdkInitialized_expectPopulateLocalMessageStoreFromCache() {
// XCTAssertTrue(manager.localMessageStore.isEmpty)
//
// setupHttpResponse(code: 200, body: sampleFetchResponseBody.data)
// manager.fetchUserMessages()
// XCTAssertEqual(manager.localMessageStore.count, 2)
//
// let localMessageStoreBefore304: [Message] = manager.localMessageStore.values.compactMap { $0 }
//
// initializeManager()
// XCTAssertTrue(manager.localMessageStore.isEmpty)
//
// setupHttpResponse(code: 304, body: "".data)
// manager.fetchUserMessages()
//
// XCTAssertEqual(manager.localMessageStore.count, 2)
// let localMessageStoreAfter304 = manager.localMessageStore.values
//
// localMessageStoreBefore304.forEach { message in
// XCTAssertTrue(localMessageStoreAfter304.contains(message))
// }
// }
//
// // The SDK could receive a 304 and the SDK does not have a previous fetch response cached. Example use cases when this could happen:
// // 1. The user logs out of the SDK and logs in again with same or different profile.
// // 2. Reinstalls the app and first fetch response is a 304
// func test_fetch_givenNoPreviousCacheSaved_given304AfterSdkInitialized_expectPopulateLocalMessageStoreFromCache() {
// XCTAssertTrue(manager.localMessageStore.isEmpty)
//
// setupHttpResponse(code: 304, body: "".data)
// manager.fetchUserMessages()
//
// XCTAssertTrue(manager.localMessageStore.isEmpty)
// }
// }
//
// extension MessageQueueManagerIntegrationTests {
// // Convenient function for test functions that need to test when a new instance of manager is created (clearing in-memory stores).
// func initializeManager() {
// manager = MessageQueueManager()
// }
// }
Loading
Loading