Fix posthog tests

This commit is contained in:
Valere 2024-05-06 10:31:45 +02:00
parent 69bb98f29d
commit d100b62e54
2 changed files with 22 additions and 24 deletions

View file

@ -16,9 +16,6 @@
package im.vector.app.features.analytics.impl 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.FakeAnalyticsStore
import im.vector.app.test.fakes.FakeLateInitUserPropertiesFactory import im.vector.app.test.fakes.FakeLateInitUserPropertiesFactory
import im.vector.app.test.fakes.FakePostHog import im.vector.app.test.fakes.FakePostHog
@ -128,7 +125,7 @@ class DefaultVectorAnalyticsTest {
defaultVectorAnalytics.screen(A_SCREEN_EVENT) 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 @Test
@ -146,7 +143,7 @@ class DefaultVectorAnalyticsTest {
defaultVectorAnalytics.capture(AN_EVENT) defaultVectorAnalytics.capture(AN_EVENT)
fakePostHog.verifyEventTracked(AN_EVENT.getName(), AN_EVENT.toPostHogProperties()) fakePostHog.verifyEventTracked(AN_EVENT.getName(), AN_EVENT.getProperties().clearNulls())
} }
@Test @Test
@ -176,16 +173,16 @@ class DefaultVectorAnalyticsTest {
fakeSentryAnalytics.verifyNoErrorTracking() fakeSentryAnalytics.verifyNoErrorTracking()
} }
}
private fun VectorAnalyticsScreen.toPostHogProperties(): Properties? { private fun Map<String, Any?>?.clearNulls(): Map<String, Any>? {
return getProperties()?.let { properties -> if (this == null) return null
Properties().also { it.putAll(properties) }
} val nonNulls = HashMap<String, Any>()
} this.forEach { (key, value) ->
if (value != null) {
private fun VectorAnalyticsEvent.toPostHogProperties(): Properties? { nonNulls[key] = value
return getProperties()?.let { properties -> }
Properties().also { it.putAll(properties) } }
return nonNulls
} }
} }

View file

@ -17,8 +17,7 @@
package im.vector.app.test.fakes package im.vector.app.test.fakes
import android.os.Looper import android.os.Looper
import com.posthog.android.PostHog import com.posthog.PostHogInterface
import com.posthog.android.Properties
import im.vector.app.features.analytics.plan.UserProperties import im.vector.app.features.analytics.plan.UserProperties
import io.mockk.every import io.mockk.every
import io.mockk.mockk import io.mockk.mockk
@ -36,16 +35,19 @@ class FakePostHog {
every { Looper.getMainLooper() } returns looper every { Looper.getMainLooper() } returns looper
} }
val instance = mockk<PostHog>(relaxed = true) val instance = mockk<PostHogInterface>(relaxed = true)
fun verifyOptOutStatus(optedOut: Boolean) { fun verifyOptOutStatus(optedOut: Boolean) {
verify { instance.optOut(optedOut) } if (optedOut) {
verify { instance.optOut() }
} else {
verify { instance.optIn() }
}
} }
fun verifyIdentifies(analyticsId: String, userProperties: UserProperties?) { fun verifyIdentifies(analyticsId: String, userProperties: UserProperties?) {
verify { verify {
val postHogProperties = userProperties?.getProperties() val postHogProperties = userProperties?.getProperties()
?.let { rawProperties -> Properties().also { it.putAll(rawProperties) } }
?.takeIf { it.isNotEmpty() } ?.takeIf { it.isNotEmpty() }
instance.identify(analyticsId, postHogProperties, null) instance.identify(analyticsId, postHogProperties, null)
} }
@ -55,7 +57,7 @@ class FakePostHog {
verify { instance.reset() } verify { instance.reset() }
} }
fun verifyScreenTracked(name: String, properties: Properties?) { fun verifyScreenTracked(name: String, properties: Map<String, Any>?) {
verify { instance.screen(name, properties) } verify { instance.screen(name, properties) }
} }
@ -63,12 +65,11 @@ class FakePostHog {
verify(exactly = 0) { verify(exactly = 0) {
instance.screen(any()) instance.screen(any())
instance.screen(any(), any()) instance.screen(any(), any())
instance.screen(any(), any(), any())
} }
} }
fun verifyEventTracked(name: String, properties: Properties?) { fun verifyEventTracked(name: String, properties: Map<String, Any>?) {
verify { instance.capture(name, properties) } verify { instance.capture(name, null, properties) }
} }
fun verifyNoEventTracking() { fun verifyNoEventTracking() {