Sivan
12/16/2022, 12:30 PMModalBottomSheetLayout
but I am not sure if the assertions are working right. All I am trying to do is to call a button click which opens up the sheet and asserting the sheetState to see if it is visible.
Every assertion is failing for some reason.
Code and logs in the thread ⬇️Sivan
12/16/2022, 12:32 PM@OptIn(ExperimentalMaterialApi::class)
@Before
fun setup() {
composeTestRule.setContent {
sheetState = rememberModalBottomSheetState(
initialValue = ModalBottomSheetValue.Hidden,
confirmStateChange = { false }
)
scope = rememberCoroutineScope()
Theme {
PrimaryButtonMedium(
modifier = Modifier.fillMaxWidth().semantics {
testTag = "open_button"
},
buttonText = "Open",
onClick = {
scope.launch {
sheetState.show()
}
}
)
ModalBottomSheet(
sheetState = sheetState,
onDismiss = {
scope.launch {
sheetState.hide()
}
},
sheetBody = {
SheetBody(modifier = Modifier.height(250.dp).padding(dimen_24).semantics {
testTag = "sheet_body"
})
}
)
}
}
}
@OptIn(ExperimentalMaterialApi::class)
@Test
fun `open up the ModalBottomSheet on button click`() {
composeTestRule.onNodeWithTag("open_button").performClick()
runTest { assert(sheetState.isVisible) }
println("State : ${sheetState.currentValue}")
}
The print statement always tells me that the sheetState
is Hidden
and the logs give me the following error
java.lang.AssertionError: Assertion failed
I am not sure if I am missing something here. Any help is appreciated
Thanks 🙂dorche
12/16/2022, 2:24 PMSivan
12/19/2022, 5:32 AMSivan
12/25/2022, 7:54 AMprivate val restorationTester = StateRestorationTester(composeTestRule)
Adding a StateRestorationTester was the missing link. Notes from the doc 👇
Instead of calling ComposeContentTestRule.setContent you need to use setContent on this object, then change your state so there is some change to be restored, then execute emulateSavedInstanceStateRestore and assert your state is restored properly.
Example :
composeTestRule.onNodeWithTag(dismissButtonTag).performClick()
emulateSavedInstanceStateRestore()
assert(!sheetState.isVisible)