Skip to content

Commit

Permalink
adds unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Greer committed Feb 14, 2025
1 parent 0b238d8 commit eeef2bc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
6 changes: 3 additions & 3 deletions tests/unit-tests/IterableApiCriteriaFetchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class IterableApiCriteriaFetchTests: XCTestCase {

let config = IterableConfig()
config.enableAnonActivation = true
config.enableOnForegroundCriteriaFetching = true
config.enableForegroundCriteriaFetch = true

IterableAPI.initializeForTesting(apiKey: IterableApiCriteriaFetchTests.apiKey,
config: config,
Expand Down Expand Up @@ -85,7 +85,7 @@ class IterableApiCriteriaFetchTests: XCTestCase {

let config = IterableConfig()
config.enableAnonActivation = true
config.enableOnForegroundCriteriaFetching = false
config.enableForegroundCriteriaFetch = false

internalApi = InternalIterableAPI.initializeForTesting(
config: config,
Expand Down Expand Up @@ -124,7 +124,7 @@ class IterableApiCriteriaFetchTests: XCTestCase {

let config = IterableConfig()
config.enableAnonActivation = true
config.enableOnForegroundCriteriaFetching = true
config.enableForegroundCriteriaFetch = true

IterableAPI.initializeForTesting(apiKey: IterableApiCriteriaFetchTests.apiKey,
config: config,
Expand Down
43 changes: 43 additions & 0 deletions tests/unit-tests/UserMergeScenariosTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,49 @@ class UserMergeScenariosTests: XCTestCase, AuthProvider {

waitForExpectations(timeout: 5, handler: nil)
}

func testCriteriaMetTwice() {
let config = IterableConfig()
config.enableAnonActivation = true

let mockSession = MockNetworkSession()

IterableAPI.initializeForTesting(apiKey: UserMergeScenariosTests.apiKey,
config: config,
networkSession: mockSession,
localStorage: localStorage)

IterableAPI.logoutUser()
guard let jsonData = mockData.data(using: .utf8) else { return }
localStorage.criteriaData = jsonData

IterableAPI.track(event: "testEvent")
IterableAPI.track(event: "testEvent")

waitForDuration(seconds: 3)

if let anonUser = localStorage.userIdAnnon {
XCTAssertFalse(anonUser.isEmpty, "Expected anon user nil")
} else {
XCTFail("Expected anon user nil but found")
}

// Verify that anon session request was made exactly once
let anonSessionRequest = mockSession.getRequest(withEndPoint: Const.Path.trackAnonSession)
XCTAssertNotNil(anonSessionRequest, "Anonymous session request should not be nil")

// Count total requests with anon session endpoint
let anonSessionRequests = mockSession.requests.filter { request in
request.url?.absoluteString.contains(Const.Path.trackAnonSession) == true
}
XCTAssertEqual(anonSessionRequests.count, 1, "Anonymous session should be called exactly once")

// Verify track events were made
let trackRequests = mockSession.requests.filter { request in
request.url?.absoluteString.contains(Const.Path.trackEvent) == true
}
XCTAssertEqual(trackRequests.count, 2, "Track event should be called twice")
}
}


0 comments on commit eeef2bc

Please sign in to comment.