Skip to content

Instantly share code, notes, and snippets.

@igorescodro
Created March 17, 2021 17:22
Show Gist options
  • Save igorescodro/9919da0c035e30fef03bd7eb6816b03e to your computer and use it in GitHub Desktop.
Save igorescodro/9919da0c035e30fef03bd7eb6816b03e to your computer and use it in GitHub Desktop.
internal class MyComposableTest {
@get:Rule
val flakyRule = FlakyTestRule()
@get:Rule
val composeTestRule = createEmptyComposeRule()
private lateinit var scenario: ActivityScenario<ComponentActivity>
@Before
fun setup() {
scenario = ActivityScenario.launch(ComponentActivity::class.java)
}
@After
fun tearDown() {
scenario.close()
}
@Test
@AllowFlaky(attempts = 5)
fun test_TitleIsShownWhenLoaded() {
initComposable(state = MyState.Loaded)
composeTestRule.onNodeWithText("Title").assertIsDisplayed()
}
@Test
fun test_TitleIsNotShownWhenError() {
initComposable(state = MyState.Error)
composeTestRule.onNodeWithText("Title").assertIsNotDisplayed()
}
private fun initComposable(state: MyState) {
scenario.onActivity { activity ->
MyTheme {
activity.setContent {
MyComposable(state = state)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment