Hey folks! Trying to test a `ModalBottomSheetLayout` but I am not sure if the assertions are working...
s
Hey folks! Trying to test a
ModalBottomSheetLayout
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 โฌ‡๏ธ
๐Ÿงต 1
Copy code
@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
Copy code
java.lang.AssertionError: Assertion failed
I am not sure if I am missing something here. Any help is appreciated Thanks ๐Ÿ™‚
d
Isn't it because you're rejecting every state change in confirmStateChange?
s
Added that because I wanted do disable dismiss while tapping on the scrim area. Removed the confirm state change parameter and still the tests fail
Copy code
private val restorationTester = StateRestorationTester(composeTestRule)
Adding a StateRestorationTester was the missing link. Notes from the doc ๐Ÿ‘‡
Copy code
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 :
Copy code
composeTestRule.onNodeWithTag(dismissButtonTag).performClick()
        emulateSavedInstanceStateRestore()
        assert(!sheetState.isVisible)