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
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
@ -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
@ -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
@ -176,16 +173,16 @@ class DefaultVectorAnalyticsTest {
fakeSentryAnalytics.verifyNoErrorTracking()
}
}
private fun VectorAnalyticsScreen.toPostHogProperties(): Properties? {
return getProperties()?.let { properties ->
Properties().also { it.putAll(properties) }
}
}
private fun VectorAnalyticsEvent.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
val nonNulls = HashMap<String, Any>()
this.forEach { (key, value) ->
if (value != null) {
nonNulls[key] = value
}
}
return nonNulls
}
}

View file

@ -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
@ -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)
}
@ -55,7 +57,7 @@ class FakePostHog {
verify { instance.reset() }
}
fun verifyScreenTracked(name: String, properties: Properties?) {
fun verifyScreenTracked(name: String, properties: Map<String, Any>?) {
verify { instance.screen(name, properties) }
}
@ -63,12 +65,11 @@ class FakePostHog {
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() {