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 0b5fd78 commit dfbbac5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,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 @@ -204,4 +204,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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,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 @@ -53,6 +54,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 @@ -73,6 +75,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 @@ -97,6 +100,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 dfbbac5

Please sign in to comment.