diff options
Diffstat (limited to 'src/test')
6 files changed, 292 insertions, 8 deletions
diff --git a/src/test/kotlin/test/TestMainApplication.kt b/src/test/kotlin/test/TestMainApplication.kt deleted file mode 100644 index 0f8824f7..00000000 --- a/src/test/kotlin/test/TestMainApplication.kt +++ /dev/null @@ -1,8 +0,0 @@ -package test - -import org.junit.* -import kotlin.test.* -import pl.treksoft.kvision.* - -class TestMainApplication { -}
\ No newline at end of file diff --git a/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt b/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt new file mode 100644 index 00000000..a1a142e7 --- /dev/null +++ b/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt @@ -0,0 +1,48 @@ +package test.pl.treksoft.kvision + +import org.w3c.dom.Element +import org.w3c.dom.HTMLElement +import pl.treksoft.kvision.core.Root +import pl.treksoft.kvision.core.Widget +import kotlin.browser.document + +interface TestSpec { + fun beforeTest() + + fun afterTest() + + fun run(code: () -> Unit) { + beforeTest() + code() + afterTest() + } +} + +interface DomSpec : TestSpec { + + override fun beforeTest() { + var fixture = "<div style=\"display: none\"><div id=\"test\"></div></div>"; + document.body?.insertAdjacentHTML("afterbegin", fixture); + } + + override fun afterTest() { + val div = document.getElementById("test") + div?.remove() + } + +} + +interface WSpec : DomSpec { + + fun runW(code: (widget: Widget, element: Element?) -> Unit) { + beforeTest() + val root = Root("test") + val widget = Widget() + widget.id = "test_id" + root.add(widget) + val element = document.getElementById("test_id") + code(widget, element) + afterTest() + } + +}
\ No newline at end of file diff --git a/src/test/kotlin/test/pl/treksoft/kvision/core/ContainerSpec.kt b/src/test/kotlin/test/pl/treksoft/kvision/core/ContainerSpec.kt new file mode 100644 index 00000000..f79d24df --- /dev/null +++ b/src/test/kotlin/test/pl/treksoft/kvision/core/ContainerSpec.kt @@ -0,0 +1,68 @@ +package test.pl.treksoft.kvision.core + +import pl.treksoft.kvision.core.Container +import pl.treksoft.kvision.core.Root +import pl.treksoft.kvision.core.Widget +import test.pl.treksoft.kvision.DomSpec +import kotlin.browser.document +import kotlin.test.Test +import kotlin.test.assertTrue + +class ContainerSpec : DomSpec { + + @Test + fun add() { + run { + val root = Root("test") + val container = Container() + val child1 = Widget() + child1.id = "child1" + val child2 = Widget() + child2.id = "child2" + container.add(child1) + container.add(child2) + root.add(container) + val elem1 = document.getElementById("child1") + val elem2 = document.getElementById("child2") + assertTrue("Container renders children") { elem1 != null && elem2 != null } + } + } + + @Test + fun remove() { + run { + val root = Root("test") + val container = Container() + val child1 = Widget() + child1.id = "child1" + val child2 = Widget() + child2.id = "child2" + container.add(child1) + container.add(child2) + root.add(container) + container.remove(child2) + val elem1 = document.getElementById("child1") + val elem2 = document.getElementById("child2") + assertTrue("Container renders children") { elem1 != null && elem2 == null } + } + } + + @Test + fun removeAt() { + run { + val root = Root("test") + val container = Container() + val child1 = Widget() + child1.id = "child1" + val child2 = Widget() + child2.id = "child2" + container.add(child1) + container.add(child2) + root.add(container) + container.removeAt(0) + val elem1 = document.getElementById("child1") + val elem2 = document.getElementById("child2") + assertTrue("Container renders children") { elem1 == null && elem2 != null } + } + } +}
\ No newline at end of file diff --git a/src/test/kotlin/test/pl/treksoft/kvision/core/KVManagerSpec.kt b/src/test/kotlin/test/pl/treksoft/kvision/core/KVManagerSpec.kt new file mode 100644 index 00000000..d123cf310 --- /dev/null +++ b/src/test/kotlin/test/pl/treksoft/kvision/core/KVManagerSpec.kt @@ -0,0 +1,58 @@ +package test.pl.treksoft.kvision.core + +import com.github.snabbdom.h +import pl.treksoft.kvision.core.KVManager +import pl.treksoft.kvision.snabbdom.snAttrs +import pl.treksoft.kvision.snabbdom.snOpt +import pl.treksoft.kvision.snabbdom.snStyle +import test.pl.treksoft.kvision.DomSpec +import kotlin.browser.document +import kotlin.test.Test +import kotlin.test.assertTrue + +class KVManagerSpec : DomSpec { + + @Test + fun patch_ById() { + run { + val vnode = h("span", snOpt { + attrs = snAttrs("id" to "test_new") + style = snStyle("fontWeight" to "bold", "fontStyle" to "italic") + }) + KVManager.patch("test", vnode) + assertTrue("Original container should not exist") { document.getElementById("test") == null } + assertTrue("New container should exist") { document.getElementById("test_new") != null } + } + } + + @Test + fun patch_ByVnode() { + run { + val vnode1 = h("span", snOpt { + attrs = snAttrs("id" to "test2") + style = snStyle("fontWeight" to "bold", "fontStyle" to "italic") + }) + val vnode2 = KVManager.patch("test", vnode1) + val vnode3 = h("span", snOpt { + attrs = snAttrs("id" to "test3") + style = snStyle("fontWeight" to "bold", "fontStyle" to "italic") + }) + KVManager.patch(vnode2, vnode3) + assertTrue("First container should not exist") { document.getElementById("test") == null } + assertTrue("Second container should not exist") { document.getElementById("test2") == null } + assertTrue("Third container should exist") { document.getElementById("test3") != null } + } + } + + @Test + fun virtualize() { + run { + val node = KVManager.virtualize("<div id=\"virtual\"><p>Virtual node</p></div>") + KVManager.patch("test", node) + assertTrue("Original container should not exist") { document.getElementById("test") == null } + val v = document.getElementById("virtual") + assertTrue("New container should exist") { v != null } + assertTrue("New container should have one child") { v?.children?.length == 1 } + } + } +} diff --git a/src/test/kotlin/test/pl/treksoft/kvision/core/RootSpec.kt b/src/test/kotlin/test/pl/treksoft/kvision/core/RootSpec.kt new file mode 100644 index 00000000..b7435e73 --- /dev/null +++ b/src/test/kotlin/test/pl/treksoft/kvision/core/RootSpec.kt @@ -0,0 +1,29 @@ +package test.pl.treksoft.kvision.core + +import pl.treksoft.kvision.core.Root +import test.pl.treksoft.kvision.DomSpec +import kotlin.browser.document +import kotlin.test.Test +import kotlin.test.assertTrue + +class RootSpec : DomSpec { + + @Test + fun getSnClass() { + run { + val root = Root("test") + val rootElem = document.getElementById("test") + assertTrue("Standard root container has correct css class") { rootElem?.className == "container" } + } + } + + @Test + fun getSnClass_Fluid() { + run { + val root = Root("test", fluid = true) + val rootElem = document.getElementById("test") + assertTrue("Fluid root container has correct css class") { rootElem?.className == "container-fluid" } + } + } + +}
\ No newline at end of file diff --git a/src/test/kotlin/test/pl/treksoft/kvision/core/WidgetSpec.kt b/src/test/kotlin/test/pl/treksoft/kvision/core/WidgetSpec.kt new file mode 100644 index 00000000..214ee1f5 --- /dev/null +++ b/src/test/kotlin/test/pl/treksoft/kvision/core/WidgetSpec.kt @@ -0,0 +1,89 @@ +package test.pl.treksoft.kvision.core + +import test.pl.treksoft.kvision.WSpec +import kotlin.browser.document +import kotlin.test.Test +import kotlin.test.assertTrue + +class WidgetSpec : WSpec { + + @Test + fun render() { + runW { widget, element -> + widget.addCssClass("testClass") + widget.title = "test_title" + val vnode = widget.render() + assertTrue("VNode has correct class attribute") { vnode.data.`class`.testClass == true } + assertTrue("VNode has correct id attribute") { vnode.data.attrs.id == "test_id" } + assertTrue("VNode has correct title attribute") { vnode.data.attrs.title == "test_title" } + } + } + + @Test + fun render_ToDom() { + runW { widget, element -> + widget.addCssClass("testClass") + widget.title = "test_title" + assertTrue("New element should be created") { element != null } + assertTrue("New element should have correct css class") { element?.className == "testClass" } + assertTrue("New element should have correct id attribute") { element?.id == "test_id" } + assertTrue("New element should have correct title attribute") { element?.getAttribute("title") == "test_title" } + } + } + + @Test + fun hide() { + runW { widget, element -> + widget.hide() + val element = document.getElementById("test_id") + assertTrue("Hidden element should not be accessible") { element == null } + } + } + + @Test + fun show() { + runW { widget, element -> + widget.hide() + widget.show() + val element = document.getElementById("test_id") + assertTrue("Visible element should be accessible") { element != null } + } + } + + @Test + fun addCssClass() { + runW { widget, element -> + widget.addCssClass("test_class1") + widget.addCssClass("test_class2") + assertTrue("Element should have correct css classes") { element?.className == "test_class1 test_class2" } + } + } + + @Test + fun removeCssClass() { + runW { widget, element -> + widget.addCssClass("test_class1") + widget.addCssClass("test_class2") + widget.removeCssClass("test_class1") + assertTrue("Element should have correct css class") { element?.className == "test_class2" } + } + } + + @Test + fun setEventListener() { + runW { widget, element -> + widget.setEventListener { click = { _ -> } } + assertTrue("Element should have an event listener") { widget.listeners.size == 1 } + } + } + + @Test + fun removeEventListener() { + runW { widget, element -> + widget.setEventListener { click = { _ -> } } + widget.removeEventListeners() + assertTrue("Element should not have any event listener") { widget.listeners.size == 0 } + } + } + +}
\ No newline at end of file |