mirror of
https://github.com/bitwarden/android.git
synced 2026-04-28 03:48:14 -05:00
[PM-21328] Migrate BaseViewModelTest and MainDispatcherExtension to test fixtures (#5146)
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package com.bitwarden.ui.platform.base
|
||||
|
||||
import app.cash.turbine.ReceiveTurbine
|
||||
import app.cash.turbine.TurbineContext
|
||||
import app.cash.turbine.turbineScope
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import org.junit.jupiter.api.extension.RegisterExtension
|
||||
|
||||
abstract class BaseViewModelTest {
|
||||
@Suppress("unused", "JUnitMalformedDeclaration")
|
||||
@RegisterExtension
|
||||
protected open val mainDispatcherExtension = MainDispatcherExtension()
|
||||
|
||||
protected suspend fun <S : Any, E : Any, T : BaseViewModel<S, E, *>> T.stateEventFlow(
|
||||
backgroundScope: CoroutineScope,
|
||||
validate: suspend TurbineContext.(
|
||||
stateFlow: ReceiveTurbine<S>,
|
||||
eventFlow: ReceiveTurbine<E>,
|
||||
) -> Unit,
|
||||
) {
|
||||
turbineScope {
|
||||
this.validate(
|
||||
this@stateEventFlow.stateFlow.testIn(backgroundScope),
|
||||
this@stateEventFlow.eventFlow.testIn(backgroundScope),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.bitwarden.ui.platform.base
|
||||
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.TestDispatcher
|
||||
import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
||||
import kotlinx.coroutines.test.resetMain
|
||||
import kotlinx.coroutines.test.setMain
|
||||
import org.junit.jupiter.api.extension.AfterAllCallback
|
||||
import org.junit.jupiter.api.extension.AfterEachCallback
|
||||
import org.junit.jupiter.api.extension.BeforeAllCallback
|
||||
import org.junit.jupiter.api.extension.BeforeEachCallback
|
||||
import org.junit.jupiter.api.extension.ExtendWith
|
||||
import org.junit.jupiter.api.extension.ExtensionContext
|
||||
import org.junit.jupiter.api.extension.RegisterExtension
|
||||
|
||||
/**
|
||||
* JUnit 5 Extension for automatically setting a [testDispatcher] as the "main" dispatcher.
|
||||
*
|
||||
* Note that this may be used as a normal class property with [RegisterExtension] or may be applied
|
||||
* directly to a test class using [ExtendWith].
|
||||
*/
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class MainDispatcherExtension(
|
||||
private val testDispatcher: TestDispatcher = UnconfinedTestDispatcher(),
|
||||
) : AfterAllCallback,
|
||||
AfterEachCallback,
|
||||
BeforeAllCallback,
|
||||
BeforeEachCallback {
|
||||
override fun afterAll(context: ExtensionContext?) {
|
||||
Dispatchers.resetMain()
|
||||
}
|
||||
|
||||
override fun afterEach(context: ExtensionContext?) {
|
||||
Dispatchers.resetMain()
|
||||
}
|
||||
|
||||
override fun beforeAll(context: ExtensionContext?) {
|
||||
Dispatchers.setMain(testDispatcher)
|
||||
}
|
||||
|
||||
override fun beforeEach(context: ExtensionContext?) {
|
||||
Dispatchers.setMain(testDispatcher)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user