Skip to content

Add create_item support to TypeaheadSelect fill method#120

Merged
digitronik merged 1 commit intoRedHatQE:mainfrom
apimplap:add-typeahead-create-item
Apr 30, 2026
Merged

Add create_item support to TypeaheadSelect fill method#120
digitronik merged 1 commit intoRedHatQE:mainfrom
apimplap:add-typeahead-create-item

Conversation

@apimplap
Copy link
Copy Markdown

@apimplap apimplap commented Apr 30, 2026

Summary by Sourcery

Add support for creating new options via the TypeaheadSelect fill method and cover it with tests.

New Features:

  • Allow BaseTypeaheadSelect.fill to optionally create and select a new item when it does not exist in the current options.

Tests:

  • Add tests for TypeaheadSelect with create_item behavior, covering existing item selection, unchanged values, error paths, and creation of new options.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Apr 30, 2026

Reviewer's Guide

Extends the Patternfly TypeaheadSelect widget to support an optional create_item flow in its fill method and adds corresponding tests and fixtures for a typeahead select with a create option.

Sequence diagram for TypeaheadSelect fill with create_item support

sequenceDiagram
    actor User
    participant TypeaheadSelectWidget
    participant Browser

    User->>TypeaheadSelectWidget: fill(value, create_item)
    TypeaheadSelectWidget->>TypeaheadSelectWidget: read()
    alt value already filled
        TypeaheadSelectWidget-->>User: return False
    else value not filled
        alt create_item is True and value not in items
            TypeaheadSelectWidget->>TypeaheadSelectWidget: input.fill(value)
            TypeaheadSelectWidget->>Browser: click(CREATE_ITEM_LOCATOR)
            TypeaheadSelectWidget-->>User: return True
        else create_item is False or value in items
            TypeaheadSelectWidget->>TypeaheadSelectWidget: item_select(value)
            TypeaheadSelectWidget-->>User: return True
        end
    end
Loading

Class diagram for updated BaseTypeaheadSelect and TypeaheadSelect

classDiagram
    class BaseSelect
    class Dropdown

    class BaseTypeaheadSelect {
        +BUTTON_LOCATOR
        +CREATE_ITEM_LOCATOR
        +input
        +read()
        +fill(value, create_item)
    }

    class TypeaheadSelect {
        +DEFAULT_LOCATOR
    }

    BaseTypeaheadSelect --|> BaseSelect
    TypeaheadSelect --|> BaseTypeaheadSelect
    TypeaheadSelect --|> Dropdown
Loading

File-Level Changes

Change Details Files
Add create_item-aware fill behavior to BaseTypeaheadSelect, including create-option locator and documentation.
  • Document BaseTypeaheadSelect as representing the Patternfly Typeahead Select with a reference URL.
  • Introduce CREATE_ITEM_LOCATOR pointing to the create-option button in the typeahead menu.
  • Add a fill method that early-returns when the requested value is already selected, selects an existing item via item_select when present, and when create_item is True and the value is not in items, types the value and clicks the create option button.
src/widgetastic_patternfly5/components/menus/select.py
Introduce testing coverage for a typeahead select that supports creating new items via the create_item option.
  • Extend the TestView fixture to expose a typeahead_create_select using the locator for the typeahead-with-create-option example.
  • Add a typeahead_create_select fixture that yields the new view attribute for tests.
  • Add test_typeahead_create_select_new_item to validate existing selection behavior, no-op on same value, error on non-existent value without create_item, and create/select behavior for both existing and new values when create_item is True.
testing/components/menus/test_select.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 2 issues, and left some high level feedback:

  • In BaseTypeaheadSelect.fill, the click for the create option uses self.root_browser.click(self.CREATE_ITEM_LOCATOR), which bypasses the widget root; consider clicking relative to self.browser so this works correctly when multiple typeahead selects or non-unique IDs are present.
  • The early-return optimization in fill only checks self.read() == value once before branching, so when create_item=True and the value matches the current selection it still goes through item_select and returns True; consider reusing the same read short-circuit for the create_item=True path to avoid unnecessary interaction and keep return semantics consistent.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `BaseTypeaheadSelect.fill`, the click for the create option uses `self.root_browser.click(self.CREATE_ITEM_LOCATOR)`, which bypasses the widget root; consider clicking relative to `self.browser` so this works correctly when multiple typeahead selects or non-unique IDs are present.
- The early-return optimization in `fill` only checks `self.read() == value` once before branching, so when `create_item=True` and the value matches the current selection it still goes through `item_select` and returns `True`; consider reusing the same `read` short-circuit for the `create_item=True` path to avoid unnecessary interaction and keep return semantics consistent.

## Individual Comments

### Comment 1
<location path="src/widgetastic_patternfly5/components/menus/select.py" line_range="223" />
<code_context>
+
+        if create_item and value not in self.items:
+            self.input.fill(value)
+            self.root_browser.click(self.CREATE_ITEM_LOCATOR)
+            return True
+        else:
</code_context>
<issue_to_address>
**issue (bug_risk):** Using `root_browser` with a relative locator may not respect the widget’s root context.

Because `CREATE_ITEM_LOCATOR` is a relative XPath, calling it on `root_browser` (which acts at page root) can match the wrong element when multiple typeahead widgets are present. Please call `click` on the widget-scoped browser (e.g. `self.browser.click(self.CREATE_ITEM_LOCATOR)`) so the locator is constrained to this instance.
</issue_to_address>

### Comment 2
<location path="testing/components/menus/test_select.py" line_range="181-182" />
<code_context>
+    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
+
+    # Non-existing value without create_item should raise
</code_context>
<issue_to_address>
**suggestion (testing):** Add a test case for `create_item=True` when the value is already selected

Since `read() == value` is checked before the `create_item` branch, calling `fill("Alabama", create_item=True)` should also return `False` when the value is already selected. Please add an assertion for this case (either in this test or a small dedicated one) to lock in that early-return behavior and prevent regressions.

```suggestion
    # Same value should report no change
    assert typeahead_create_select.fill("Alabama") is False
    assert typeahead_create_select.fill("Alabama", create_item=True) is False
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/widgetastic_patternfly5/components/menus/select.py
Comment thread testing/components/menus/test_select.py
Copy link
Copy Markdown
Member

@digitronik digitronik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 30, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.19%. Comparing base (9a0809c) to head (1832721).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #120      +/-   ##
==========================================
+ Coverage   91.15%   91.19%   +0.04%     
==========================================
  Files          38       38              
  Lines        2181     2191      +10     
==========================================
+ Hits         1988     1998      +10     
  Misses        193      193              
Flag Coverage Δ
unittests 91.19% <100.00%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@digitronik digitronik merged commit 028eff4 into RedHatQE:main Apr 30, 2026
12 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants