From 89bbba0ed2938f6dfe7afdc1cb8d34ee02ed44e3 Mon Sep 17 00:00:00 2001 From: John O'Reilly Date: Sat, 10 Jul 2021 16:15:33 +0100 Subject: [PATCH] compose test updates --- .../PeopleInSpaceRepositoryFake.kt | 4 +++- .../surrus/peopleinspace/PeopleInSpaceTest.kt | 19 +++++++++++++------ .../peopleinspace/ui/PersonListScreen.kt | 5 ++++- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/app/src/androidTest/java/com/surrus/peopleinspace/PeopleInSpaceRepositoryFake.kt b/app/src/androidTest/java/com/surrus/peopleinspace/PeopleInSpaceRepositoryFake.kt index c3ec62c..045a523 100644 --- a/app/src/androidTest/java/com/surrus/peopleinspace/PeopleInSpaceRepositoryFake.kt +++ b/app/src/androidTest/java/com/surrus/peopleinspace/PeopleInSpaceRepositoryFake.kt @@ -8,8 +8,10 @@ import kotlinx.coroutines.flow.emptyFlow import kotlinx.coroutines.flow.flowOf class PeopleInSpaceRepositoryFake: PeopleInSpaceRepositoryInterface { + val peopleList = listOf(Assignment("Apollo 11", "Neil Armstrong"), + Assignment("Apollo 11", "Buzz Aldrin")) + override fun fetchPeopleAsFlow(): Flow> { - val peopleList = listOf(Assignment("ISS", "Megan McArthur")) return flowOf(peopleList) } diff --git a/app/src/androidTest/java/com/surrus/peopleinspace/PeopleInSpaceTest.kt b/app/src/androidTest/java/com/surrus/peopleinspace/PeopleInSpaceTest.kt index 344591b..23712e9 100644 --- a/app/src/androidTest/java/com/surrus/peopleinspace/PeopleInSpaceTest.kt +++ b/app/src/androidTest/java/com/surrus/peopleinspace/PeopleInSpaceTest.kt @@ -1,11 +1,11 @@ package com.surrus.peopleinspace import androidx.compose.foundation.layout.PaddingValues -import androidx.compose.ui.test.assertIsDisplayed +import androidx.compose.ui.test.* import androidx.compose.ui.test.junit4.createComposeRule -import androidx.compose.ui.test.onNodeWithText import com.surrus.peopleinspace.ui.PeopleInSpaceViewModel import com.surrus.peopleinspace.ui.PersonListScreen +import com.surrus.peopleinspace.ui.PersonListTag import org.junit.Rule import org.junit.Test @@ -26,9 +26,16 @@ class PeopleInSpaceTest { peopleInSpaceViewModel = peopleInSpaceViewModel ) } - - composeTestRule - .onNodeWithText("Megan McArthur") - .assertIsDisplayed() + + val peopleList = peopleInSpaceRepository.peopleList + + composeTestRule.onNodeWithTag(PersonListTag).assertIsDisplayed() + composeTestRule.onNodeWithTag(PersonListTag).onChildren().assertCountEquals(peopleList.size) + + peopleList.forEach { person -> + composeTestRule + .onNodeWithText(person.name) + .assertExists() + } } } diff --git a/app/src/main/java/com/surrus/peopleinspace/ui/PersonListScreen.kt b/app/src/main/java/com/surrus/peopleinspace/ui/PersonListScreen.kt index beb696e..714e136 100644 --- a/app/src/main/java/com/surrus/peopleinspace/ui/PersonListScreen.kt +++ b/app/src/main/java/com/surrus/peopleinspace/ui/PersonListScreen.kt @@ -13,6 +13,7 @@ import androidx.compose.runtime.collectAsState import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color +import androidx.compose.ui.platform.testTag import androidx.compose.ui.text.TextStyle import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp @@ -20,6 +21,8 @@ import com.google.accompanist.coil.rememberCoilPainter import com.surrus.common.remote.Assignment import org.koin.androidx.compose.getViewModel +const val PersonListTag = "PersonList" + @Composable fun PersonListScreen(paddingValues: PaddingValues, personSelected: (person: Assignment) -> Unit, @@ -32,7 +35,7 @@ fun PersonListScreen(paddingValues: PaddingValues, TopAppBar(title = { Text("People In Space") }) } ) { - LazyColumn(contentPadding = paddingValues) { + LazyColumn(contentPadding = paddingValues, modifier = Modifier.testTag(PersonListTag)) { items(peopleState.value) { person -> PersonView(person, personSelected) }