From ce9fbcc8dd62224be87f242602c6349770d61878 Mon Sep 17 00:00:00 2001 From: Shagen Ogandzhanian Date: Wed, 25 Aug 2021 12:09:12 +0200 Subject: [PATCH] Add test that basically check that scope is remembered Thanks to Oleksandr - I would have never come up with a solution this quick --- .../jsTest/kotlin/elements/AttributesTests.kt | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/web/core/src/jsTest/kotlin/elements/AttributesTests.kt b/web/core/src/jsTest/kotlin/elements/AttributesTests.kt index 5ae91273e2..dfc7fc0078 100644 --- a/web/core/src/jsTest/kotlin/elements/AttributesTests.kt +++ b/web/core/src/jsTest/kotlin/elements/AttributesTests.kt @@ -3,6 +3,8 @@ package org.jetbrains.compose.web.core.tests import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.setValue +import kotlinx.browser.document +import kotlinx.dom.clear import org.jetbrains.compose.web.attributes.AttrsBuilder import org.jetbrains.compose.web.attributes.disabled import org.jetbrains.compose.web.attributes.forId @@ -334,6 +336,42 @@ class AttributesTests { assertEquals(0, refDisposeCounter) } + @Test + fun disposableRefEffectWithChangingKey() = runTest { + var key by mutableStateOf(0) + + composition { + val readKey = key // read key here to recompose an entire scope + Div( + attrs = { + id("id$readKey") + } + ) { + DisposableRefEffect(readKey) { + val p = document.createElement("p").also { it.innerHTML = "Key=$readKey" } + it.appendChild(p) + + onDispose { + it.clear() + } + } + } + } + + assertEquals( + expected = "

Key=0

", + actual = root.outerHTML + ) + + key = 1 + waitForRecompositionComplete() + + assertEquals( + expected = "

Key=1

", + actual = root.outerHTML + ) + } + @Test // issue: https://github.com/JetBrains/compose-jb/issues/981 fun attributesUpdateShouldNotCauseInlineStylesCleanUp() = runTest { var hasValue by mutableStateOf(false)