Skip to content

Commit

Permalink
Ensure ModalBottomSheet always contains Hidden detent when created
Browse files Browse the repository at this point in the history
Fixes #39
  • Loading branch information
alexstyl committed Oct 29, 2024
1 parent a0ad4da commit 561ee0a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 36 deletions.
2 changes: 1 addition & 1 deletion core/src/commonMain/kotlin/ModalBottomSheet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
56 changes: 32 additions & 24 deletions core/src/jvmTest/kotlin/BottomSheetTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}
}
Expand All @@ -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))
}
}
Expand All @@ -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) {
Expand All @@ -83,8 +74,7 @@ class BottomSheetTests {
}

val state = rememberBottomSheetState(
initialDetent = SheetDetent.FullyExpanded,
decayAnimationSpec = rememberSplineBasedDecay()
initialDetent = SheetDetent.FullyExpanded, decayAnimationSpec = rememberSplineBasedDecay()
)

BottomSheet(state, Modifier.testTag("sheet")) {
Expand All @@ -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)
)
}
}
}
28 changes: 17 additions & 11 deletions core/src/jvmTest/kotlin/ModalBottomSheetTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}"
}
}
}
}

0 comments on commit 561ee0a

Please sign in to comment.