Skip to content

Commit

Permalink
feat: compose can pop check variable
Browse files Browse the repository at this point in the history
  • Loading branch information
programadorthi committed Jun 7, 2024
1 parent d62a16e commit 1d345c3
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ internal expect fun Routing.popOnPlatform(
fallback: () -> Unit,
)

public expect val Routing.canPop: Boolean

public fun Routing.pop(result: Any? = null) {
popOnPlatform(result = result) {
if (callStack.size < 2) return@popOnPlatform
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.advanceTimeBy
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue

@OptIn(ExperimentalCoroutinesApi::class)
class ComposeRoutingTest {
Expand Down Expand Up @@ -46,6 +48,7 @@ class ComposeRoutingTest {

// THEN
assertEquals("I'm the initial content", fakeContent.result)
assertFalse(routing.canPop, "pop having one call only isn't valid")
}

@Test
Expand Down Expand Up @@ -83,6 +86,7 @@ class ComposeRoutingTest {

// THEN
assertEquals("I'm the path based content", fakeContent.result)
assertTrue(routing.canPop, "pop should be available having more than one call")
}

@Test
Expand Down Expand Up @@ -311,6 +315,7 @@ class ComposeRoutingTest {
assertEquals("", "${result?.name}")
assertEquals(RouteMethod.ReplaceAll, result?.routeMethod)
assertEquals(Parameters.Empty, result?.parameters)
assertFalse(routing.canPop, "replace all should turn can pop false")
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ class ComposeHistoryAttributeTest {
fun shouldUseMemoryHistoryMode() =
runComposeTest { coroutineContext, composition, clock ->
// GIVEN
val routing = routing(parentCoroutineContext = coroutineContext) {
composable(path = "/initial") {
val routing =
routing(parentCoroutineContext = coroutineContext) {
composable(path = "/initial") {
}
}
}

// WHEN
composition.setContent {
Expand All @@ -43,10 +44,11 @@ class ComposeHistoryAttributeTest {
fun shouldUseHtml5HistoryMode() =
runComposeTest { coroutineContext, composition, clock ->
// GIVEN
val routing = routing(parentCoroutineContext = coroutineContext) {
composable(path = "/initial") {
val routing =
routing(parentCoroutineContext = coroutineContext) {
composable(path = "/initial") {
}
}
}

// WHEN
composition.setContent {
Expand All @@ -66,10 +68,11 @@ class ComposeHistoryAttributeTest {
fun shouldUseHashHistoryMode() =
runComposeTest { coroutineContext, composition, clock ->
// GIVEN
val routing = routing(parentCoroutineContext = coroutineContext) {
composable(path = "/initial") {
val routing =
routing(parentCoroutineContext = coroutineContext) {
composable(path = "/initial") {
}
}
}

// WHEN
composition.setContent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ internal actual fun Routing.popOnPlatform(
}
}
}

public actual val Routing.canPop: Boolean
get() = historyMode != ComposeHistoryMode.Memory || callStack.size > 1
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ internal actual fun Routing.popOnPlatform(
) {
fallback()
}

public actual val Routing.canPop: Boolean
get() = callStack.size > 1
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ internal actual fun Routing.popOnPlatform(
) {
fallback()
}

public actual val Routing.canPop: Boolean
get() = callStack.size > 1

0 comments on commit 1d345c3

Please sign in to comment.