Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Nov 12, 2024
1 parent 6f656bc commit ef2e552
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
7 changes: 5 additions & 2 deletions posthog/src/main/java/com/posthog/PostHog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,8 @@ public class PostHog private constructor(
config?.logger?.log("identify called with invalid anonymousId: $anonymousId.")
}

if (previousDistinctId != distinctId && !isIdentified) {
val hasDifferentDistinctId = previousDistinctId != distinctId
if (hasDifferentDistinctId && !isIdentified) {
// this has to be set before capture since this flag will be read during the event
// capture
synchronized(identifiedLock) {
Expand All @@ -583,7 +584,9 @@ public class PostHog private constructor(
if (reloadFeatureFlags) {
reloadFeatureFlags()
}
} else if (userProperties?.isNotEmpty() == true || userPropertiesSetOnce?.isNotEmpty() == true) {
// we need to make sure the user props update is for the same user
// otherwise they have to reset and identify again
} else if (!hasDifferentDistinctId && (userProperties?.isNotEmpty() == true || userPropertiesSetOnce?.isNotEmpty() == true)) {
capture(
"\$set",
distinctId = distinctId,
Expand Down
33 changes: 32 additions & 1 deletion posthog/src/test/java/com/posthog/PostHogTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -574,13 +574,44 @@ internal class PostHogTest {

sut.identify(
"anotherDistinctId",
)

queueExecutor.shutdownAndAwaitTermination()

assertEquals(1, http.requestCount)

sut.close()
}

@Test
fun `captures a set event if identified`() {
val http = mockHttp()
val url = http.url("/")

val sut = getSut(url.toString(), preloadFeatureFlags = false, reloadFeatureFlags = false, flushAt = 2)

sut.identify(
DISTINCT_ID,
userProperties = userProps,
userPropertiesSetOnce = userPropsOnce,
)

sut.identify(
DISTINCT_ID,
userProperties = userProps,
userPropertiesSetOnce = userPropsOnce,
)

queueExecutor.shutdownAndAwaitTermination()

assertEquals(1, http.requestCount)
val request = http.takeRequest()

val content = request.body.unGzip()
val batch = serializer.deserialize<PostHogBatchEvent>(content.reader())

val theEvent = batch.batch.last()

assertEquals("\$set", theEvent.event)

sut.close()
}
Expand Down

0 comments on commit ef2e552

Please sign in to comment.