Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zihejia committed Jun 27, 2022
1 parent 7c6d58e commit 71c4db0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
36 changes: 36 additions & 0 deletions MixpanelDemo/MixpanelDemoTests/MixpanelDemoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,42 @@ class MixpanelDemoTests: MixpanelBaseTests {
}
removeDBfile(testMixpanel.apiToken)
}

func testCreateAlias() {
let testMixpanel = Mixpanel.initialize(token: randomId(), flushInterval: 60)
testMixpanel.people.set(properties: ["p1": "a"])
waitForTrackingQueue(testMixpanel)
var unidentifiedQueue = unIdentifiedPeopleQueue(token: testMixpanel.apiToken)
// the user profile update has been queued up in unidentifiedQueue until identify is called
XCTAssertTrue(!unidentifiedQueue.isEmpty)

let distinctId = testMixpanel.distinctId
let alias: String = "a1"
testMixpanel.createAlias(alias, distinctId: testMixpanel.distinctId)
waitForTrackingQueue(testMixpanel)

let mixpanelIdentity = MixpanelPersistence.loadIdentity(apiToken: testMixpanel.apiToken)
XCTAssertTrue(distinctId == mixpanelIdentity.distinctID && distinctId == mixpanelIdentity.peopleDistinctID && distinctId == mixpanelIdentity.userId && alias == mixpanelIdentity.alias)
removeDBfile(testMixpanel.apiToken)
unidentifiedQueue = unIdentifiedPeopleQueue(token: testMixpanel.apiToken)
// unidentifiedQueue has been flushed
XCTAssertTrue(unidentifiedQueue.isEmpty)

let testMixpanel2 = Mixpanel.initialize(token: randomId(), flushInterval: 60)
testMixpanel2.people.set(properties: ["p1": "a"])
waitForTrackingQueue(testMixpanel2)

let distinctId2 = testMixpanel2.distinctId
testMixpanel2.createAlias(alias, distinctId: testMixpanel.distinctId, andIdentify: false)
waitForTrackingQueue(testMixpanel2)

let unidentifiedQueue2 = unIdentifiedPeopleQueue(token: testMixpanel2.apiToken)
// The user profile updates should still be held in unidentifiedQueue cause no identify is called
XCTAssertTrue(!unidentifiedQueue2.isEmpty)
let mixpanelIdentity2 = MixpanelPersistence.loadIdentity(apiToken: testMixpanel2.apiToken)
XCTAssertTrue(distinctId2 == mixpanelIdentity2.distinctID && nil == mixpanelIdentity2.peopleDistinctID && nil == mixpanelIdentity2.userId && alias == mixpanelIdentity2.alias)
removeDBfile(testMixpanel2.apiToken)
}

func testPersistentIdentity() {
let testMixpanel = Mixpanel.initialize(token: randomId(), flushInterval: 60)
Expand Down
6 changes: 6 additions & 0 deletions Sources/MixpanelInstance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,12 @@ extension MixpanelInstance {
self.hadPersistedDistinctId = true
}

if self.userId == nil {
self.readWriteLock.write {
self.userId = distinctId
}
}

if distinctId != self.distinctId {
let oldDistinctId = self.distinctId
self.readWriteLock.write {
Expand Down

0 comments on commit 71c4db0

Please sign in to comment.