Skip to content

Commit ec9bfc8

Browse files
committed
🧪 Added tests
1 parent 1385c42 commit ec9bfc8

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

script.sh

100644100755
File mode changed.

tests/offline-events-tests/RequestHandlerTests.swift

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,82 @@ class RequestHandlerTests: XCTestCase {
778778
NetworkHelper.isNetworkLoggingEnabled = false
779779
}
780780

781+
func testNetworkLoggingActualLogs() throws {
782+
// 1. Setup Mock Log Delegate
783+
class MockLogDelegate: NSObject, IterableLogDelegate {
784+
var loggedMessages: [String] = []
785+
func log(level: LogLevel, message: String) {
786+
loggedMessages.append(message)
787+
}
788+
}
789+
let mockLogDelegate = MockLogDelegate()
790+
IterableLogUtil.sharedInstance = IterableLogUtil(dateProvider: SystemDateProvider(), logDelegate: mockLogDelegate)
791+
792+
// 2. Enable Network Logging
793+
NetworkHelper.isNetworkLoggingEnabled = true
794+
795+
// 3. Perform Request (Success)
796+
let expectation1 = expectation(description: "Request success")
797+
// Create a dummy JSON object to be returned as Data
798+
let successData = try! JSONSerialization.data(withJSONObject: ["msg": "success"], options: [])
799+
let networkSession = MockNetworkSession(statusCode: 200, data: successData)
800+
801+
// Need to set up request callback to fulfill expectation when request completes
802+
networkSession.requestCallback = { _ in
803+
expectation1.fulfill()
804+
}
805+
806+
let requestHandler = createRequestHandler(networkSession: networkSession, notificationCenter: MockNotificationCenter(), selectOffline: false)
807+
808+
requestHandler.track(event: "testEvent", dataFields: nil, onSuccess: nil, onFailure: nil)
809+
810+
wait(for: [expectation1], timeout: testExpectationTimeout)
811+
812+
// Wait a little for async logging dispatch
813+
let loggingExpectation = expectation(description: "Logging wait")
814+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
815+
loggingExpectation.fulfill()
816+
}
817+
wait(for: [loggingExpectation], timeout: 1.0)
818+
819+
// 4. Verify Logs for Success
820+
// "sending request" and "successfully sent"
821+
let successLogs = mockLogDelegate.loggedMessages.filter { $0.contains("sending request") || $0.contains("successfully sent") }
822+
XCTAssertTrue(successLogs.count >= 2, "Should have logged request sending and success. Found: \(mockLogDelegate.loggedMessages)")
823+
824+
// Clear logs for next test
825+
mockLogDelegate.loggedMessages = []
826+
827+
// 5. Perform Request (Failure/Retry)
828+
let expectation2 = expectation(description: "Request failure")
829+
// 500 status code triggers retry logic in NetworkHelper
830+
let failureSession = MockNetworkSession(statusCode: 500, data: nil)
831+
832+
// We expect it to fail eventually after retries
833+
// The MockNetworkSession doesn't automatically retry, the NetworkHelper logic does.
834+
// We need to wait for the final failure callback.
835+
836+
let requestHandlerFailure = createRequestHandler(networkSession: failureSession, notificationCenter: MockNotificationCenter(), selectOffline: false)
837+
838+
requestHandlerFailure.track(event: "testEventFail", dataFields: nil, onSuccess: nil, onFailure: { _, _ in
839+
expectation2.fulfill()
840+
})
841+
842+
// Wait longer for retries
843+
wait(for: [expectation2], timeout: testExpectationTimeout * 2)
844+
845+
// 6. Verify Logs for Failure
846+
// Should see "retry attempt" and eventually "errored"
847+
let retryLogs = mockLogDelegate.loggedMessages.filter { $0.contains("retry attempt") }
848+
let errorLogs = mockLogDelegate.loggedMessages.filter { $0.contains("errored") }
849+
850+
XCTAssertTrue(retryLogs.count > 0, "Should have logged retry attempts")
851+
XCTAssertTrue(errorLogs.count > 0, "Should have logged final error")
852+
853+
// 7. Cleanup
854+
NetworkHelper.isNetworkLoggingEnabled = false
855+
}
856+
781857
func testCreatedAtSentAtForOffline() throws {
782858
let expectation1 = expectation(description: #function)
783859
let date = Date().addingTimeInterval(-5000)

0 commit comments

Comments
 (0)