From d68c432506640823cf81a588b8a422b7dd932108 Mon Sep 17 00:00:00 2001 From: Oleksandr Karpovich Date: Wed, 15 Sep 2021 11:52:11 +0200 Subject: [PATCH] web: add tests for ComposableLambdaCalls.kt (#1121) * web: add tests for ComposableLambdaCalls.kt * web update compose version 1.0.0-alpha4-build348 Co-authored-by: Oleksandr Karpovich --- .../kotlin/CrossmoduleTestsDependencies.kt | 5 + .../jsTest/kotlin/ComposableLambdaCalls.kt | 141 ++++++++++++++++++ web/gradle.properties | 4 +- 3 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 web/compose-compiler-integration/src/jsTest/kotlin/ComposableLambdaCalls.kt 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