Browse Source

Add InheritanceTests with Composables for k/js (#2666)

relerelease/1.3
Oleksandr Karpovich 2 years ago committed by GitHub
parent
commit
5d18d0a79d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      web/compose-compiler-integration/build.gradle.kts
  2. 27
      web/compose-compiler-integration/lib/build.gradle.kts
  3. 23
      web/compose-compiler-integration/lib/src/commonMain/kotlin/Interfaces.kt
  4. 43
      web/compose-compiler-integration/src/jsMain/kotlin/CrossmoduleTestsDependencies.kt
  5. 86
      web/compose-compiler-integration/src/jsTest/kotlin/InheritanceTests.kt
  6. 1
      web/settings.gradle.kts

1
web/compose-compiler-integration/build.gradle.kts

@ -21,6 +21,7 @@ kotlin {
sourceSets {
val jsMain by getting {
dependencies {
implementation(project(":compose-compiler-integration-lib"))
implementation(kotlin("stdlib-js"))
implementation(compose.runtime)
implementation(project(":web-core"))

27
web/compose-compiler-integration/lib/build.gradle.kts

@ -0,0 +1,27 @@
plugins {
kotlin("multiplatform")
id("org.jetbrains.compose")
}
repositories {
mavenLocal()
mavenCentral()
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
}
kotlin {
js(IR) {
nodejs {}
browser() {}
}
sourceSets {
val commonMain by getting {
dependencies {
implementation(kotlin("stdlib-common"))
implementation(compose.runtime)
implementation(project(":web-core"))
}
}
}
}

23
web/compose-compiler-integration/lib/src/commonMain/kotlin/Interfaces.kt

@ -0,0 +1,23 @@
import androidx.compose.runtime.Composable
import org.jetbrains.compose.web.dom.Div
import org.jetbrains.compose.web.dom.Text
interface ComposableCollection {
val list: List<@Composable () -> Unit>
fun add(c: @Composable () -> Unit)
}
interface ComposableContent {
@Composable
fun ComposableContent()
}
abstract class AbstrComposableContent : ComposableContent {
@Composable
override fun ComposableContent() {
Div { Text("AbstrComposableContent") }
}
}

43
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.Div
import org.jetbrains.compose.web.dom.Text
data class DataClassTakesValComposable(val c: @Composable () -> Unit)
@ -105,3 +106,45 @@ sealed interface StableSealedInterface {
data class StableDataClass(val abc: Int)
data class StableClass(val abc: Int)
class StableTypedClass<T>(val abc: T)
class ComposableCollectionImpl : ComposableCollection {
private val _list = mutableListOf<@Composable () -> Unit>()
override val list: List<@Composable () -> Unit>
get() = _list
override fun add(c: @Composable () -> Unit) {
_list.add(c)
}
}
internal fun createComposableCollection(): ComposableCollection = ComposableCollectionImpl()
private class ComposableContentImpl : ComposableContent {
@Composable
override fun ComposableContent() {
Div { Text("ComposableContent") }
}
}
internal fun createComposableContent(): ComposableContent = ComposableContentImpl()
internal class ComposableContentDelegation(private val impl: ComposableContent) : ComposableContent by impl
class AbstrComposableContentExtendImpl : AbstrComposableContent() {
@Composable
override fun ComposableContent() {
super.ComposableContent()
Div {
Text("AbstrComposableContentImpl")
}
}
}
class AbstrComposableContentNoExtendImpl : AbstrComposableContent()

86
web/compose-compiler-integration/src/jsTest/kotlin/InheritanceTests.kt

@ -0,0 +1,86 @@
import kotlinx.browser.document
import org.jetbrains.compose.web.dom.Div
import org.jetbrains.compose.web.dom.Text
import org.jetbrains.compose.web.renderComposable
import kotlin.test.Test
import kotlin.test.assertEquals
class InheritanceTests {
@Test
// Issues:
// https://github.com/JetBrains/compose-jb/issues/2291
// https://github.com/JetBrains/compose-jb/issues/2660
fun implementComposableCollection() {
val collection = createComposableCollection().apply {
add {
Div { Text("Div1") }
}
add {
Div { Text("Div2") }
}
add {
Div { Text("Div3") }
}
}
val root = document.createElement("div")
renderComposable(root) {
collection.list.forEach { it() }
}
assertEquals("<div>Div1</div><div>Div2</div><div>Div3</div>", root.innerHTML)
}
@Test
fun implementComposableContent() {
val contentImpl = createComposableContent()
val root = document.createElement("div")
renderComposable(root) {
contentImpl.ComposableContent()
}
assertEquals("<div>ComposableContent</div>", root.innerHTML)
}
@Test
fun implementComposableContentDelegation() {
val contentImpl = ComposableContentDelegation(createComposableContent())
val root = document.createElement("div")
renderComposable(root) {
contentImpl.ComposableContent()
}
assertEquals("<div>ComposableContent</div>", root.innerHTML)
}
@Test
fun testAbstrComposableContentExtendImpl() {
val contentImpl: ComposableContent = AbstrComposableContentExtendImpl()
val root = document.createElement("div")
renderComposable(root) {
contentImpl.ComposableContent()
}
assertEquals("<div>AbstrComposableContent</div><div>AbstrComposableContentImpl</div>", root.innerHTML)
}
@Test
fun testAbstrComposableContentNoExtendImpl() {
val contentImpl: ComposableContent = AbstrComposableContentNoExtendImpl()
val root = document.createElement("div")
renderComposable(root) {
contentImpl.ComposableContent()
}
assertEquals("<div>AbstrComposableContent</div>", root.innerHTML)
}
}

1
web/settings.gradle.kts

@ -62,6 +62,7 @@ module(":web-core", "core")
module(":web-svg", "svg")
module(":web-integration-core", "integration-core")
module(":compose-compiler-integration", "compose-compiler-integration")
module(":compose-compiler-integration-lib", "compose-compiler-integration/lib")
module(":internal-web-core-runtime", "internal-web-core-runtime")
module(":test-utils", "test-utils")

Loading…
Cancel
Save