From 561ee0a2e14ec3cc28472ca1edd7b29e890cdd43 Mon Sep 17 00:00:00 2001 From: alexstyl <1665273+alexstyl@users.noreply.github.com> Date: Tue, 29 Oct 2024 20:56:17 +0700 Subject: [PATCH] Ensure ModalBottomSheet always contains Hidden detent when created Fixes https://github.com/composablehorizons/compose-unstyled/issues/39 --- .../src/commonMain/kotlin/ModalBottomSheet.kt | 2 +- core/src/jvmTest/kotlin/BottomSheetTests.kt | 56 +++++++++++-------- .../jvmTest/kotlin/ModalBottomSheetTests.kt | 28 ++++++---- 3 files changed, 50 insertions(+), 36 deletions(-) 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}" + } + } + } }