Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/widgetastic_patternfly5/components/menus/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,41 @@ class CheckboxSelect(BaseCheckboxSelect, Dropdown):


class BaseTypeaheadSelect(BaseSelect):
"""Represents the Patternfly Typeahead Select.

https://www.patternfly.org/components/menus/select/#typeahead
"""

BUTTON_LOCATOR = (
".//button[(contains(@class, '-c-select__toggle') "
"or contains(@class, '-c-menu-toggle')) "
"and not(contains(@class, '-c-select__toggle-clear'))]"
)
CREATE_ITEM_LOCATOR = ".//button[@id='select-typeahead-create']"
input = TextInput(locator=".//input")

def read(self):
return self.browser.get_attribute("value", self.input)

def fill(self, value, create_item=False):
"""Fills a TypeaheadSelect with a value.

Args:
value: The value to fill in or select.
create_item: If True, types the value and clicks the 'Create' option
when the item is not found in the existing options.
"""
if self.read() == value:
return False

if create_item and value not in self.items:
self.input.fill(value)
self.root_browser.click(self.CREATE_ITEM_LOCATOR)
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
return True
else:
self.item_select(value)
return True


class TypeaheadSelect(BaseTypeaheadSelect, Dropdown):
DEFAULT_LOCATOR = './/div[contains(@class, "-c-select")][1]'
34 changes: 34 additions & 0 deletions testing/components/menus/test_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class TestView(View):
select = Select(locator=".//div[@id='ws-react-c-select-single-select']")
checkbox_select = CheckboxSelect(locator='.//div[@id="ws-react-c-select-checkbox-select"]')
typeahead_select = TypeaheadSelect(locator=".//div[@id='ws-react-c-select-typeahead']")
typeahead_create_select = TypeaheadSelect(
locator=".//div[@id='ws-react-c-select-typeahead-with-create-option']"
)

yield TestView(browser)

Expand Down Expand Up @@ -159,3 +162,34 @@ def test_typeahead_select_item_select(typeahead_select):
with pytest.raises(SelectItemNotFound):
typeahead_select.fill("Non existing item")
assert not typeahead_select.is_open


@pytest.fixture
def typeahead_create_select(view):
yield view.typeahead_create_select


def test_typeahead_create_select_new_item(typeahead_create_select):
"""Test TypeaheadSelect with create_item option for creating new items."""
assert typeahead_create_select.is_displayed

# Selecting an existing item should work normally
typeahead_create_select.fill("Alabama")
assert typeahead_create_select.read() == "Alabama"
assert not typeahead_create_select.is_open

# Same value should report no change
assert typeahead_create_select.fill("Alabama") is False
Comment thread
sourcery-ai[bot] marked this conversation as resolved.

# Non-existing value without create_item should raise
with pytest.raises(SelectItemNotFound):
typeahead_create_select.fill("NonExistentItem")

# create_item=True with an existing item should select it normally
assert typeahead_create_select.fill("Florida", create_item=True) is True
assert typeahead_create_select.read() == "Florida"

# create_item=True with a new value should create and select it
assert typeahead_create_select.fill("MyNewOption", create_item=True) is True
assert typeahead_create_select.read() == "MyNewOption"
assert not typeahead_create_select.is_open
Loading