Skip to content

Commit

Permalink
Fix LazyList testing (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
phansier committed Aug 19, 2024
1 parent 8c6a6f6 commit 87a8534
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
13 changes: 10 additions & 3 deletions app/src/androidTest/java/ru/beryukhov/coffeegram/ComposeAppTest.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package ru.beryukhov.coffeegram

import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import io.github.kakaocup.compose.node.element.ComposeScreen
import org.junit.Rule
import org.junit.Test

@OptIn(ExperimentalTestApi::class)
class ComposeAppTest {
@get:Rule
val composeTestRule = createAndroidComposeRule<MainActivity>()
Expand All @@ -18,10 +20,15 @@ class ComposeAppTest {
}
}
ComposeScreen.onComposeScreen<CoffeeListScreen>(composeTestRule) {
// composeTestRule.onRoot().printToLog("TEST_")
assertIsDisplayed()
coffeeList.assertLengthEquals(42)
// CappuccinoItem.assertIsDisplayed()
// LatteItem.assertIsDisplayed()
coffeeList.assertLengthEquals(12)
coffeeList.childAt<CoffeeItemNode>(0) {
title.assertTextEquals("Cappuccino")
}
coffeeList.childAt<CoffeeItemNode>(1) {
title.assertTextEquals("Latte")
}
}
}
}
3 changes: 2 additions & 1 deletion app/src/main/java/ru/beryukhov/coffeegram/PagesContent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
Expand Down Expand Up @@ -97,7 +98,7 @@ fun PagesContent(
}, snackbarHost = {
SnackbarHost(hostState = snackbarHostState)
}) {
Column(modifier = Modifier.padding(it)) {
Column(modifier = Modifier.padding(it).testTag(currentNavigationState.mapTestTag())) {
Spacer(
Modifier
.padding(top = topPadding)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ sealed interface NavigationIntent {
}

sealed interface NavigationState {
fun mapTestTag(): String = when (this) {
is CoffeeListPage -> "CoffeeListScreen"
MapPage -> "MapScreen"
SettingsPage -> "SettingsScreen"
is TablePage -> "TableScreen"
}

class TablePage(val yearMonth: YearMonth) : NavigationState
data class CoffeeListPage(val date: LocalDate) : NavigationState
object SettingsPage : NavigationState
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fun CoffeeListPage(
coffeeItems = coffeeListViewModel.getDayCoffeesWithEmpty(localDate).toPersistentList(),
onPlusClick = onPlusClick,
onMinusClick = onMinusClick,
modifier = modifier.testTag("CoffeeListScreen")
modifier = modifier
)
}

Expand All @@ -99,8 +99,8 @@ private fun CoffeeList(
LazyColumn(
modifier = modifier
.fillMaxHeight()
.testTag("CoffeeList")
.lazyListLength(coffeeItems.size)
.testTag("CoffeeList")
) {
itemsIndexed(items = coffeeItems) { index, model ->
val (type, count) = model
Expand Down

0 comments on commit 87a8534

Please sign in to comment.