Add online state test, Update offline state tests

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

@ -56,7 +56,7 @@ class NiaAppStateTest {
// Create the test dependencies.
private val networkMonitor = TestNetworkMonitor()
private val errorMonitor = TestErrorMonitor()
private val errorMonitor = TestErrorMonitor(networkMonitor)
private val timeZoneMonitor = TestTimeZoneMonitor()
@ -122,7 +122,7 @@ class NiaAppStateTest {
)
}
backgroundScope.launch { state.isOffline.collect() }
backgroundScope.launch { state.isOfflineState.collect() }
networkMonitor.setConnected(false)
assertEquals(
true,
@ -130,6 +130,26 @@ class NiaAppStateTest {
)
}
@Test
fun niaAppState_whenNetworkMonitorIsOnline_StateIsOnline() = runTest(UnconfinedTestDispatcher()) {
composeTestRule.setContent {
state = NiaAppState(
navController = NavHostController(LocalContext.current),
coroutineScope = backgroundScope,
errorMonitor = errorMonitor,
userNewsResourceRepository = userNewsResourceRepository,
timeZoneMonitor = timeZoneMonitor,
)
}
backgroundScope.launch { state.isOfflineState.collect() }
networkMonitor.setConnected(true)
assertEquals(
false,
state.isOfflineState.value,
)
}
@Test
fun niaAppState_differentTZ_withTimeZoneMonitorChange() = runTest(UnconfinedTestDispatcher()) {
composeTestRule.setContent {

@ -20,11 +20,11 @@ import com.google.samples.apps.nowinandroid.core.data.util.ErrorMessage
import com.google.samples.apps.nowinandroid.core.data.util.ErrorMonitor
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
class TestErrorMonitor : ErrorMonitor {
class TestErrorMonitor(networkMonitor: TestNetworkMonitor) : ErrorMonitor {
override var offlineMessage: String? = "offline"
override val isOffline: Flow<Boolean>
get() = flowOf(false)
override val isOffline: Flow<Boolean> = networkMonitor.isOnline.map { !it }
override fun addShortErrorMessage(
error: String,
label: String?,

Loading…
Cancel
Save