Skip to content

Commit

Permalink
replace DynaTest with JUnit5
Browse files Browse the repository at this point in the history
  • Loading branch information
mvysny committed Oct 25, 2024
1 parent dc87bed commit ab60b35
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ open class AbstractAllTests10(val isModuleTest: Boolean) {
@Nested inner class BasicUtilsTests : AbstractBasicUtilsTests()
@Nested inner class ElementUtilsTests : AbstractElementUtilsTests()
@Nested inner class RenderersTests : AbstractRenderersTests()
@Nested inner class ButtonTests : AbstractButtonTests()
}

/**
Expand All @@ -32,9 +33,6 @@ fun DynaNodeGroup.allTests(isModuleTest: Boolean) {
Locale.setDefault(Locale.ENGLISH)
}

group("button") {
buttonTestbatch()
}
group("HasValue utils") {
hasValueTestbatch()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.github.mvysny.kaributesting.v10

import com.github.mvysny.dynatest.DynaNodeGroup
import com.github.mvysny.dynatest.DynaTestDsl
import com.github.mvysny.dynatest.expectThrows
import com.github.mvysny.karibudsl.v10.button
import com.vaadin.flow.component.ClickNotifier
Expand All @@ -23,25 +21,27 @@ import com.vaadin.flow.component.icon.Icon
import com.vaadin.flow.component.orderedlayout.FlexLayout
import com.vaadin.flow.component.orderedlayout.HorizontalLayout
import com.vaadin.flow.component.orderedlayout.VerticalLayout
import org.junit.jupiter.api.Assumptions.assumeTrue
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import kotlin.test.expect
import kotlin.test.fail

@DynaTestDsl
internal fun DynaNodeGroup.buttonTestbatch() {
group("button click") {
abstract class AbstractButtonTests {
@Nested inner class `button click` {
fun expectClickCount(button: Button, clickCount: Int, block: Button.()->Unit) {
var clicked = 0
button.addClickListener { if (++clicked > clickCount) fail("Clicked more than $clickCount times") }
button.block()
expect(clickCount) { clicked }
}

test("enabled button") {
@Test fun `enabled button`() {
expectClickCount(Button(), 1) { click() }
expectClickCount(Button(), 1) { _click() }
}

test("disabled button") {
@Test fun `disabled button`() {
// click() does not check for disabled state. Actually, in Vaadin 24.2.2+ it does, and the event is not fired.
// doesn't matter - you should always use _click()
// expectClickCount(Button().apply { isEnabled = false }, 0) { click() }
Expand All @@ -51,7 +51,7 @@ internal fun DynaNodeGroup.buttonTestbatch() {
}
}

test("button with parent disabled") {
@Test fun `button with parent disabled`() {
val layout = VerticalLayout().apply { isEnabled = false }
expect(false) { layout.isEnabled } // sanity check, to test that isEnabled works as intended
expect(false) { layout.button().isEnabled } // sanity check, to test that isEnabled works as intended
Expand All @@ -64,7 +64,7 @@ internal fun DynaNodeGroup.buttonTestbatch() {
}
}

test("invisible button") {
@Test fun `invisible button`() {
// click() does not check for invisible state
expectClickCount(Button().apply { isVisible = false }, 1) { click() }
// however _click() will properly fail
Expand All @@ -74,27 +74,26 @@ internal fun DynaNodeGroup.buttonTestbatch() {
}
}

group("_click()") {
clickTest("Button", true) { Button() }
clickTest("Checkbox", true) { Checkbox() }
clickTest("FormLayout", true) { FormLayout() }
clickTest("Icon", false) { Icon() }
clickTest("VerticalLayout", true) { VerticalLayout() }
clickTest("HorizontalLayout", true) { HorizontalLayout() }
clickTest("Image", true) { Image() }
clickTest("ListItem", true) { ListItem() }
clickTest("UnorderedList", true) { UnorderedList() }
clickTest("Div", true) { Div() }
clickTest("NativeButton", true) { NativeButton() }
clickTest("FlexLayout", true) { FlexLayout() }
clickTest("Paragraph", true) { Paragraph() }
clickTest("H1", true) { H1() }
clickTest("Span", true) { Span() }
@Nested inner class _click() {
@Nested inner class ButtonTests : AbstractClickTests("Button", true, { Button() })
@Nested inner class CheckboxTests : AbstractClickTests("Checkbox", true, { Checkbox() })
@Nested inner class FormLayoutTests : AbstractClickTests("FormLayout", true, { FormLayout() })
@Nested inner class IconTests : AbstractClickTests("Icon", false, { Icon() })
@Nested inner class VerticalLayoutTests : AbstractClickTests("VerticalLayout", true, { VerticalLayout() })
@Nested inner class HorizontalLayoutTests : AbstractClickTests("HorizontalLayout", true, { HorizontalLayout() })
@Nested inner class ImageTests : AbstractClickTests("Image", true, { Image() })
@Nested inner class ListItemTests : AbstractClickTests("ListItem", true, { ListItem() })
@Nested inner class UnorderedListTests : AbstractClickTests("UnorderedList", true, { UnorderedList() })
@Nested inner class DivTests : AbstractClickTests("Div", true, { Div() })
@Nested inner class NativeButtonTests : AbstractClickTests("NativeButton", true, { NativeButton() })
@Nested inner class FlexLayoutTests : AbstractClickTests("FlexLayout", true, { FlexLayout() })
@Nested inner class ParagraphTests : AbstractClickTests("Paragraph", true, { Paragraph() })
@Nested inner class H1Tests : AbstractClickTests("H1", true, { H1() })
@Nested inner class SpanTests : AbstractClickTests("Span", true, { Span() })
}
}

@DynaTestDsl
fun DynaNodeGroup.clickTest(componentName: String, hasEnabled: Boolean, componentProvider: () -> ClickNotifier<*>) {
abstract class AbstractClickTests(val componentName: String, val hasEnabled: Boolean, val componentProvider: () -> ClickNotifier<*>) {
fun <T : ClickNotifier<*>> expectClickCount(button: T, clickCount: Int) {
var clicked = 0
button.addClickListener { e ->
Expand All @@ -105,50 +104,47 @@ fun DynaNodeGroup.clickTest(componentName: String, hasEnabled: Boolean, componen
expect(clickCount) { clicked }
}

group(componentName) {
test("click succeeds on enabled component") {
expectClickCount(componentProvider(), 1)
}
@Test fun `click succeeds on enabled component`() {
expectClickCount(componentProvider(), 1)
}

@Test fun `_click() also fires DOM Event`() {
val c = componentProvider()
var domClicked = 0
(c as HasElement).element.addEventListener("click") {domClicked++ }
c._click()
expect(1) { domClicked }
expectClickCount(c, 1)
expect(2) { domClicked } // both DOM listener and click listener are called when both are registered.
}

test("_click() also fires DOM Event") {
@Test fun `click fails on disabled component`() {
assumeTrue(hasEnabled)
expectThrows(IllegalStateException::class, "is not enabled") {
val c = componentProvider()
var domClicked = 0
(c as HasElement).element.addEventListener("click") {domClicked++ }
c._click()
expect(1) { domClicked }
expectClickCount(c, 1)
expect(2) { domClicked } // both DOM listener and click listener are called when both are registered.
(c as HasEnabled).isEnabled = false
expectClickCount(c, 0)
}
}

if (hasEnabled) {
test("click fails on disabled component") {
expectThrows(IllegalStateException::class, "is not enabled") {
val c = componentProvider()
(c as HasEnabled).isEnabled = false
expectClickCount(c, 0)
}
}
@Test fun `click fails on component with parent disabled`() {
val layout = VerticalLayout()
layout.isEnabled = false
val c = componentProvider()
layout.add(c as Component)
expect(false) { c.isEnabled } // sanity check, to test that isEnabled works as intended
// click() does not check for parent disabled state
// however _click() will properly fail
expectThrows(IllegalStateException::class, "is nested in a disabled component") {
expectClickCount(c, 0)
}
}

test("click fails on component with parent disabled") {
val layout = VerticalLayout()
layout.isEnabled = false
@Test fun `click fails on invisible component`() {
expectThrows(IllegalStateException::class, "is not effectively visible") {
val c = componentProvider()
layout.add(c as Component)
expect(false) { c.isEnabled } // sanity check, to test that isEnabled works as intended
// click() does not check for parent disabled state
// however _click() will properly fail
expectThrows(IllegalStateException::class, "is nested in a disabled component") {
expectClickCount(c, 0)
}
}

test("click fails on invisible component") {
expectThrows(IllegalStateException::class, "is not effectively visible") {
val c = componentProvider()
(c as Component).isVisible = false
expectClickCount(c, 0)
}
(c as Component).isVisible = false
expectClickCount(c, 0)
}
}
}

0 comments on commit ab60b35

Please sign in to comment.