improve tests

This commit is contained in:
Valere 2024-05-29 17:48:38 +02:00
parent a363e392b4
commit 28fa4ab784
2 changed files with 73 additions and 7 deletions

View file

@ -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<AutoSuperPropertiesFlowProvider>().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<String, Any?>?.clearNulls(): Map<String, Any>? {
if (this == null) return null

View file

@ -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<SuperProperties>()
val instance = mockk<AutoSuperPropertiesFlowProvider>().also {
every { it.superPropertiesFlow } returns flow
}
suspend fun postSuperProperty(properties: SuperProperties) {
flow.emit(properties)
}
}