Skip to content

Commit

Permalink
#155 Implement Tab._select(). Fixes #155
Browse files Browse the repository at this point in the history
  • Loading branch information
mvysny committed Jan 15, 2024
1 parent 948f221 commit 5a71d3e
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
6 changes: 6 additions & 0 deletions karibu-testing-v10/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1484,6 +1484,12 @@ is implemented in JavaScript and thus it's impossible to click from Karibu-Testi
Call `messageInput._submit("Hello World")`/`MessagesKt._submut(mi, "Hello World")` to submit a new message
(fire the `SubmitEvent`).

### Support for TabSheet/Tabs

Call `Tab._select()` to select given tab (Java: `TabsKt._select(tab)`) (since Karibu-Testing 2.1.1, depend on `karibu-testing-v23` or higher).
The `_select()` function will not allow you to select a disabled or invisible tab and will fail with an informative
error message. This function works with tabs both nested in `Tabs` and in `TabSheet`.

## Adding support for custom search criteria

> *Note*: this feature is unsupported for Java since Java lacks extension methods.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ internal fun DynaNodeGroup.prettyPrintTreeTest() {
testSpan.toPrettyString()
}
expect("Tab[label='foo', Tab{foo}]") {
// The trailing Tab{foo} comes from the custom Tab.toString()
Tab("foo").toPrettyString()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ fun DynaNodeGroup.allTests19(isModuleTest: Boolean) {
group("SideNav") {
sideNavTests()
}

group("Tabs+TabSheet") {
tabsTests()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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.kaributesting.v23._select
import com.vaadin.flow.component.Component
import com.vaadin.flow.component.html.Span
import com.vaadin.flow.component.tabs.Tab
import com.vaadin.flow.component.tabs.TabSheet
import com.vaadin.flow.component.tabs.Tabs
import kotlin.test.expect


@DynaTestDsl
internal fun DynaNodeGroup.tabsTests() {
group("_select()") {
group("Tabs") {
selectTests(
{ Tabs() },
{ tabs, tab -> tabs.add(tab) },
{ it.selectedTab })
}
group("TabSheet") {
selectTests(
{ TabSheet() },
{ tabs, tab -> tabs.add(tab, Span("contents of $tab")) },
{ it.selectedTab })
}
}
}

@DynaTestDsl
private fun <T : Component> DynaNodeGroup.selectTests(
create: () -> T,
add: (T, Tab) -> Unit,
selected: (T) -> Tab
) {
test("select already selected tab succeeds") {
val t = create()
val tab = Tab("Foo")
add(t, tab)
expect(tab) { selected(t) }

tab._select()
expect(tab) { selected(t) }
}

test("select enabled+visible tab succeeds") {
val t = create()
val tab = Tab("Foo")
add(t, tab)
val tab2 = Tab("Bar")
add(t, tab2)
expect(tab) { selected(t) }

tab2._select()
expect(tab2) { selected(t) }
}

test("select disabled tab fails") {
val t = create()
val tab = Tab("Foo")
add(t, tab)
val tab2 = Tab("Bar")
tab2.isEnabled = false
add(t, tab2)
expect(tab) { selected(t) }

expectThrows<IllegalStateException>("DISABLED, label='Bar', Tab{Bar}] is not enabled") {
tab2._select()
}
expect(tab) { selected(t) }
}

test("select invisible tab fails") {
val t = create()
val tab = Tab("Foo")
add(t, tab)
val tab2 = Tab("Bar")
tab2.isVisible = false
add(t, tab2)
expect(tab) { selected(t) }

expectThrows<IllegalStateException>("INVIS, label='Bar', Tab{Bar}] is not effectively visible") {
tab2._select()
}
expect(tab) { selected(t) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.mvysny.kaributesting.v23

import com.github.mvysny.kaributesting.v10._expectEditableByUser
import com.github.mvysny.kaributools.v23.owner
import com.vaadin.flow.component.tabs.Tab

/**
* Selects this tab, but only if the tab can be selected by the user (it is visible and enabled).
*/
public fun Tab._select() {
_expectEditableByUser()
owner.selectedTab = this
}

0 comments on commit 5a71d3e

Please sign in to comment.