I have an app that has a composable MyCard()
.
I am testing the app in myTest()
and would like to simulate a swipeRight
gesture on the card.
When I use performTouchInput { swipeRight() }
nothing happens. The UI does not update and card stays in the same place.
How can I simulate swipe right gesture on the card? What am I missing?
Desired Result
Code
@OptIn(ExperimentalCoroutinesApi::class)
class MyTest {
@get:Rule
val composeRule = createComposeRule()
@Before
fun setUp() {
composeRule.setContent {
MyCard()
}
}
@Test
fun myTest() = runTest {
composeRule.onNodeWithTag("DraggableCard")
.performTouchInput { swipeRight() }
}
}
@SuppressLint("UnusedTransitionTargetStateParameter")
@Composable
fun MyCard() {
var swipeState by remember { mutableStateOf(false) }
val transitionState = remember {
MutableTransitionState(swipeState).apply { targetState = !swipeState }
}
val transition = updateTransition(transitionState, "cardTransition")
val offsetTransition by transition.animateFloat(
label = "cardOffsetTransition",
transitionSpec = { tween(durationMillis = 300) },
targetValueByState = { if (swipeState) 75f else 0f },)
Card(
modifier = Modifier
.testTag("DraggableCard")
.fillMaxWidth()
.height(35.dp)
.padding(horizontal = 4.dp, vertical = 1.dp)
.offset { IntOffset(offsetTransition.roundToInt(), 0) }
.pointerInput(Unit) {
detectHorizontalDragGestures { _, dragAmount ->
when {
dragAmount >= 6 -> { swipeState = true }
dragAmount < -6 -> { swipeState = false }
}
}
},
backgroundColor = Color.Gray,
content = { Text(text = "Hello") }
)
}