Skip to content

Commit

Permalink
[#83] Minor refactors from PR.
Browse files Browse the repository at this point in the history
Move composeAndroidTestRule to BaseScreenTest
  • Loading branch information
kaungkhantsoe committed Feb 20, 2023
1 parent d6008a5 commit 9052846
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package co.nimblehq.compose.crypto.ui

import androidx.compose.ui.test.junit4.createAndroidComposeRule
import co.nimblehq.compose.crypto.test.CoroutineTestRule
import co.nimblehq.compose.crypto.test.runBlockingTest
import co.nimblehq.compose.crypto.ui.screens.MainActivity
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestCoroutineScope
import org.junit.Rule

@ExperimentalCoroutinesApi
abstract class BaseScreenTest {

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

@get:Rule
private var coroutineRule = CoroutineTestRule()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package co.nimblehq.compose.crypto.ui.screen

import androidx.activity.compose.setContent
import androidx.compose.ui.test.*
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import co.nimblehq.compose.crypto.test.MockUtil
import co.nimblehq.compose.crypto.R
import co.nimblehq.compose.crypto.domain.usecase.GetMyCoinsUseCase
import co.nimblehq.compose.crypto.domain.usecase.GetTrendingCoinsUseCase
import co.nimblehq.compose.crypto.extension.toFormattedString
import co.nimblehq.compose.crypto.ui.BaseScreenTest
import co.nimblehq.compose.crypto.ui.navigation.AppDestination
import co.nimblehq.compose.crypto.ui.screens.MainActivity
import co.nimblehq.compose.crypto.ui.screens.home.*
import io.kotest.matchers.shouldBe
import io.mockk.every
Expand All @@ -19,15 +17,11 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf
import org.junit.Before
import org.junit.Rule
import org.junit.Test

@ExperimentalCoroutinesApi
class HomeScreenUITest : BaseScreenTest() {

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

private val homeTitle: String
get() = composeAndroidTestRule.activity.getString(R.string.home_title)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package co.nimblehq.compose.crypto.ui.screen.detail

import androidx.activity.compose.setContent
import androidx.compose.ui.test.*
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import co.nimblehq.compose.crypto.R
import co.nimblehq.compose.crypto.domain.usecase.GetCoinDetailUseCase
import co.nimblehq.compose.crypto.domain.usecase.GetCoinPricesUseCase
Expand All @@ -11,8 +10,6 @@ import co.nimblehq.compose.crypto.test.MockUtil.coinDetail
import co.nimblehq.compose.crypto.test.MockUtil.coinPrices
import co.nimblehq.compose.crypto.ui.BaseScreenTest
import co.nimblehq.compose.crypto.ui.components.chartintervals.TimeIntervals
import co.nimblehq.compose.crypto.ui.navigation.AppDestination
import co.nimblehq.compose.crypto.ui.screens.MainActivity
import co.nimblehq.compose.crypto.ui.screens.detail.*
import co.nimblehq.compose.crypto.ui.screens.home.FIAT_CURRENCY
import io.mockk.every
Expand All @@ -22,16 +19,12 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import kotlin.math.abs

@ExperimentalCoroutinesApi
class DetailScreenUiTest : BaseScreenTest() {

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

private val errorGeneric: String
get() = composeAndroidTestRule.activity.getString(R.string.error_generic)

Expand All @@ -40,11 +33,8 @@ class DetailScreenUiTest : BaseScreenTest() {

private lateinit var detailViewModel: DetailViewModel

private var appDestination: AppDestination? = null

@Before
fun setUp() {

every { mockGetCoinDetailUseCase.execute(any()) } returns flowOf(coinDetail)
every { mockGetCoinPricesUseCase.execute(any()) } returns flowOf(coinPrices)

Expand All @@ -53,9 +43,7 @@ class DetailScreenUiTest : BaseScreenTest() {
DetailScreen(
coinId = "",
viewModel = detailViewModel,
navigator = { destination ->
appDestination = destination
}
navigator = { }
)
}
}
Expand All @@ -78,7 +66,6 @@ class DetailScreenUiTest : BaseScreenTest() {

@Test
fun when_navigating_to_detail_screen_it_renders_chart_interval_buttons_properly() {

with(composeAndroidTestRule) {
onNodeWithText(TimeIntervals.ONE_DAY.text).assertIsDisplayed()
onNodeWithText(TimeIntervals.ONE_WEEK.text).assertIsDisplayed()
Expand All @@ -90,7 +77,6 @@ class DetailScreenUiTest : BaseScreenTest() {

@Test
fun when_navigating_to_detail_screen_it_render_currentPrice_and_priceChangePercentage24hInCurrency_properly() {

with(composeAndroidTestRule) {
coinDetail.marketData?.let { marketData ->
val currentPrice = "$${marketData.currentPrice[FIAT_CURRENCY]?.toFormattedString()}"
Expand All @@ -107,7 +93,6 @@ class DetailScreenUiTest : BaseScreenTest() {

@Test
fun when_navigating_to_detail_screen_it_render_coin_info_properly() {

with(composeAndroidTestRule) {
coinDetail.marketData?.let { marketData ->
val marketCap = "$${marketData.marketCap[FIAT_CURRENCY]?.toFormattedString()}"
Expand All @@ -124,6 +109,7 @@ class DetailScreenUiTest : BaseScreenTest() {

@Test
fun when_navigating_to_detail_screen_and_has_api_error_coin_price_chart_is_not_displayed() {
// TODO: Investigate using ShadowToast as described in https://github.com/nimblehq/jetpack-compose-crypto/pull/95#discussion_r1109230673
every { mockGetCoinDetailUseCase.execute(any()) } returns flow {
throw Throwable(errorGeneric)
}
Expand All @@ -135,7 +121,6 @@ class DetailScreenUiTest : BaseScreenTest() {

@Test
fun when_navigating_to_detail_screen_chart_interval_buttons_are_clickable() {

with(composeAndroidTestRule) {
onNodeWithText(TimeIntervals.ONE_DAY.text).assertHasClickAction()
onNodeWithText(TimeIntervals.ONE_WEEK.text).assertHasClickAction()
Expand Down

0 comments on commit 9052846

Please sign in to comment.