Skip to content

Commit

Permalink
Add ability to match components by their enabled state
Browse files Browse the repository at this point in the history
  • Loading branch information
mvysny committed Apr 15, 2024
1 parent 404364f commit a3b01d7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
Expand Up @@ -245,6 +245,12 @@ internal fun DynaNodeGroup.locatorTest() {
expect(true) { Button().matches {}}
expect(false) { Button().matches { predicates.add(Predicate { false }) }}
}
test("enabled") {
expect(true) { Button().matches { enabled = true }}
expect(false) { Button().matches { enabled = false }}
expect(false) { Button().apply { isEnabled = false }.matches { enabled = true }}
expect(true) { Button().apply { isEnabled = false }.matches { enabled = false }}
}
test("themes") {
expect(true) { Button().apply { addThemeNames("custom-theme", "my-theme") }.matches {} }
expect(true) {
Expand Down
Expand Up @@ -197,8 +197,8 @@ public String toString() {

/**
* Only matches component with given icon. Only works for Button and Icon.
* @param collection
* @param iconName
* @param collection the icon collection, e.g. "vaadin" for Vaadin-provided icons
* @param iconName the name of the individual icon.
* @return this
*/
@NotNull
Expand All @@ -217,4 +217,15 @@ public SearchSpecJ<T> withIcon(@NotNull VaadinIcon vaadinIcon) {
spec.iconIs(vaadinIcon);
return this;
}

/**
* Only matches component with given enabled state.
* @param enabled the enabled state to match
* @return this
*/
@NotNull
public SearchSpecJ<T> withEnabled(boolean enabled) {
spec.setEnabled(enabled);
return this;
}
}
Expand Up @@ -34,7 +34,8 @@ import java.util.function.Predicate
* @property themes if not null, the component must have all theme names defined. Space-separated
* @property withoutThemes if not null, the component must NOT have any of the theme names defined. Space-separated
* @property icon if not null, the component must have given [_iconName].
* @property predicates the predicates the component needs to match, not null. May be empty - in such case it is ignored. By default empty.
* @property enabled if not null, the component's [Component.isEnabled] must match this value.
* @property predicates the predicates the component needs to match, not null. May be empty - in such case it is ignored. By default, empty.
*/
public class SearchSpec<T : Component>(
public val clazz: Class<T>,
Expand All @@ -51,6 +52,7 @@ public class SearchSpec<T : Component>(
public var themes: String? = null,
public var withoutThemes: String? = null,
public var icon: IconName? = null,
public var enabled: Boolean? = null,
public var predicates: MutableList<Predicate<T>> = mutableListOf()
) {

Expand All @@ -71,6 +73,7 @@ public class SearchSpec<T : Component>(
if (!withoutThemes.isNullOrBlank()) list.add("withoutThemes='$withoutThemes'")
if (icon != null) list.add("icon='$icon'")
if (value != null) list.add("value=$value")
if (enabled != null) list.add(if (enabled!!) "enabled" else "disabled")
if (count != (0..Int.MAX_VALUE) && count != 1..1) list.add("count=$count")
list.addAll(predicates.map { it.toString() })
return list.joinToString(" and ")
Expand All @@ -95,6 +98,7 @@ public class SearchSpec<T : Component>(
if (text != null) p.add { component -> component._text == text }
if (value != null) p.add { component -> (component as? HasValue<*, *>)?.value == value }
if (icon != null) p.add { component -> component._iconName == icon }
if (enabled != null) p.add { component -> component.isEnabled == enabled }
@Suppress("UNCHECKED_CAST")
p.addAll(predicates.map { predicate -> { component: Component -> clazz.isInstance(component) && predicate.test(component as T) } })
return p.and()
Expand Down

0 comments on commit a3b01d7

Please sign in to comment.