Skip to content

Instantly share code, notes, and snippets.

@adavis
Last active September 17, 2021 09:32
Show Gist options
  • Save adavis/5654c8356d61fbc8f919e3b7cfcd9717 to your computer and use it in GitHub Desktop.
Save adavis/5654c8356d61fbc8f919e3b7cfcd9717 to your computer and use it in GitHub Desktop.
Instrumentation tests for Jetpack Compose screen using Intents for navigation
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_graph"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.meetup.organizer.home.HomeFragment"
android:label="HomeFragment">
<action
android:id="@+id/action_homeFragment_to_nonOrganizerFragment"
app:destination="@id/nonOrganizerFragment" />
</fragment>
<fragment
android:id="@+id/nonOrganizerFragment"
android:name="com.meetup.organizer.auth.NonOrganizerFragment"
android:label="NonOrganizerFragment" />
</navigation>
@ExperimentalAnimationApi
@RunWith(AndroidJUnit4::class)
class NonOrganizerTest {
@get:Rule
val composeTestRule = createAndroidComposeRule<MainActivity>()
@Before
fun goToNonOrganizerFragment() {
composeTestRule.activityRule.scenario.onActivity {
findNavController(it, R.id.nav_host_fragment).navigate(R.id.nonOrganizerFragment)
}
Intents.init()
}
@After
fun tearDown() {
Intents.release()
}
@Test
fun shouldDisplayStaticContent() {
listOf(
NON_ORGANIZER_MAIN_IMAGE,
NON_ORGANIZER_MAIN_TEXT,
NON_ORGANIZER_CONTACT_US,
NON_ORGANIZER_MEMBER_APP
).onEach {
composeTestRule.onNodeWithTag(it)
.assertIsDisplayed()
}
}
@Test
fun shouldGoToWeb_whenContactUsClicked() {
composeTestRule.onNodeWithTag(NON_ORGANIZER_CONTACT_US).performClick()
intended(
allOf(
hasAction(Intent.ACTION_VIEW),
hasData("https://help.meetup.com")
)
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment