Skip to content

Commit

Permalink
Add molecule test
Browse files Browse the repository at this point in the history
  • Loading branch information
takahirom committed Apr 9, 2024
1 parent fd2e1e2 commit 9eba3a3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ androidx-compose-material-icons = { module = "androidx.compose.material:material
androidx-compose-material-material = { module = "org.jetbrains.compose.material:material", version.ref = "compose-material" }
androidx-compose-integration-activity = { module = "androidx.activity:activity-compose", version.ref = "androidx-activity" }
androidx-core = "androidx.core:core-ktx:1.12.0"
molecule-runtime = { module = "app.cash.molecule:molecule-runtime", version = "1.4.2" }

# test
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
Expand Down
1 change: 1 addition & 0 deletions rin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ kotlin {
implementation(compose.dependencies.uiTest)
implementation(libs.jetbrains.androidx.navigation.compose)
implementation(compose.dependencies.material3)
implementation(libs.molecule.runtime)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion rin/src/commonMain/kotlin/io/github/takahirom/rin/Rin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.withContext
import kotlin.coroutines.EmptyCoroutineContext

var RIN_DEBUG = true
var RIN_DEBUG = false

private class ProduceRetainedStateScopeImpl<T>(
state: MutableState<T>,
Expand Down
43 changes: 40 additions & 3 deletions rin/src/iosTest/kotlin/IosNavigationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,62 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.*
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import androidx.lifecycle.*
import kotlin.test.Test
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import app.cash.molecule.RecompositionMode
import app.cash.molecule.launchMolecule
import io.github.takahirom.rin.RetainedObserver
import io.github.takahirom.rin.RinViewModel
import io.github.takahirom.rin.rememberRetained
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlin.test.assertEquals

private const val TAG_REMEMBER = "remember"
private const val TAG_RETAINED_1 = "retained1"

class IosNavigationTest {
@OptIn(InternalComposeApi::class)
@Test
fun test() {
assertEquals(
expected = "test",
actual = CoroutineScope(Job()).launchMolecule(RecompositionMode.Immediate) {
val nestedRegistry = remember {
object : ViewModelStoreOwner {
override val viewModelStore: ViewModelStore = ViewModelStore()
}
}
val lifecycleRegistry = remember {
object : LifecycleOwner {
override val lifecycle: Lifecycle = LifecycleRegistry(this)
}
}
CompositionLocalProviderWithReturnValue(LocalViewModelStoreOwner provides nestedRegistry) {
CompositionLocalProviderWithReturnValue(LocalLifecycleOwner provides lifecycleRegistry) {
var retainedText: String by rememberRetained { mutableStateOf("test") }
retainedText
}
}
}.value
)
}
@Composable
@OptIn(InternalComposeApi::class)
fun <T> CompositionLocalProviderWithReturnValue(
value: ProvidedValue<*>,
content: @Composable () -> T,
): T {
currentComposer.startProvider(value)
val result = content()
currentComposer.endProvider()
return result
}

@Composable
private fun KeyContent(key: String?) {
Expand Down

0 comments on commit 9eba3a3

Please sign in to comment.