From 28fa4ab784e63bb2ef18ab08527baf9244e783ac Mon Sep 17 00:00:00 2001 From: Valere Date: Wed, 29 May 2024 17:48:38 +0200 Subject: [PATCH] improve tests --- .../impl/DefaultVectorAnalyticsTest.kt | 44 ++++++++++++++++--- .../FakeAutoSuperPropertiesFlowProvider.kt | 36 +++++++++++++++ 2 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 vector/src/test/java/im/vector/app/test/fakes/FakeAutoSuperPropertiesFlowProvider.kt 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 70c97c234f..3b0d13c945 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 @@ -18,6 +18,7 @@ package im.vector.app.features.analytics.impl import im.vector.app.features.analytics.plan.SuperProperties import im.vector.app.test.fakes.FakeAnalyticsStore +import im.vector.app.test.fakes.FakeAutoSuperPropertiesFlowProvider import im.vector.app.test.fakes.FakeLateInitUserPropertiesFactory import im.vector.app.test.fakes.FakePostHog import im.vector.app.test.fakes.FakePostHogFactory @@ -26,12 +27,9 @@ import im.vector.app.test.fixtures.AnalyticsConfigFixture.anAnalyticsConfig import im.vector.app.test.fixtures.aUserProperties import im.vector.app.test.fixtures.aVectorAnalyticsEvent import im.vector.app.test.fixtures.aVectorAnalyticsScreen -import io.mockk.every -import io.mockk.mockk import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.test.runTest import org.junit.Before import org.junit.Test @@ -48,9 +46,7 @@ class DefaultVectorAnalyticsTest { private val fakeAnalyticsStore = FakeAnalyticsStore() private val fakeLateInitUserPropertiesFactory = FakeLateInitUserPropertiesFactory() private val fakeSentryAnalytics = FakeSentryAnalytics() - private val mockAutoSuperPropertiesFlowProvider = mockk().also { - every { it.superPropertiesFlow } returns flowOf(SuperProperties()) - } + private val fakeAutoSuperPropertiesFlowProvider = FakeAutoSuperPropertiesFlowProvider() private val defaultVectorAnalytics = DefaultVectorAnalytics( postHogFactory = FakePostHogFactory(fakePostHog.instance).instance, @@ -59,7 +55,7 @@ class DefaultVectorAnalyticsTest { globalScope = CoroutineScope(Dispatchers.Unconfined), analyticsConfig = anAnalyticsConfig(isEnabled = true), lateInitUserPropertiesFactory = fakeLateInitUserPropertiesFactory.instance, - autoSuperPropertiesFlowProvider = mockAutoSuperPropertiesFlowProvider, + autoSuperPropertiesFlowProvider = fakeAutoSuperPropertiesFlowProvider.instance, ) @Before @@ -293,6 +289,40 @@ class DefaultVectorAnalyticsTest { ) } + @Test + fun `Update super properties from flow`() = runTest { + fakeAnalyticsStore.givenUserContent(consent = true) + + fakeAutoSuperPropertiesFlowProvider.postSuperProperty( + SuperProperties( + cryptoSDKVersion = "0" + ) + ) + + val fakeEvent = aVectorAnalyticsEvent("THE_NAME", null) + defaultVectorAnalytics.capture(fakeEvent) + + fakePostHog.verifyEventTracked( + "THE_NAME", + mapOf( + "cryptoSDKVersion" to "0" + ) + ) + + fakeAutoSuperPropertiesFlowProvider.postSuperProperty(SuperProperties( + cryptoSDKVersion = "1" + )) + + defaultVectorAnalytics.capture(fakeEvent) + + fakePostHog.verifyEventTracked( + "THE_NAME", + mapOf( + "cryptoSDKVersion" to "1" + ) + ) + } + private fun Map?.clearNulls(): Map? { if (this == null) return null diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeAutoSuperPropertiesFlowProvider.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeAutoSuperPropertiesFlowProvider.kt new file mode 100644 index 0000000000..eff184298e --- /dev/null +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeAutoSuperPropertiesFlowProvider.kt @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2024 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.test.fakes + +import im.vector.app.features.analytics.impl.AutoSuperPropertiesFlowProvider +import im.vector.app.features.analytics.plan.SuperProperties +import io.mockk.every +import io.mockk.mockk +import kotlinx.coroutines.flow.MutableSharedFlow + +class FakeAutoSuperPropertiesFlowProvider { + + val flow = MutableSharedFlow() + + val instance = mockk().also { + every { it.superPropertiesFlow } returns flow + } + + suspend fun postSuperProperty(properties: SuperProperties) { + flow.emit(properties) + } +}