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/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 ++++++++++++++++++ 7 files changed, 221 insertions(+), 2 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/test/kotlin') 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("
abc
", 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("
def
", 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