Skip to content

Commit

Permalink
[#354] Create initComposable to build AndroidComposeTestRule for Main…
Browse files Browse the repository at this point in the history
…Activity + HomeScreen
  • Loading branch information
luongvo committed Feb 14, 2023
1 parent 41c3f7f commit 922751f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package co.nimblehq.sample.compose.ui.screens.home

import androidx.activity.compose.setContent
import androidx.compose.ui.test.*
import androidx.compose.ui.test.junit4.ComposeContentTestRule
import androidx.compose.ui.test.junit4.AndroidComposeTestRule
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.rule.GrantPermissionRule
import co.nimblehq.sample.compose.domain.model.Model
import co.nimblehq.sample.compose.domain.usecase.UseCase
Expand Down Expand Up @@ -66,7 +67,9 @@ class HomeScreenTest {
assertEquals(expectedAppDestination, AppDestination.Second)
}

private fun initComposable(testBody: ComposeContentTestRule.() -> Unit) {
private fun initComposable(
testBody: AndroidComposeTestRule<ActivityScenarioRule<MainActivity>, MainActivity>.() -> Unit
) {
composeRule.activity.setContent {
ComposeTheme {
HomeScreen(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package co.nimblehq.template.compose.ui.screens.home

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 androidx.compose.ui.test.*
import androidx.compose.ui.test.junit4.*
import androidx.test.ext.junit.rules.ActivityScenarioRule
import co.nimblehq.template.compose.R
import co.nimblehq.template.compose.ui.AppDestination
import co.nimblehq.template.compose.ui.screens.MainActivity
import co.nimblehq.template.compose.ui.theme.ComposeTheme
import org.junit.*
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
Expand All @@ -21,17 +22,24 @@ class HomeScreenTest {

@Before
fun setUp() {
composeRule.activity.setContent {
HomeScreen(
navigator = { destination -> expectedAppDestination = destination }
)
}
// TODO more setup logic here
}

@Test
fun `When entering the Home screen, it shows UI correctly`() {
composeRule.run {
onNodeWithText(activity.getString(R.string.app_name)).assertIsDisplayed()
fun `When entering the Home screen, it shows UI correctly`() = initComposable {
onNodeWithText(activity.getString(R.string.app_name)).assertIsDisplayed()
}

private fun initComposable(
testBody: AndroidComposeTestRule<ActivityScenarioRule<MainActivity>, MainActivity>.() -> Unit
) {
composeRule.activity.setContent {
ComposeTheme {
HomeScreen(
navigator = { destination -> expectedAppDestination = destination }
)
}
}
testBody(composeRule)
}
}

0 comments on commit 922751f

Please sign in to comment.