Skip to content
This repository has been archived by the owner on Aug 10, 2024. It is now read-only.

Commit

Permalink
ValueElement.value wasn't propagating KVar changes from server to bro…
Browse files Browse the repository at this point in the history
…wser (#418)
  • Loading branch information
sanity authored Dec 23, 2022
1 parent 0dfdb56 commit 54a9879
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/main/kotlin/kweb/prelude.kt
Original file line number Diff line number Diff line change
Expand Up @@ -604,16 +604,27 @@ abstract class ValueElement(
this.creator?.onCleanup(true) {
value.close(CloseReason("Parent element closed"))
}
attachListeners(value)
}
}
return _valueKvar!!
}
set(v) {
if (_valueKvar != null) error("`value` may only be set once, and cannot be set after it has been retrieved")
updateKVar(v, updateOn = kvarUpdateEvent)
attachListeners(v)
setValue(v.value)
_valueKvar = v
}

private fun attachListeners(kv : KVar<String>) {
val handle = kv.addListener { _, newValue ->
setValue(newValue)
}
element.creator?.onCleanup(true) {
kv.removeListener(handle)
}
}

/**
* Automatically update `toBind` with the value of this INPUT element when `updateOn` event occurs.
Expand Down
18 changes: 16 additions & 2 deletions src/test/kotlin/kweb/StringDiffTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import io.github.bonigarcia.seljup.SeleniumJupiter
import io.kotest.matchers.shouldBe
import kweb.*
import kweb.state.KVar
import org.awaitility.Awaitility
import org.awaitility.Awaitility.await
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
Expand All @@ -20,7 +22,7 @@ class StringDiffTest(@Arguments("--headless") unprotectedDriver: ChromeDriver) {

//ThreadGuard.protect ensures that the ChromeDriver can only be called by the thread that created it
//This should make this test thread safe.
val driver : WebDriver = ThreadGuard.protect(unprotectedDriver)
val driver: WebDriver = ThreadGuard.protect(unprotectedDriver)

companion object {
private lateinit var stringDiffTestApp: StringDiffTestApp
Expand Down Expand Up @@ -89,10 +91,22 @@ class StringDiffTest(@Arguments("--headless") unprotectedDriver: ChromeDriver) {
inputField.sendKeys(Keys.ENTER)
inputField.getAttribute("value") shouldBe "Lazy Brown"
}

@Test
fun modifyTextOnServerAndVerifyChangeInBrowser() {
driver.get("http://localhost:7660/")
val inputField = driver.findElement(By.tagName("input"))
val currentValue = stringDiffTestApp.inputString.value
stringDiffTestApp.inputString.value = "Super Fox"
await().pollInSameThread().untilAsserted {
inputField.getAttribute("value") shouldBe "Super Fox"
}
stringDiffTestApp.inputString.value = currentValue
}
}

class StringDiffTestApp {
var inputString = KVar("")
var inputString = KVar("Initial")

val server: Kweb = Kweb(port = 7660) {
doc.body {
Expand Down

0 comments on commit 54a9879

Please sign in to comment.