diff --git a/web/compose-compiler-integration/src/jsMain/kotlin/CrossmoduleTestsDependencies.kt b/web/compose-compiler-integration/src/jsMain/kotlin/CrossmoduleTestsDependencies.kt index f0885903a5..24aa6b4dcc 100644 --- a/web/compose-compiler-integration/src/jsMain/kotlin/CrossmoduleTestsDependencies.kt +++ b/web/compose-compiler-integration/src/jsMain/kotlin/CrossmoduleTestsDependencies.kt @@ -1,6 +1,7 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.SideEffect import kotlinx.browser.document +import org.jetbrains.compose.web.dom.Text data class DataClassTakesValComposable(val c: @Composable () -> Unit) data class DataClassTakesValComposableTyped(val c: @Composable (T) -> Unit) @@ -86,3 +87,7 @@ class ClassSavesStringAndComposableIntoVar( var composableVar: @Composable ClassSavesStringAndComposableIntoVar.() -> Unit = c var stringVar: String = s } + +val GlobalComposableLambdaToShowText: @Composable (text: () -> String) -> Unit = { + Text(it()) +} diff --git a/web/compose-compiler-integration/src/jsTest/kotlin/ComposableLambdaCalls.kt b/web/compose-compiler-integration/src/jsTest/kotlin/ComposableLambdaCalls.kt new file mode 100644 index 0000000000..4522dbc1b6 --- /dev/null +++ b/web/compose-compiler-integration/src/jsTest/kotlin/ComposableLambdaCalls.kt @@ -0,0 +1,141 @@ +import androidx.compose.runtime.Composable +import kotlinx.browser.document +import org.jetbrains.compose.web.dom.Div +import org.jetbrains.compose.web.dom.Text +import org.jetbrains.compose.web.renderComposableInBody +import kotlin.test.Test +import kotlin.test.assertEquals + +class ComposableLambdaCalls { + + @Composable + private fun ComposableSomeText(someText : () -> String) { + Text(someText()) + } + + private fun someText(): String { + return "SomeText" + } + + @Test + fun passingFunctionReference() { + renderComposableInBody { + Div { + ComposableSomeText(::someText) + } + } + + assertEquals("
SomeText
", document.body!!.firstElementChild!!.outerHTML) + } + + @Test + fun passingAnonymousLambda() { + renderComposableInBody { + Div { + ComposableSomeText { "Text1" } + } + } + + assertEquals("
Text1
", document.body!!.firstElementChild!!.outerHTML) + } + + @Test + fun callingComposableLambdaWithoutArguments() { + val l: @Composable () -> Unit = { + Text("TextA") + } + + renderComposableInBody { + Div { + l() + } + } + + assertEquals("
TextA
", document.body!!.firstElementChild!!.outerHTML) + } + + @Test + fun invokingNullComposableLambdaWithoutArguments() { + val l: (@Composable () -> Unit)? = null + + renderComposableInBody { + Div { + l?.invoke() + } + } + + assertEquals("
", document.body!!.firstElementChild!!.outerHTML) + } + + @Test + fun invokingNullComposableLambdaWithArguments() { + val l: (@Composable (Int) -> Unit)? = null + + var someIntInvoked = false + + fun someInt(): Int { + someIntInvoked = true + return 10 + } + + renderComposableInBody { + Div { + l?.invoke(someInt()) + } + } + + assertEquals("
", document.body!!.firstElementChild!!.outerHTML) + assertEquals(false, someIntInvoked, message = "someInt() should never be invoked as `l` is null") + } + + @Test + fun invokingComposableLambdaWithArguments() { + val l: (@Composable (Int) -> Unit) = { + Text("Text$it") + } + + var someIntInvoked = false + + fun someInt(): Int { + someIntInvoked = true + return 10 + } + + renderComposableInBody { + Div { + l.invoke(someInt()) + } + } + + assertEquals("
Text10
", document.body!!.firstElementChild!!.outerHTML) + assertEquals(true, someIntInvoked) + } + + @Test + fun invokingComposableLambdaWithFunctionReferenceAsArgument() { + val l: (@Composable (() -> String) -> Unit) = { + Text("Text-${it()}") + } + + renderComposableInBody { + Div { + l.invoke(::someText) + } + } + + assertEquals("
Text-SomeText
", document.body!!.firstElementChild!!.outerHTML) + } + + @Test + fun invokingComposableLambdaFromAnotherModule() { + renderComposableInBody { + Div { + GlobalComposableLambdaToShowText { + "SuperText" + } + } + } + + assertEquals("
SuperText
", document.body!!.firstElementChild!!.outerHTML) + } +} diff --git a/web/gradle.properties b/web/gradle.properties index 62265c53a8..c1ff05201d 100644 --- a/web/gradle.properties +++ b/web/gradle.properties @@ -1,6 +1,6 @@ # __LATEST_COMPOSE_RELEASE_VERSION__ -COMPOSE_CORE_VERSION=1.0.0-alpha4-build328 -COMPOSE_WEB_VERSION=1.0.0-alpha4-build328 +COMPOSE_CORE_VERSION=1.0.0-alpha4-build348 +COMPOSE_WEB_VERSION=1.0.0-alpha4-build348 compose.web.buildSamples=false compose.web.tests.integration.withFirefox compose.web.tests.skip.benchmarks=false