From d100b62e5444dd98733e1216ef8085db6bcd30db Mon Sep 17 00:00:00 2001 From: Valere Date: Mon, 6 May 2024 10:31:45 +0200 Subject: [PATCH] Fix posthog tests --- .../impl/DefaultVectorAnalyticsTest.kt | 27 +++++++++---------- .../im/vector/app/test/fakes/FakePostHog.kt | 19 ++++++------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/vector/src/test/java/im/vector/app/features/analytics/impl/DefaultVectorAnalyticsTest.kt b/vector/src/test/java/im/vector/app/features/analytics/impl/DefaultVectorAnalyticsTest.kt index 226a2d8ecf..ea8beaa86c 100644 --- a/vector/src/test/java/im/vector/app/features/analytics/impl/DefaultVectorAnalyticsTest.kt +++ b/vector/src/test/java/im/vector/app/features/analytics/impl/DefaultVectorAnalyticsTest.kt @@ -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?.clearNulls(): Map? { + if (this == null) return null + + val nonNulls = HashMap() + this.forEach { (key, value) -> + if (value != null) { + nonNulls[key] = value + } + } + return nonNulls } } diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakePostHog.kt b/vector/src/test/java/im/vector/app/test/fakes/FakePostHog.kt index e14f809e66..ccc326ba14 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakePostHog.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakePostHog.kt @@ -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(relaxed = true) + val instance = mockk(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?) { 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?) { + verify { instance.capture(name, null, properties) } } fun verifyNoEventTracking() {