diff options
author | Robert Jaros <rjaros@finn.pl> | 2019-10-13 00:04:39 +0200 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2019-10-13 00:04:39 +0200 |
commit | 32551e8918b22762c067a895382a4faee53b9003 (patch) | |
tree | e0722fdd5a6c01079cfbf22ccba38a3e21a779b9 /src/test/kotlin | |
parent | 6ea600defefbe16e59c421785d9a09989e672083 (diff) | |
download | kvision-32551e8918b22762c067a895382a4faee53b9003.tar.gz kvision-32551e8918b22762c067a895382a4faee53b9003.tar.bz2 kvision-32551e8918b22762c067a895382a4faee53b9003.zip |
Refactor StateBinding class with ObservableState interface.
Diffstat (limited to 'src/test/kotlin')
-rw-r--r-- | src/test/kotlin/test/pl/treksoft/kvision/state/StateBindingSpec.kt | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/test/kotlin/test/pl/treksoft/kvision/state/StateBindingSpec.kt b/src/test/kotlin/test/pl/treksoft/kvision/state/StateBindingSpec.kt new file mode 100644 index 00000000..f5416748 --- /dev/null +++ b/src/test/kotlin/test/pl/treksoft/kvision/state/StateBindingSpec.kt @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2017-present Robert Jaros + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package test.pl.treksoft.kvision.state + +import pl.treksoft.kvision.html.Div.Companion.div +import pl.treksoft.kvision.panel.Root +import pl.treksoft.kvision.panel.SimplePanel +import pl.treksoft.kvision.state.StateBinding.Companion.stateBinding +import pl.treksoft.kvision.state.StateBinding.Companion.stateUpdate +import pl.treksoft.kvision.state.observableListOf +import test.pl.treksoft.kvision.DomSpec +import kotlin.browser.document +import kotlin.test.Test + +class StateBindingSpec : DomSpec { + + @Test + fun stateBinding() { + run { + val root = Root("test", fixed = true) + val container = SimplePanel() + val observableList = observableListOf(1, 2, 3) + container.stateBinding(observableList) { state -> + state.forEach { + div("$it") + } + } + root.add(container) + val element = document.getElementById("test") + assertEqualsHtml( + "<div><div></div><div>1</div><div>2</div><div>3</div></div>", + element?.innerHTML, + "Should render initial state of the container" + ) + observableList.add(4) + assertEqualsHtml( + "<div><div></div><div>1</div><div>2</div><div>3</div><div>4</div></div>", + element?.innerHTML, + "Should render changed state of the container" + ) + } + } + + @Test + fun stateUpdate() { + run { + val root = Root("test", fixed = true) + val container = SimplePanel() + val observableList = observableListOf(1) + container.stateUpdate(observableList) { state -> + div("${state[0]}") + } updateWith { state, d -> + d.content = "${state[0]}" + } + root.add(container) + val element = document.getElementById("test") + assertEqualsHtml( + "<div><div></div><div>1</div></div>", + element?.innerHTML, + "Should render initial state of the container" + ) + observableList[0] = 2 + assertEqualsHtml( + "<div><div></div><div>2</div></div>", + element?.innerHTML, + "Should render changed state of the container" + ) + } + } + +} |