diff --git a/core/src/commonMain/kotlin/ModalBottomSheet.kt b/core/src/commonMain/kotlin/ModalBottomSheet.kt index 9ddae2c..65f082a 100644 --- a/core/src/commonMain/kotlin/ModalBottomSheet.kt +++ b/core/src/commonMain/kotlin/ModalBottomSheet.kt @@ -41,7 +41,7 @@ public fun rememberModalBottomSheetState( val actualDetents = (setOf(SheetDetent.Hidden) + detents).toList() val sheetState = rememberBottomSheetState( initialDetent = SheetDetent.Hidden, - detents = detents, + detents = actualDetents, animationSpec = animationSpec, velocityThreshold = velocityThreshold, positionalThreshold = positionalThreshold, diff --git a/core/src/jvmTest/kotlin/BottomSheetTests.kt b/core/src/jvmTest/kotlin/BottomSheetTests.kt index a5d35b1..907daf5 100644 --- a/core/src/jvmTest/kotlin/BottomSheetTests.kt +++ b/core/src/jvmTest/kotlin/BottomSheetTests.kt @@ -3,20 +3,10 @@ package com.composables.core import androidx.compose.animation.rememberSplineBasedDecay import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.size -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue +import androidx.compose.runtime.* import androidx.compose.ui.Modifier import androidx.compose.ui.platform.testTag -import androidx.compose.ui.test.ExperimentalTestApi -import androidx.compose.ui.test.assertHeightIsEqualTo -import androidx.compose.ui.test.assertIsDisplayed -import androidx.compose.ui.test.assertIsNotDisplayed -import androidx.compose.ui.test.assertWidthIsEqualTo -import androidx.compose.ui.test.onNodeWithTag -import androidx.compose.ui.test.runComposeUiTest +import androidx.compose.ui.test.* import androidx.compose.ui.unit.dp import kotlin.test.Test import kotlinx.coroutines.delay @@ -27,10 +17,11 @@ class BottomSheetTests { @Test fun sheetWithInitialDetentHidden_isNotDisplayed() = runComposeUiTest { setContent { - BottomSheet(rememberBottomSheetState( - initialDetent = SheetDetent.Hidden, - decayAnimationSpec = rememberSplineBasedDecay() - )) { + BottomSheet( + rememberBottomSheetState( + initialDetent = SheetDetent.Hidden, decayAnimationSpec = rememberSplineBasedDecay() + ) + ) { Box(Modifier.testTag("sheet_contents").size(40.dp)) } } @@ -41,10 +32,11 @@ class BottomSheetTests { @Test fun sheetWithInitialDetentFullyExpanded_isDisplayed() = runComposeUiTest { setContent { - BottomSheet(rememberBottomSheetState( - initialDetent = SheetDetent.FullyExpanded, - decayAnimationSpec = rememberSplineBasedDecay() - )) { + BottomSheet( + rememberBottomSheetState( + initialDetent = SheetDetent.FullyExpanded, decayAnimationSpec = rememberSplineBasedDecay() + ) + ) { Box(Modifier.testTag("sheet_contents").size(40.dp)) } } @@ -57,8 +49,7 @@ class BottomSheetTests { fun settingDetentToFullyDetent_whenInitialIsDetentHidden_isDisplayed() = runComposeUiTest { setContent { val state = rememberBottomSheetState( - initialDetent = SheetDetent.Hidden, - decayAnimationSpec = rememberSplineBasedDecay() + initialDetent = SheetDetent.Hidden, decayAnimationSpec = rememberSplineBasedDecay() ) LaunchedEffect(Unit) { @@ -83,8 +74,7 @@ class BottomSheetTests { } val state = rememberBottomSheetState( - initialDetent = SheetDetent.FullyExpanded, - decayAnimationSpec = rememberSplineBasedDecay() + initialDetent = SheetDetent.FullyExpanded, decayAnimationSpec = rememberSplineBasedDecay() ) BottomSheet(state, Modifier.testTag("sheet")) { @@ -96,4 +86,22 @@ class BottomSheetTests { onNodeWithTag("sheet").assertWidthIsEqualTo(150.dp) onNodeWithTag("sheet").assertHeightIsEqualTo(150.dp) } + + @Test(expected = IllegalStateException::class) + fun creatingStateWithNoDetents_throws_exception() = runComposeUiTest { + setContent { + rememberBottomSheetState( + initialDetent = SheetDetent.FullyExpanded, detents = emptyList() + ) + } + } + + @Test(expected = IllegalStateException::class) + fun creating_stateWithoutInitialDetent_throws_exception() = runComposeUiTest { + setContent { + rememberBottomSheetState( + initialDetent = SheetDetent.FullyExpanded, detents = listOf(SheetDetent.Hidden) + ) + } + } } diff --git a/core/src/jvmTest/kotlin/ModalBottomSheetTests.kt b/core/src/jvmTest/kotlin/ModalBottomSheetTests.kt index 5aee265..2843198 100644 --- a/core/src/jvmTest/kotlin/ModalBottomSheetTests.kt +++ b/core/src/jvmTest/kotlin/ModalBottomSheetTests.kt @@ -2,19 +2,10 @@ package com.composables.core import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.size -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue +import androidx.compose.runtime.* import androidx.compose.ui.Modifier import androidx.compose.ui.platform.testTag -import androidx.compose.ui.test.ExperimentalTestApi -import androidx.compose.ui.test.assertHeightIsEqualTo -import androidx.compose.ui.test.assertIsDisplayed -import androidx.compose.ui.test.assertWidthIsEqualTo -import androidx.compose.ui.test.onNodeWithTag -import androidx.compose.ui.test.runComposeUiTest +import androidx.compose.ui.test.* import androidx.compose.ui.unit.dp import kotlin.test.Test import kotlinx.coroutines.delay @@ -92,4 +83,19 @@ class ModalBottomSheetTests { onNodeWithTag("sheet").assertWidthIsEqualTo(150.dp) onNodeWithTag("sheet").assertHeightIsEqualTo(150.dp) } + + @Test + fun always_adds_hidden_detent() = runComposeUiTest { + setContent { + val sheetState = rememberModalBottomSheetState( + initialDetent = SheetDetent.FullyExpanded, + detents = listOf(SheetDetent.FullyExpanded) + ) + sheetState.currentDetent = SheetDetent.Hidden + + assert(sheetState.currentDetent == SheetDetent.Hidden) { + "Expected currentDetent to be ${SheetDetent.Hidden.identifier} at this point but was ${sheetState.currentDetent.identifier}" + } + } + } }