Update General Error Handling Tests

pull/1461/head
TM 5 months ago
parent d25152b8d1
commit 1d5f10a76b

@ -23,14 +23,41 @@ import kotlinx.coroutines.flow.flowOf
import javax.inject.Inject
class FakeErrorMonitor @Inject constructor() : ErrorMonitor {
override fun addErrorMessage(error: String): String? {
override var offlineMessage: String? = "offline"
override val isOffline: Flow<Boolean>
get() = flowOf(false)
override fun addShortErrorMessage(
error: String,
label: String?,
successAction: (() -> Unit)?,
failureAction: (() -> Unit)?,
): String? {
return "1"
}
override fun addLongErrorMessage(
error: String,
label: String?,
successAction: (() -> Unit)?,
failureAction: (() -> Unit)?,
): String? {
return "2"
}
override fun addIndefiniteErrorMessage(
error: String,
label: String?,
successAction: (() -> Unit)?,
failureAction: (() -> Unit)?,
): String? {
return "3"
}
override fun clearErrorMessage(id: String) {
// Do nothing
}
override val errorMessages: Flow<List<ErrorMessage?>>
get() = flowOf(emptyList())
override val errorMessage: Flow<ErrorMessage?>
get() = flowOf(null)
}

@ -15,11 +15,14 @@
*/
import com.google.samples.apps.nowinandroid.core.data.util.ErrorMessage
import com.google.samples.apps.nowinandroid.core.data.util.NetworkMonitor
import com.google.samples.apps.nowinandroid.core.data.util.SnackbarErrorMonitor
import com.google.samples.apps.nowinandroid.core.testing.util.TestNetworkMonitor
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import kotlin.test.assertEquals
@ -29,14 +32,22 @@ import kotlin.test.assertEquals
class SnackbarErrorMonitorTest {
lateinit var networkMonitor: NetworkMonitor
// Subject under test.
private var state = SnackbarErrorMonitor()
private lateinit var state: SnackbarErrorMonitor
private var message: ErrorMessage? = null
@Before
fun setup() {
networkMonitor = TestNetworkMonitor()
state = SnackbarErrorMonitor(networkMonitor)
}
@Test
fun whenErrorIsNotAdded_NullIsPresent() = runTest(UnconfinedTestDispatcher()) {
backgroundScope.launch { state.errorMessages.collect() }
backgroundScope.launch { state.errorMessage.collect() }
assertEquals(
null,
message,
@ -46,12 +57,12 @@ class SnackbarErrorMonitorTest {
@Test
fun whenErrorIsAdded_ErrorMessageIsPresent() = runTest(UnconfinedTestDispatcher()) {
backgroundScope.launch {
state.errorMessages.collect {
message = it.firstOrNull()
state.errorMessage.collect {
message = it
}
}
val id = state.addErrorMessage("Test Error Message")
val id = state.addShortErrorMessage("Test Error Message")
assertEquals(
id,
@ -62,12 +73,12 @@ class SnackbarErrorMonitorTest {
@Test
fun whenErrorsAreAdded_FirstErrorMessageIsPresent() =
runTest(UnconfinedTestDispatcher()) {
val id1 = state.addErrorMessage("Test Error Message 1")
state.addErrorMessage("Test Error Message 2")
val id1 = state.addShortErrorMessage("Test Error Message 1")
state.addShortErrorMessage("Test Error Message 2")
backgroundScope.launch {
state.errorMessages.collect {
message = it.firstOrNull()
state.errorMessage.collect {
message = it
}
}
@ -81,11 +92,11 @@ class SnackbarErrorMonitorTest {
fun whenErrorIsCleared_ErrorMessageIsNotPresent() =
runTest(UnconfinedTestDispatcher()) {
backgroundScope.launch {
state.errorMessages.collect {
message = it.firstOrNull()
state.errorMessage.collect {
message = it
}
}
val id = state.addErrorMessage("Test Error Message 1")
val id = state.addShortErrorMessage("Test Error Message 1")
if (id != null) {
state.clearErrorMessage(id)
assertEquals(
@ -99,12 +110,12 @@ class SnackbarErrorMonitorTest {
fun whenErrorsAreCleared_NextErrorMessageIsPresent() =
runTest(UnconfinedTestDispatcher()) {
backgroundScope.launch {
state.errorMessages.collect {
message = it.firstOrNull()
state.errorMessage.collect {
message = it
}
}
val id1 = state.addErrorMessage("Test Error Message 1")
val id2 = state.addErrorMessage("Test Error Message 2")
val id1 = state.addShortErrorMessage("Test Error Message 1")
val id2 = state.addShortErrorMessage("Test Error Message 2")
if (id1 != null) {
state.clearErrorMessage(id1)
assertEquals(

@ -22,14 +22,40 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
class TestErrorMonitor : ErrorMonitor {
override fun addErrorMessage(error: String): String? {
override var offlineMessage: String? = "offline"
override val isOffline: Flow<Boolean>
get() = flowOf(false)
override fun addShortErrorMessage(
error: String,
label: String?,
successAction: (() -> Unit)?,
failureAction: (() -> Unit)?,
): String? {
return "1"
}
override fun addLongErrorMessage(
error: String,
label: String?,
successAction: (() -> Unit)?,
failureAction: (() -> Unit)?,
): String? {
return "2"
}
override fun addIndefiniteErrorMessage(
error: String,
label: String?,
successAction: (() -> Unit)?,
failureAction: (() -> Unit)?,
): String? {
return "3"
}
override fun clearErrorMessage(id: String) {
// Do nothing
}
override val errorMessages: Flow<List<ErrorMessage?>>
get() = flowOf(listOf(ErrorMessage("Error Message", "1")))
override val errorMessage: Flow<ErrorMessage?>
get() = flowOf(ErrorMessage("Error Message", "1"))
}

Loading…
Cancel
Save