Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Oct 2, 2023
1 parent d23802a commit 12ddcdc
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion posthog/src/main/java/com/posthog/PostHog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public class PostHog private constructor(
this.anonymousId = previousDistinctId
this.distinctId = distinctId

// only because of testing, this flag is always enabled
// only because of testing in isolation, this flag is always enabled
if (reloadFeatureFlags) {
reloadFeatureFlags()
}
Expand Down
94 changes: 94 additions & 0 deletions posthog/src/test/java/com/posthog/PostHogTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ internal class PostHogTest {
optOut: Boolean = false,
preloadFeatureFlags: Boolean = true,
reloadFeatureFlags: Boolean = true,
sendFeatureFlagEvent: Boolean = true,
integration: PostHogIntegration? = null,
): PostHogInterface {
config = PostHogConfig(apiKey, host).apply {
Expand All @@ -45,6 +46,7 @@ internal class PostHogTest {
if (integration != null) {
addIntegration(integration)
}
this.sendFeatureFlagEvent = sendFeatureFlagEvent
}
return PostHog.withInternal(config, queueExecutor, featureFlagsExecutor, cachedEventsExecutor, reloadFeatureFlags)
}
Expand Down Expand Up @@ -186,6 +188,75 @@ internal class PostHogTest {
sut.close()
}

@Test
fun `getFeatureFlag returns the value after reloaded`() {
val http = mockHttp(
response =
MockResponse()
.setBody(responseDecideApi),
)
val url = http.url("/")

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

sut.reloadFeatureFlags()

featureFlagsExecutor.shutdownAndAwaitTermination()

assertTrue(sut.getFeatureFlag("4535-funnel-bar-viz") as Boolean)

sut.close()
}

@Test
fun `getFeatureFlag captures feature flag event if enabled`() {
val http = mockHttp(
response =
MockResponse()
.setBody(responseDecideApi),
)
http.enqueue(
MockResponse()
.setBody(""),
)
val url = http.url("/")

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

sut.reloadFeatureFlags()

featureFlagsExecutor.shutdownAndAwaitTermination()

// remove from the http queue
http.takeRequest()

assertTrue(sut.getFeatureFlag("4535-funnel-bar-viz") as Boolean)

queueExecutor.shutdownAndAwaitTermination()

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

val theEvent = batch.batch.first()
assertEquals("\$feature_flag_called", theEvent.event)
assertNotNull(theEvent.distinctId)
assertNotNull(theEvent.timestamp)
assertNotNull(theEvent.uuid)

// {$feature/4535-funnel-bar-viz=true, $active_feature_flags=[4535-funnel-bar-viz], $feature_flag=4535-funnel-bar-viz, $feature_flag_response=true}
assertEquals(true, theEvent.properties!!["\$feature/4535-funnel-bar-viz"])

@Suppress("UNCHECKED_CAST")
val theFlags = theEvent.properties!!["\$active_feature_flags"] as List<String>
assertEquals("4535-funnel-bar-viz", theFlags[0])

assertEquals("4535-funnel-bar-viz", theEvent.properties!!["\$feature_flag"])
assertEquals(true, theEvent.properties!!["\$feature_flag_response"])

sut.close()
}

@Test
fun `getFeatureFlagPayload returns the value after reloaded`() {
val http = mockHttp(
Expand Down Expand Up @@ -547,4 +618,27 @@ internal class PostHogTest {

sut.close()
}

@Test
fun `close not capture events after closing`() {
val http = mockHttp()
val url = http.url("/")

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

sut.close()

sut.capture(
event,
distinctId,
props,
userProperties = userProps,
userPropertiesSetOnce = userPropsOnce,
groupProperties = groupProps,
)

queueExecutor.shutdownAndAwaitTermination()

assertEquals(0, http.requestCount)
}
}

0 comments on commit 12ddcdc

Please sign in to comment.