Skip to content

Instantly share code, notes, and snippets.

@rubenquadros
Last active August 20, 2021 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubenquadros/445e27eabfc7fd869e6bc5bc9287b48e to your computer and use it in GitHub Desktop.
Save rubenquadros/445e27eabfc7fd869e6bc5bc9287b48e to your computer and use it in GitHub Desktop.
Home screen test
class HomeScreenTest {
@get:Rule val composeTestRule = createComposeRule()
//Mock the viewmodel
private val homeViewModel = mockk<HomeViewModel>()
@Before
fun init() {
MockKAnnotations.init(this, true)
//provide mock data
every { homeViewModel.getAllGames() } answers
{
FakeGamesData.getFakePagingData()
}
}
@Test
fun games_should_be_displayed_in_home_screen() {
//load our composable
composeTestRule.setContent {
EpicWorldTheme {
HomeScreen(
openSearch = {},
openFilters = {},
openGameDetails = {},
homeViewModel = homeViewModel
)
}
}
//assert that a view with text "Max Payne" is displayed
composeTestRule
.onNodeWithText("Max Payne")
.assertIsDisplayed()
//assert that a view with text "4.5" is displayed
composeTestRule
.onNodeWithText("4.5")
.assertIsDisplayed()
//assert that 2 views with content description "Image" are displayed
composeTestRule
.onAllNodesWithContentDescription("Image")
.assertCountEquals(2)
//assert that 2 views with content description "Star" are displayed
composeTestRule
.onAllNodesWithContentDescription("Star")
.assertCountEquals(2)
//assert that a view with text "GTA V" is displayed
composeTestRule
.onNodeWithText("GTA V")
.assertIsDisplayed()
//assert that a view with text "4.8" is displayed
composeTestRule
.onNodeWithText("4.8")
.assertIsDisplayed()
}
}
//Provide mock data
object FakeGamesData {
fun getFakePagingData():
Flow<PagingData<GameResultsEntity>> {
return flow {
emit(PagingData.from(getGamesEntity()))
}
}
private fun getGamesEntity(): List<GameResultsEntity> {
val gameResults: ArrayList<GameResultsEntity> =
ArrayList()
gameResults.add(
GameResultsEntity(1, "Max Payne", "", 4.5)
)
gameResults.add(
GameResultsEntity(2, "GTA V", "", 4.8)
)
return gameResults
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment