From a107c5b7fe1cf0429436045e336b08d16b387367 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Sun, 26 Nov 2017 23:19:07 +0100 Subject: Unit tests --- .../test/pl/treksoft/kvision/form/FormPanelSpec.kt | 175 +++++++++++++++++++++ .../test/pl/treksoft/kvision/form/FormSpec.kt | 175 +++++++++++++++++++++ .../test/pl/treksoft/kvision/form/HelpBlockSpec.kt | 23 +++ .../test/pl/treksoft/kvision/form/text/TextSpec.kt | 2 + 4 files changed, 375 insertions(+) create mode 100644 src/test/kotlin/test/pl/treksoft/kvision/form/FormPanelSpec.kt create mode 100644 src/test/kotlin/test/pl/treksoft/kvision/form/FormSpec.kt create mode 100644 src/test/kotlin/test/pl/treksoft/kvision/form/HelpBlockSpec.kt (limited to 'src') diff --git a/src/test/kotlin/test/pl/treksoft/kvision/form/FormPanelSpec.kt b/src/test/kotlin/test/pl/treksoft/kvision/form/FormPanelSpec.kt new file mode 100644 index 00000000..d832e75d --- /dev/null +++ b/src/test/kotlin/test/pl/treksoft/kvision/form/FormPanelSpec.kt @@ -0,0 +1,175 @@ +package test.pl.treksoft.kvision.form + +import pl.treksoft.kvision.form.FormPanel +import pl.treksoft.kvision.form.text.Text +import pl.treksoft.kvision.form.time.DateTime +import test.pl.treksoft.kvision.SimpleSpec +import kotlin.js.Date +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotNull +import kotlin.test.assertNull + +@Suppress("CanBeParameter") +class FormPanelSpec : SimpleSpec { + + @Test + fun add() { + run { + class DataForm(val map: Map) { + val a: String? by map + val b: Boolean? by map + val c: Date? by map + } + + val formPanel = FormPanel { + DataForm(it) + } + val data = DataForm(mapOf("a" to "Test value")) + formPanel.setData(data) + val result = formPanel.getData() + assertNull(result.a, "FormPanel should return null without adding any control") + val textField = Text() + formPanel.add("a", textField) + formPanel.setData(data) + val result2 = formPanel.getData() + assertEquals("Test value", result2.a, "FormPanel should return initial value") + } + } + + @Test + fun remove() { + run { + class DataForm(val map: Map) { + val a: String? by map + val b: Boolean? by map + val c: Date? by map + } + + val formPanel = FormPanel { + DataForm(it) + } + val data = DataForm(mapOf("a" to "Test value")) + formPanel.add("a", Text()) + formPanel.setData(data) + formPanel.remove("a") + val result = formPanel.getData() + assertNull(result.a, "FormPanel should return null after removing control") + } + } + + @Test + fun removeAll() { + run { + class DataForm(val map: Map) { + val a: String? by map + val b: Boolean? by map + val c: Date? by map + } + + val formPanel = FormPanel { + DataForm(it) + } + val data = DataForm(mapOf("a" to "Test value")) + formPanel.add("a", Text()) + formPanel.setData(data) + formPanel.removeAll() + val result = formPanel.getData() + assertNull(result.a, "FormPanel should return null after removing all controls") + } + } + + @Test + fun getControl() { + run { + class DataForm(val map: Map) { + val a: String? by map + val b: Boolean? by map + val c: Date? by map + } + + val formPanel = FormPanel { + DataForm(it) + } + formPanel.add("a", Text()) + val control = formPanel.getControl("b") + assertNull(control, "Should return null when there is no such control") + val control2 = formPanel.getControl("a") + assertNotNull(control2, "Should return correct control") + } + } + + @Test + fun get() { + run { + class DataForm(val map: Map) { + val a: String? by map + val b: Boolean? by map + val c: Date? by map + } + + val formPanel = FormPanel { + DataForm(it) + } + val data = DataForm(mapOf("a" to "Test value")) + formPanel.add("a", Text()) + val b = formPanel["b"] + assertNull(b, "Should return null value when there is no added control") + val a = formPanel["a"] + assertNull(a, "Should return null value when control is empty") + formPanel.setData(data) + val a2 = formPanel["a"] + assertEquals("Test value", a2, "Should return correct value") + } + } + + @Test + fun getData() { + run { + class DataForm(val map: Map) { + val a: String? by map + val b: Boolean? by map + val c: Date? by map + } + + val formPanel = FormPanel { + DataForm(it) + } + val data = DataForm(mapOf("a" to "Test value")) + val textField = Text() + formPanel.add("a", textField) + formPanel.setData(data) + textField.value = "New value" + val result = formPanel.getData() + assertEquals("New value", result.a, "Form should return changed value") + } + } + + @Test + fun validate() { + run { + class DataForm(val map: Map) { + val s: String? by map + val d: Date? by map + } + + val formPanel = FormPanel { + DataForm(it) + } + formPanel.add("s", Text()) { + it.getValue()?.length ?: 0 > 4 + } + formPanel.add("d", DateTime(), required = true) + formPanel.setData(DataForm(mapOf("s" to "123"))) + val valid = formPanel.validate() + assertEquals(false, valid, "Should be invalid with initial data") + formPanel.setData(DataForm(mapOf("s" to "12345"))) + val valid2 = formPanel.validate() + assertEquals(false, valid2, "Should be invalid with partially changed data") + formPanel.setData(DataForm(mapOf("s" to "12345", "d" to Date()))) + val valid3 = formPanel.validate() + assertEquals(true, valid3, "Should be valid") + } + } + +} \ No newline at end of file diff --git a/src/test/kotlin/test/pl/treksoft/kvision/form/FormSpec.kt b/src/test/kotlin/test/pl/treksoft/kvision/form/FormSpec.kt new file mode 100644 index 00000000..c5333ac7 --- /dev/null +++ b/src/test/kotlin/test/pl/treksoft/kvision/form/FormSpec.kt @@ -0,0 +1,175 @@ +package test.pl.treksoft.kvision.form + +import pl.treksoft.kvision.form.Form +import pl.treksoft.kvision.form.text.Text +import pl.treksoft.kvision.form.time.DateTime +import test.pl.treksoft.kvision.SimpleSpec +import kotlin.js.Date +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotNull +import kotlin.test.assertNull + +@Suppress("CanBeParameter") +class FormSpec : SimpleSpec { + + @Test + fun add() { + run { + class DataForm(val map: Map) { + val a: String? by map + val b: Boolean? by map + val c: Date? by map + } + + val form = Form { + DataForm(it) + } + val data = DataForm(mapOf("a" to "Test value")) + form.setData(data) + val result = form.getData() + assertNull(result.a, "Form should return null without adding any control") + val textField = Text() + form.add("a", textField) + form.setData(data) + val result2 = form.getData() + assertEquals("Test value", result2.a, "Form should return initial value") + } + } + + @Test + fun remove() { + run { + class DataForm(val map: Map) { + val a: String? by map + val b: Boolean? by map + val c: Date? by map + } + + val form = Form { + DataForm(it) + } + val data = DataForm(mapOf("a" to "Test value")) + form.add("a", Text()) + form.setData(data) + form.remove("a") + val result = form.getData() + assertNull(result.a, "Form should return null after removing control") + } + } + + @Test + fun removeAll() { + run { + class DataForm(val map: Map) { + val a: String? by map + val b: Boolean? by map + val c: Date? by map + } + + val form = Form { + DataForm(it) + } + val data = DataForm(mapOf("a" to "Test value")) + form.add("a", Text()) + form.setData(data) + form.removeAll() + val result = form.getData() + assertNull(result.a, "Form should return null after removing all controls") + } + } + + @Test + fun getControl() { + run { + class DataForm(val map: Map) { + val a: String? by map + val b: Boolean? by map + val c: Date? by map + } + + val form = Form { + DataForm(it) + } + form.add("a", Text()) + val control = form.getControl("b") + assertNull(control, "Should return null when there is no such control") + val control2 = form.getControl("a") + assertNotNull(control2, "Should return correct control") + } + } + + @Test + fun get() { + run { + class DataForm(val map: Map) { + val a: String? by map + val b: Boolean? by map + val c: Date? by map + } + + val form = Form { + DataForm(it) + } + val data = DataForm(mapOf("a" to "Test value")) + form.add("a", Text()) + val b = form["b"] + assertNull(b, "Should return null value when there is no added control") + val a = form["a"] + assertNull(a, "Should return null value when control is empty") + form.setData(data) + val a2 = form["a"] + assertEquals("Test value", a2, "Should return correct value") + } + } + + @Test + fun getData() { + run { + class DataForm(val map: Map) { + val a: String? by map + val b: Boolean? by map + val c: Date? by map + } + + val form = Form { + DataForm(it) + } + val data = DataForm(mapOf("a" to "Test value")) + val textField = Text() + form.add("a", textField) + form.setData(data) + textField.value = "New value" + val result = form.getData() + assertEquals("New value", result.a, "Form should return changed value") + } + } + + @Test + fun validate() { + run { + class DataForm(val map: Map) { + val s: String? by map + val d: Date? by map + } + + val form = Form { + DataForm(it) + } + form.add("s", Text()) { + it.getValue()?.length ?: 0 > 4 + } + form.add("d", DateTime(), required = true) + form.setData(DataForm(mapOf("s" to "123"))) + val valid = form.validate() + assertEquals(false, valid, "Should be invalid with initial data") + form.setData(DataForm(mapOf("s" to "12345"))) + val valid2 = form.validate() + assertEquals(false, valid2, "Should be invalid with partially changed data") + form.setData(DataForm(mapOf("s" to "12345", "d" to Date()))) + val valid3 = form.validate() + assertEquals(true, valid3, "Should be valid") + } + } + +} \ No newline at end of file diff --git a/src/test/kotlin/test/pl/treksoft/kvision/form/HelpBlockSpec.kt b/src/test/kotlin/test/pl/treksoft/kvision/form/HelpBlockSpec.kt new file mode 100644 index 00000000..d325dc0f --- /dev/null +++ b/src/test/kotlin/test/pl/treksoft/kvision/form/HelpBlockSpec.kt @@ -0,0 +1,23 @@ +package test.pl.treksoft.kvision.form + +import pl.treksoft.kvision.core.Root +import pl.treksoft.kvision.form.HelpBlock +import test.pl.treksoft.kvision.DomSpec +import kotlin.browser.document +import kotlin.test.Test +import kotlin.test.assertEquals + +class HelpBlockSpec : DomSpec { + + @Test + fun render() { + run { + val root = Root("test") + val fl = HelpBlock("Form Error") + root.add(fl) + val element = document.getElementById("test") + assertEquals("Form Error", element?.innerHTML, "Should render correct help block") + } + } + +} \ No newline at end of file diff --git a/src/test/kotlin/test/pl/treksoft/kvision/form/text/TextSpec.kt b/src/test/kotlin/test/pl/treksoft/kvision/form/text/TextSpec.kt index aafd6dbf..1e9a5b6c 100644 --- a/src/test/kotlin/test/pl/treksoft/kvision/form/text/TextSpec.kt +++ b/src/test/kotlin/test/pl/treksoft/kvision/form/text/TextSpec.kt @@ -23,6 +23,8 @@ class TextSpec : DomSpec { val element = document.getElementById("test") val id = ti.input.id assertEquals("
", element?.innerHTML, "Should render correct input form control") + ti.validatorError = "Validation Error" + assertEquals("
Validation Error
", element?.innerHTML, "Should render correct input form control with validation error") } } -- cgit