Add turbine test assertion

This commit is contained in:
Wolf-Martell Montwé 2023-05-30 17:40:28 +02:00
parent 6b44ff4004
commit 493356726a
No known key found for this signature in database
GPG key ID: 6D45B21512ACBF72
4 changed files with 48 additions and 26 deletions

View file

@ -4,4 +4,5 @@ plugins {
dependencies { dependencies {
implementation(libs.assertk) implementation(libs.assertk)
implementation(libs.turbine)
} }

View file

@ -0,0 +1,34 @@
package assertk.assertions
import app.cash.turbine.ReceiveTurbine
import assertk.Assert
import assertk.all
import assertk.assertThat
/**
* The `assertThatAndTurbinesConsumed` function ensures that the assertion passed and
* all events in the given turbines have been consumed.
*
* Usage:
* val actualValue: T = getActualValue()
* val turbines: List<ReceiveTurbine<*>> = getTurbines()
* assertThatAndEnsureAllEventsConsumed(actualValue, turbines) {
* // your assertion here
* }
*
* @param T The type of the actual value.
* @param actual The actual value being asserted.
* @param turbines The list of ReceiveTurbine instances to check if all events are consumed.
* @param assertion An extension function on `Assert<T>`, which is used to define assertions on the actual value.
*/
fun <T> assertThatAndTurbinesConsumed(
actual: T,
turbines: List<ReceiveTurbine<*>>,
assertion: Assert<T>.() -> Unit,
) {
assertThat(actual).all {
assertion()
}
turbines.forEach { it.ensureAllEventsConsumed() }
}

View file

@ -7,6 +7,8 @@ android {
} }
dependencies { dependencies {
api(projects.core.testing)
implementation(projects.core.ui.compose.theme) implementation(projects.core.ui.compose.theme)
implementation(libs.androidx.compose.material) implementation(libs.androidx.compose.material)

View file

@ -1,15 +1,12 @@
package app.k9mail.feature.account.setup.ui package app.k9mail.feature.account.setup.ui
import app.cash.turbine.ReceiveTurbine
import app.cash.turbine.testIn import app.cash.turbine.testIn
import app.k9mail.core.ui.compose.testing.MainDispatcherRule import app.k9mail.core.ui.compose.testing.MainDispatcherRule
import app.k9mail.feature.account.setup.ui.AccountSetupContract.Effect.NavigateBack import app.k9mail.feature.account.setup.ui.AccountSetupContract.Effect.NavigateBack
import app.k9mail.feature.account.setup.ui.AccountSetupContract.Effect.NavigateNext import app.k9mail.feature.account.setup.ui.AccountSetupContract.Effect.NavigateNext
import app.k9mail.feature.account.setup.ui.AccountSetupContract.SetupStep import app.k9mail.feature.account.setup.ui.AccountSetupContract.SetupStep
import app.k9mail.feature.account.setup.ui.AccountSetupContract.State import app.k9mail.feature.account.setup.ui.AccountSetupContract.State
import assertk.Assert import assertk.assertions.assertThatAndTurbinesConsumed
import assertk.all
import assertk.assertThat
import assertk.assertions.isEqualTo import assertk.assertions.isEqualTo
import assertk.assertions.prop import assertk.assertions.prop
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
@ -31,7 +28,7 @@ class AccountSetupViewModelTest {
val turbines = listOf(stateTurbine, effectTurbine) val turbines = listOf(stateTurbine, effectTurbine)
// Initial state // Initial state
assertThatAndAllEventsConsumed( assertThatAndTurbinesConsumed(
actual = stateTurbine.awaitItem(), actual = stateTurbine.awaitItem(),
turbines = turbines, turbines = turbines,
) { ) {
@ -40,7 +37,7 @@ class AccountSetupViewModelTest {
viewModel.event(AccountSetupContract.Event.OnNext) viewModel.event(AccountSetupContract.Event.OnNext)
assertThatAndAllEventsConsumed( assertThatAndTurbinesConsumed(
actual = stateTurbine.awaitItem(), actual = stateTurbine.awaitItem(),
turbines = turbines, turbines = turbines,
) { ) {
@ -49,7 +46,7 @@ class AccountSetupViewModelTest {
viewModel.event(AccountSetupContract.Event.OnNext) viewModel.event(AccountSetupContract.Event.OnNext)
assertThatAndAllEventsConsumed( assertThatAndTurbinesConsumed(
actual = stateTurbine.awaitItem(), actual = stateTurbine.awaitItem(),
turbines = turbines, turbines = turbines,
) { ) {
@ -58,7 +55,7 @@ class AccountSetupViewModelTest {
viewModel.event(AccountSetupContract.Event.OnNext) viewModel.event(AccountSetupContract.Event.OnNext)
assertThatAndAllEventsConsumed( assertThatAndTurbinesConsumed(
actual = stateTurbine.awaitItem(), actual = stateTurbine.awaitItem(),
turbines = turbines, turbines = turbines,
) { ) {
@ -67,7 +64,7 @@ class AccountSetupViewModelTest {
viewModel.event(AccountSetupContract.Event.OnNext) viewModel.event(AccountSetupContract.Event.OnNext)
assertThatAndAllEventsConsumed( assertThatAndTurbinesConsumed(
actual = effectTurbine.awaitItem(), actual = effectTurbine.awaitItem(),
turbines = turbines, turbines = turbines,
) { ) {
@ -84,7 +81,7 @@ class AccountSetupViewModelTest {
val turbines = listOf(stateTurbine, effectTurbine) val turbines = listOf(stateTurbine, effectTurbine)
// Initial state // Initial state
assertThatAndAllEventsConsumed( assertThatAndTurbinesConsumed(
actual = stateTurbine.awaitItem(), actual = stateTurbine.awaitItem(),
turbines = turbines, turbines = turbines,
) { ) {
@ -93,7 +90,7 @@ class AccountSetupViewModelTest {
viewModel.event(AccountSetupContract.Event.OnBack) viewModel.event(AccountSetupContract.Event.OnBack)
assertThatAndAllEventsConsumed( assertThatAndTurbinesConsumed(
actual = stateTurbine.awaitItem(), actual = stateTurbine.awaitItem(),
turbines = turbines, turbines = turbines,
) { ) {
@ -102,7 +99,7 @@ class AccountSetupViewModelTest {
viewModel.event(AccountSetupContract.Event.OnBack) viewModel.event(AccountSetupContract.Event.OnBack)
assertThatAndAllEventsConsumed( assertThatAndTurbinesConsumed(
actual = stateTurbine.awaitItem(), actual = stateTurbine.awaitItem(),
turbines = turbines, turbines = turbines,
) { ) {
@ -111,7 +108,7 @@ class AccountSetupViewModelTest {
viewModel.event(AccountSetupContract.Event.OnBack) viewModel.event(AccountSetupContract.Event.OnBack)
assertThatAndAllEventsConsumed( assertThatAndTurbinesConsumed(
actual = stateTurbine.awaitItem(), actual = stateTurbine.awaitItem(),
turbines = turbines, turbines = turbines,
) { ) {
@ -120,23 +117,11 @@ class AccountSetupViewModelTest {
viewModel.event(AccountSetupContract.Event.OnBack) viewModel.event(AccountSetupContract.Event.OnBack)
assertThatAndAllEventsConsumed( assertThatAndTurbinesConsumed(
actual = effectTurbine.awaitItem(), actual = effectTurbine.awaitItem(),
turbines = turbines, turbines = turbines,
) { ) {
isEqualTo(NavigateBack) isEqualTo(NavigateBack)
} }
} }
private fun <T> assertThatAndAllEventsConsumed(
actual: T,
turbines: List<ReceiveTurbine<*>>,
assertion: Assert<T>.() -> Unit,
) {
assertThat(actual).all {
assertion()
}
turbines.forEach { it.ensureAllEventsConsumed() }
}
} }