Skip to content

Commit

Permalink
[#102] Update isNetworkConnected to be StateFlow and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
kaungkhantsoe committed Dec 10, 2023
1 parent a3f5d4b commit 4b5bc67
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 11 deletions.
10 changes: 5 additions & 5 deletions app/src/main/java/co/nimblehq/compose/crypto/CryptoAppState.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package co.nimblehq.compose.crypto

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.remember
import co.nimblehq.compose.crypto.domain.usecase.IsNetworkConnectedUseCase
import co.nimblehq.compose.crypto.util.DispatchersProvider
Expand All @@ -15,17 +16,16 @@ fun rememberCryptoAppState(
CryptoAppState(isNetworkConnectedUseCase, dispatchersProvider)
}

@Stable
class CryptoAppState(
isNetworkConnectedUseCase: IsNetworkConnectedUseCase,
dispatchersProvider: DispatchersProvider,
) {
private val _isNetworkConnected = MutableSharedFlow<Boolean?>()
val isNetworkConnected: SharedFlow<Boolean?>
get() = _isNetworkConnected
private val _isNetworkConnected = MutableStateFlow<Boolean?>(null)
val isNetworkConnected = _isNetworkConnected.asStateFlow()

private val _networkError = MutableSharedFlow<Throwable?>()
val networkError: SharedFlow<Throwable?>
get() = _networkError
val networkError = _networkError.asSharedFlow()

init {
isNetworkConnectedUseCase()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package co.nimblehq.compose.crypto.ui.navigation

import android.widget.Toast
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.platform.LocalContext
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
Expand All @@ -15,7 +18,9 @@ fun ComposeCryptoApp(
) {
val context = LocalContext.current

cryptoAppState.isNetworkConnected.collectAsEffect { isNetworkConnected ->
val isNetworkConnected by cryptoAppState.isNetworkConnected.collectAsState()

LaunchedEffect(isNetworkConnected) {
if (isNetworkConnected == false) {
val destination = AppDestination.NoNetwork

Expand All @@ -30,7 +35,5 @@ fun ComposeCryptoApp(
Toast.makeText(context, error?.message, Toast.LENGTH_SHORT).show()
}

AppNavigation(
navController = navController
)
AppNavigation(navController = navController)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package co.nimblehq.compose.crypto.ui.screens

import androidx.activity.compose.setContent
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithText
import co.nimblehq.compose.crypto.R
import co.nimblehq.compose.crypto.domain.usecase.IsNetworkConnectedUseCase
import co.nimblehq.compose.crypto.rememberCryptoAppState
import co.nimblehq.compose.crypto.ui.navigation.ComposeCryptoApp
import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flowOf
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(RobolectricTestRunner::class)
class ComposeCryptoAppTest : BaseViewModelTest() {

@get:Rule
val composeAndroidTestRule = createAndroidComposeRule<MainActivity>()

private val mockIsNetworkConnectedUseCase: IsNetworkConnectedUseCase = mockk()

private val noInternetMessage: String
get() = composeAndroidTestRule.activity.getString(R.string.no_internet_message)

@Before
fun setUp() {
composeAndroidTestRule.activity.setContent {
ComposeCryptoApp(
cryptoAppState = rememberCryptoAppState(
isNetworkConnectedUseCase = mockIsNetworkConnectedUseCase,
dispatchersProvider = testDispatcherProvider
)
)
}
}

@Test
fun `When there is no internet, it shows dialog`() {
every { mockIsNetworkConnectedUseCase.invoke() } returns flowOf(false)

with(composeAndroidTestRule) {
onNodeWithText(noInternetMessage).assertIsDisplayed()
}
}

@Test
fun `When there is internet, it does not show dialog`() {
every { mockIsNetworkConnectedUseCase.invoke() } returns flowOf(true)

with(composeAndroidTestRule) {
onNodeWithText(noInternetMessage).assertDoesNotExist()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class DetailScreenTest : BaseViewModelTest() {
viewModel = viewModel,
navigator = { destination -> appDestination = destination },
coinId = "Bitcoin",
onNetworkReconnected = {}
onShowGlobalDialog = {}
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ class HomeScreenTest : BaseViewModelTest() {
composeAndroidTestRule.activity.setContent {
HomeScreen(
viewModel = viewModel,
navigator = { destination -> appDestination = destination }
navigator = { destination -> appDestination = destination },
onShowGlobalDialog = {}
)
}
}
Expand Down

0 comments on commit 4b5bc67

Please sign in to comment.