Skip to content

Commit

Permalink
Fix posthog tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BillCarsonFr committed May 6, 2024
1 parent 69bb98f commit d100b62
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

package im.vector.app.features.analytics.impl

import com.posthog.android.Properties
import im.vector.app.features.analytics.itf.VectorAnalyticsEvent
import im.vector.app.features.analytics.itf.VectorAnalyticsScreen
import im.vector.app.test.fakes.FakeAnalyticsStore
import im.vector.app.test.fakes.FakeLateInitUserPropertiesFactory
import im.vector.app.test.fakes.FakePostHog
Expand Down Expand Up @@ -128,7 +125,7 @@ class DefaultVectorAnalyticsTest {

defaultVectorAnalytics.screen(A_SCREEN_EVENT)

fakePostHog.verifyScreenTracked(A_SCREEN_EVENT.getName(), A_SCREEN_EVENT.toPostHogProperties())
fakePostHog.verifyScreenTracked(A_SCREEN_EVENT.getName(), A_SCREEN_EVENT.getProperties())
}

@Test
Expand All @@ -146,7 +143,7 @@ class DefaultVectorAnalyticsTest {

defaultVectorAnalytics.capture(AN_EVENT)

fakePostHog.verifyEventTracked(AN_EVENT.getName(), AN_EVENT.toPostHogProperties())
fakePostHog.verifyEventTracked(AN_EVENT.getName(), AN_EVENT.getProperties().clearNulls())
}

@Test
Expand Down Expand Up @@ -176,16 +173,16 @@ class DefaultVectorAnalyticsTest {

fakeSentryAnalytics.verifyNoErrorTracking()
}
}

private fun VectorAnalyticsScreen.toPostHogProperties(): Properties? {
return getProperties()?.let { properties ->
Properties().also { it.putAll(properties) }
}
}
private fun Map<String, Any?>?.clearNulls(): Map<String, Any>? {
if (this == null) return null

private fun VectorAnalyticsEvent.toPostHogProperties(): Properties? {
return getProperties()?.let { properties ->
Properties().also { it.putAll(properties) }
val nonNulls = HashMap<String, Any>()
this.forEach { (key, value) ->
if (value != null) {
nonNulls[key] = value
}
}
return nonNulls
}
}
19 changes: 10 additions & 9 deletions vector/src/test/java/im/vector/app/test/fakes/FakePostHog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
package im.vector.app.test.fakes

import android.os.Looper
import com.posthog.android.PostHog
import com.posthog.android.Properties
import com.posthog.PostHogInterface
import im.vector.app.features.analytics.plan.UserProperties
import io.mockk.every
import io.mockk.mockk
Expand All @@ -36,16 +35,19 @@ class FakePostHog {
every { Looper.getMainLooper() } returns looper
}

val instance = mockk<PostHog>(relaxed = true)
val instance = mockk<PostHogInterface>(relaxed = true)

fun verifyOptOutStatus(optedOut: Boolean) {
verify { instance.optOut(optedOut) }
if (optedOut) {
verify { instance.optOut() }
} else {
verify { instance.optIn() }
}
}

fun verifyIdentifies(analyticsId: String, userProperties: UserProperties?) {
verify {
val postHogProperties = userProperties?.getProperties()
?.let { rawProperties -> Properties().also { it.putAll(rawProperties) } }
?.takeIf { it.isNotEmpty() }
instance.identify(analyticsId, postHogProperties, null)
}
Expand All @@ -55,20 +57,19 @@ class FakePostHog {
verify { instance.reset() }
}

fun verifyScreenTracked(name: String, properties: Properties?) {
fun verifyScreenTracked(name: String, properties: Map<String, Any>?) {
verify { instance.screen(name, properties) }
}

fun verifyNoScreenTracking() {
verify(exactly = 0) {
instance.screen(any())
instance.screen(any(), any())
instance.screen(any(), any(), any())
}
}

fun verifyEventTracked(name: String, properties: Properties?) {
verify { instance.capture(name, properties) }
fun verifyEventTracked(name: String, properties: Map<String, Any>?) {
verify { instance.capture(name, null, properties) }
}

fun verifyNoEventTracking() {
Expand Down

0 comments on commit d100b62

Please sign in to comment.