From ef4d83ee13289761a014f884ee83688038e7b497 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Sat, 30 Sep 2017 12:50:04 +0200 Subject: Unit tests --- .../kotlin/pl/treksoft/kvision/core/KVManager.kt | 2 +- src/main/kotlin/pl/treksoft/kvision/core/Root.kt | 8 +-- .../kotlin/pl/treksoft/kvision/panel/StackPanel.kt | 2 + src/main/kotlin/pl/treksoft/kvision/tabs/Tabs.kt | 8 ++- .../kotlin/test/pl/treksoft/kvision/TestUtil.kt | 5 +- .../test/pl/treksoft/kvision/core/ContainerSpec.kt | 34 ++++++++++ .../test/pl/treksoft/kvision/core/RootSpec.kt | 15 +++++ .../test/pl/treksoft/kvision/modal/ModalSpec.kt | 3 +- .../pl/treksoft/kvision/panel/SplitPanelSpec.kt | 32 ++++++++++ .../pl/treksoft/kvision/panel/StackPanelSpec.kt | 74 ++++++++++++++++++++++ .../test/pl/treksoft/kvision/tabs/TabsSpec.kt | 60 ++++++++++++++++++ 11 files changed, 235 insertions(+), 8 deletions(-) create mode 100644 src/test/kotlin/test/pl/treksoft/kvision/panel/SplitPanelSpec.kt create mode 100644 src/test/kotlin/test/pl/treksoft/kvision/panel/StackPanelSpec.kt create mode 100644 src/test/kotlin/test/pl/treksoft/kvision/tabs/TabsSpec.kt (limited to 'src') diff --git a/src/main/kotlin/pl/treksoft/kvision/core/KVManager.kt b/src/main/kotlin/pl/treksoft/kvision/core/KVManager.kt index e75167e1..aa7d7a4a 100644 --- a/src/main/kotlin/pl/treksoft/kvision/core/KVManager.kt +++ b/src/main/kotlin/pl/treksoft/kvision/core/KVManager.kt @@ -22,7 +22,7 @@ object KVManager { private val sdPatch = Snabbdom.init(arrayOf(classModule, attributesModule, propsModule, styleModule, eventListenersModule, datasetModule)) private val sdVirtualize = require("snabbdom-virtualize/strings").default - private val splitCss = require("./css/style.css") + private val styleCss = require("./css/style.css") internal fun patch(id: String, vnode: VNode): VNode { val container = document.getElementById(id) diff --git a/src/main/kotlin/pl/treksoft/kvision/core/Root.kt b/src/main/kotlin/pl/treksoft/kvision/core/Root.kt index 82030249..52424f0e 100644 --- a/src/main/kotlin/pl/treksoft/kvision/core/Root.kt +++ b/src/main/kotlin/pl/treksoft/kvision/core/Root.kt @@ -18,16 +18,16 @@ class Root(id: String, private val fixed: Boolean = false) : Container() { return kvh("div#" + id, childrenVNodes() + modalsVNodes()) } - private fun modalsVNodes(): Array { - return modals.filter { it.visible }.map { it.render() }.toTypedArray() - } - internal fun addModal(modal: Modal) { modals.add(modal) modal.parent = this refresh() } + private fun modalsVNodes(): Array { + return modals.filter { it.visible }.map { it.render() }.toTypedArray() + } + override fun getSnClass(): List { val css = if (!fixed) "container-fluid" else "container" return super.getSnClass() + (css to true) diff --git a/src/main/kotlin/pl/treksoft/kvision/panel/StackPanel.kt b/src/main/kotlin/pl/treksoft/kvision/panel/StackPanel.kt index 24429d42..372098b8 100644 --- a/src/main/kotlin/pl/treksoft/kvision/panel/StackPanel.kt +++ b/src/main/kotlin/pl/treksoft/kvision/panel/StackPanel.kt @@ -23,12 +23,14 @@ open class StackPanel(private val activateLast: Boolean = true, override fun add(child: Widget): Container { super.add(child) if (activateLast) activeIndex = children.size - 1 + else if (activeIndex == -1) activeIndex = 0 return this } override fun addAll(children: List): Container { super.addAll(children) if (activateLast) activeIndex = this.children.size - 1 + else if (activeIndex == -1) activeIndex = 0 return this } diff --git a/src/main/kotlin/pl/treksoft/kvision/tabs/Tabs.kt b/src/main/kotlin/pl/treksoft/kvision/tabs/Tabs.kt index b21b1713..773f341a 100644 --- a/src/main/kotlin/pl/treksoft/kvision/tabs/Tabs.kt +++ b/src/main/kotlin/pl/treksoft/kvision/tabs/Tabs.kt @@ -11,7 +11,7 @@ import pl.treksoft.kvision.panel.StackPanel open class Tabs : Container(setOf()) { private var nav = Tag(TAG.UL, classes = setOf("nav", "nav-tabs")) private var content = StackPanel(false) - private var activeIndex + var activeIndex get() = content.activeIndex set(value) { content.activeIndex = value @@ -47,4 +47,10 @@ open class Tabs : Container(setOf()) { return this } + open fun removeTab(index: Int): Tabs { + nav.removeAt(index) + content.removeAt(index) + activeIndex = content.activeIndex + return this + } } diff --git a/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt b/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt index a4ac44ed..fbda7a45 100644 --- a/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt +++ b/src/test/kotlin/test/pl/treksoft/kvision/TestUtil.kt @@ -1,6 +1,7 @@ package test.pl.treksoft.kvision import org.w3c.dom.Element +import pl.treksoft.jquery.jQuery import pl.treksoft.kvision.core.Root import pl.treksoft.kvision.core.Widget import kotlin.browser.document @@ -20,13 +21,15 @@ interface TestSpec { interface DomSpec : TestSpec { override fun beforeTest() { - val fixture = "
" + val fixture = "
" + + "
" document.body?.insertAdjacentHTML("afterbegin", fixture) } override fun afterTest() { val div = document.getElementById("pretest") div?.remove() + jQuery(`object` = ".modal-backdrop").remove() } } diff --git a/src/test/kotlin/test/pl/treksoft/kvision/core/ContainerSpec.kt b/src/test/kotlin/test/pl/treksoft/kvision/core/ContainerSpec.kt index d9d59c4c..baed1372 100644 --- a/src/test/kotlin/test/pl/treksoft/kvision/core/ContainerSpec.kt +++ b/src/test/kotlin/test/pl/treksoft/kvision/core/ContainerSpec.kt @@ -82,4 +82,38 @@ class ContainerSpec : DomSpec { assertTrue("Container renders children") { elem1 == null && elem2 != null } } } + @Test + fun removeAll() { + 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.removeAll() + val elem1 = document.getElementById("child1") + val elem2 = document.getElementById("child2") + assertTrue("Children are not rendered") { elem1 == null && elem2 == null } + } + } + @Test + fun getChildren() { + 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 childern = container.getChildren() + assertTrue("Returns children of current element") { childern.size == 2 } + } + } } \ No newline at end of file diff --git a/src/test/kotlin/test/pl/treksoft/kvision/core/RootSpec.kt b/src/test/kotlin/test/pl/treksoft/kvision/core/RootSpec.kt index d8739a8b..6407dbc3 100644 --- a/src/test/kotlin/test/pl/treksoft/kvision/core/RootSpec.kt +++ b/src/test/kotlin/test/pl/treksoft/kvision/core/RootSpec.kt @@ -1,6 +1,7 @@ package test.pl.treksoft.kvision.core import pl.treksoft.kvision.core.Root +import pl.treksoft.kvision.modal.Modal import test.pl.treksoft.kvision.DomSpec import kotlin.browser.document import kotlin.test.Test @@ -34,4 +35,18 @@ class RootSpec : DomSpec { assertTrue("Should return self") { r == root } } } + + @Test + fun addModal() { + run { + val root = Root("test") + val modal = Modal("test") + modal.id = "test_modal" + root.addModal(modal) + modal.show() + val elem = document.getElementById("test_modal") + assertTrue("Should render standard modal") { elem != null } + modal.hide() + } + } } \ No newline at end of file diff --git a/src/test/kotlin/test/pl/treksoft/kvision/modal/ModalSpec.kt b/src/test/kotlin/test/pl/treksoft/kvision/modal/ModalSpec.kt index 7e4e7814..7bdd3562 100644 --- a/src/test/kotlin/test/pl/treksoft/kvision/modal/ModalSpec.kt +++ b/src/test/kotlin/test/pl/treksoft/kvision/modal/ModalSpec.kt @@ -18,8 +18,8 @@ class ModalSpec : DomSpec { val modal = Modal("Modal") modal.show() val content = document.getElementById("test")?.let { jQuery(it).find(".modal-title").html() } - modal.hide() assertEquals("Modal", content, "Should render correct modal") + modal.hide() } } @@ -34,6 +34,7 @@ class ModalSpec : DomSpec { modal.toggle() val content2 = document.getElementById("test")?.let { jQuery(it).find(".modal-title").html() } assertNull(content2, "Should hide modal after second toggle") + modal.hide() } } diff --git a/src/test/kotlin/test/pl/treksoft/kvision/panel/SplitPanelSpec.kt b/src/test/kotlin/test/pl/treksoft/kvision/panel/SplitPanelSpec.kt new file mode 100644 index 00000000..6e6ede53 --- /dev/null +++ b/src/test/kotlin/test/pl/treksoft/kvision/panel/SplitPanelSpec.kt @@ -0,0 +1,32 @@ +package test.pl.treksoft.kvision.panel + +import pl.treksoft.jquery.jQuery +import pl.treksoft.kvision.basic.Label +import pl.treksoft.kvision.core.Root +import pl.treksoft.kvision.modal.Alert +import pl.treksoft.kvision.panel.DIRECTION +import pl.treksoft.kvision.panel.SplitPanel +import test.pl.treksoft.kvision.DomSpec +import kotlin.browser.document +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotNull +import kotlin.test.assertNull + +class SplitPanelSpec : DomSpec { + + @Test + fun render() { + run { + val root = Root("test") + val splitPanel = SplitPanel(DIRECTION.VERTICAL) + root.add(splitPanel) + val label1 = Label("abc") + val label2 = Label("def") + splitPanel.add(label1) + splitPanel.add(label2) + val element = document.getElementById("test") + assertEquals("
abc
def
", element?.innerHTML, "Should render correct split panel") + } + } +} \ No newline at end of file diff --git a/src/test/kotlin/test/pl/treksoft/kvision/panel/StackPanelSpec.kt b/src/test/kotlin/test/pl/treksoft/kvision/panel/StackPanelSpec.kt new file mode 100644 index 00000000..9484c194 --- /dev/null +++ b/src/test/kotlin/test/pl/treksoft/kvision/panel/StackPanelSpec.kt @@ -0,0 +1,74 @@ +package test.pl.treksoft.kvision.panel + +import pl.treksoft.kvision.basic.Label +import pl.treksoft.kvision.core.Root +import pl.treksoft.kvision.panel.StackPanel +import test.pl.treksoft.kvision.DomSpec +import kotlin.browser.document +import kotlin.test.Test +import kotlin.test.assertEquals + +class StackPanelSpec : DomSpec { + + @Test + fun render() { + run { + val root = Root("test") + val stackPanel = StackPanel() + root.add(stackPanel) + val label1 = Label("abc") + val label2 = Label("def") + stackPanel.add(label1) + stackPanel.add(label2) + val element = document.getElementById("test") + assertEquals("
def
", element?.innerHTML, "Should render correct stack panel") + } + } + + @Test + fun renderNotActivateLast() { + run { + val root = Root("test") + val stackPanel = StackPanel(activateLast = false) + root.add(stackPanel) + val label1 = Label("abc") + val label2 = Label("def") + stackPanel.add(label1) + stackPanel.add(label2) + val element = document.getElementById("test") + assertEquals("
abc
", element?.innerHTML, "Should render correct stack panel with activateLast = false") + } + } + + @Test + fun remove() { + run { + val root = Root("test") + val stackPanel = StackPanel(activateLast = false) + root.add(stackPanel) + val label1 = Label("abc") + val label2 = Label("def") + stackPanel.add(label1) + stackPanel.add(label2) + stackPanel.remove(label1) + val element = document.getElementById("test") + assertEquals("
def
", element?.innerHTML, "Should remove correct child widget") + } + } + + @Test + fun removeAll() { + run { + val root = Root("test") + val stackPanel = StackPanel(activateLast = false) + root.add(stackPanel) + val label1 = Label("abc") + val label2 = Label("def") + stackPanel.add(label1) + stackPanel.add(label2) + stackPanel.removeAll() + val element = document.getElementById("test") + assertEquals("
", element?.innerHTML, "Should remove all child widgets") + } + } +} \ No newline at end of file diff --git a/src/test/kotlin/test/pl/treksoft/kvision/tabs/TabsSpec.kt b/src/test/kotlin/test/pl/treksoft/kvision/tabs/TabsSpec.kt new file mode 100644 index 00000000..60e2ef60 --- /dev/null +++ b/src/test/kotlin/test/pl/treksoft/kvision/tabs/TabsSpec.kt @@ -0,0 +1,60 @@ +package test.pl.treksoft.kvision.tabs + +import pl.treksoft.kvision.basic.Label +import pl.treksoft.kvision.core.Root +import pl.treksoft.kvision.tabs.Tabs +import test.pl.treksoft.kvision.DomSpec +import kotlin.browser.document +import kotlin.test.Test +import kotlin.test.assertEquals + +class TabsSpec : DomSpec { + + @Test + fun render() { + run { + val root = Root("test") + val tabs = Tabs() + root.add(tabs) + val label1 = Label("abc") + val label2 = Label("def") + tabs.addTab("ABC", label1) + tabs.addTab("DEF", label2) + val element = document.getElementById("test") + assertEquals("", element?.innerHTML, "Should render correct tabs") + } + } + + @Test + fun setActiveIndex() { + run { + val root = Root("test") + val tabs = Tabs() + root.add(tabs) + val label1 = Label("abc") + val label2 = Label("def") + tabs.addTab("ABC", label1) + tabs.addTab("DEF", label2) + tabs.activeIndex = 1 + val element = document.getElementById("test") + assertEquals("", element?.innerHTML, "Should change selected tab") + } + } + + @Test + fun removeTab() { + run { + val root = Root("test") + val tabs = Tabs() + root.add(tabs) + val label1 = Label("abc") + val label2 = Label("def") + tabs.addTab("ABC", label1) + tabs.addTab("DEF", label2) + tabs.activeIndex = 1 + tabs.removeTab(1) + val element = document.getElementById("test") + assertEquals("
abc
", element?.innerHTML, "Should remove tab") + } + } +} \ No newline at end of file -- cgit