discourse/spec/system/page_objects/components/select_kit.rb
Régis Hanol 5665452779
UX: Keep usable tags before disabled rows when sorted alphabetically (#39356)
Two related issues in the composer and bulk-actions tag pickers:

1. When `tags_sort_alphabetically` is enabled, the alphabetical sort in
`mini-tag-chooser` and `tag-chooser` re-ordered the merged results
array, interleaving the disabled rows (added in #39072) with usable
matches. This defeated the backend's "enabled-first, disabled-appended"
ordering and pushed selectable tags below a wall of explanation text.

2. `tag-chooser-row.gjs` (used by bulk-actions, move-to-topic, the
reviewable form, form-template fields, and several admin surfaces) never
received the disabled-reason rendering that was added to `tag-row.gjs`.
Those surfaces silently rendered unclickable rows with no explanation.

This commit:

- Extracts a shared `sortSearchResults` helper on the `tagUtils`
service. When alphabetical sort is enabled it partitions results by the
`disabled` flag, sorts each partition independently, and concatenates
with usable tags first.
- Switches `mini-tag-chooser` and `tag-chooser` to the helper. Also
replaces the broken `(a, b) => a.name > b.name` comparator in
`tag-chooser` (which returned a boolean instead of -1/0/1) with the
helper's `localeCompare` ordering.
- Adds the disabled-reason `<span>` to `tag-chooser-row` for parity with
`tag-row`. The existing CSS already covered the `.tag-chooser` selector,
so no styling changes were needed.
- Adds a backend assertion in `Tags::Search` spec that allowed tags
precede disabled tags in the response, locking in the contract the
frontend helper relies on.
- Adds two `SelectKit` page object methods (`has_disabled_row_name?` and
`has_disabled_row_reason?`) and reorganises `tag_search_hints_spec.rb`
to cover both the composer and the bulk-actions surfaces, including the
alphabetical-sort ordering.

Ref - t/181010
2026-05-26 09:29:11 +02:00

189 lines
5.1 KiB
Ruby
Vendored

# frozen_string_literal: true
module PageObjects
module Components
class SelectKit < PageObjects::Components::Base
attr_reader :context
def initialize(context)
@context = context
end
def component
if @context.is_a?(Capybara::Node::Element)
@context
else
find(@context)
end
end
def visible?
has_css?(@context, visible: true)
end
def hidden?
has_no_css?(@context)
end
def expanded_component(skip_collapsed_check: false)
# Skip collapsed check to avoid infinite loop when called from .expand
expand if is_collapsed? && !skip_collapsed_check
locator("#{@context}.is-expanded")
end
def collapsed_component
find(@context + ":not(.is-expanded)")
end
def expanded?
component.has_css?(".select-kit-body", visible: true)
end
def is_collapsed?
has_css?(context) && has_css?("#{context}:not(.is-expanded)", wait: 0)
end
def is_not_disabled?
has_css?(@context + ":not(.disabled)", wait: 0)
end
def value
component.find(".select-kit-header")["data-value"]
end
def has_selected_value?(value)
component.find(".select-kit-header[data-value='#{value}']")
end
def has_selected_name?(name)
component.find(".select-kit-header[data-name='#{name}']")
end
def has_no_selection?
component.has_no_css?(".selected-choice")
end
def has_selected_choice_name?(name)
component.find(".selected-choice[data-name='#{name}']")
end
def has_selected_names?(*names)
selected = component.find(".formatted-selection").text.split(", ")
names.map(&:to_s).sort == selected.sort
end
def has_option_name?(name)
component.find(".select-kit-collection li[data-name='#{name}']")
end
def has_no_option_name?(name)
component.has_no_css?(".select-kit-collection li[data-name='#{name}']")
end
def has_option_value?(value)
component.find(".select-kit-collection li[data-value='#{value}']")
end
def has_no_option_value?(value)
component.has_no_css?(".select-kit-collection li[data-value='#{value}']")
end
def expand
collapsed_component.find(".select-kit-header", visible: :all).click
expanded_component(skip_collapsed_check: true)
end
def collapse
expanded_component.locator(".select-kit-header").click
collapsed_component
end
def collapse_with_escape
expanded_component.press("Escape")
collapsed_component
end
def has_filter?
expanded_component # auto-expands if collapsed
has_css?("#{@context}.is-expanded .select-kit-filter .filter-input", visible: true)
end
def has_no_filter?
expanded_component
has_no_css?("#{@context}.is-expanded .select-kit-filter .filter-input")
end
def search(value = nil)
expanded_component.locator(".select-kit-filter .filter-input").fill(value.to_s)
wait_for_loaded
end
def wait_for_loaded
has_no_css?("#{@context}.is-loading")
end
def select_row_by_value(value)
expanded_component.locator(".select-kit-row[data-value='#{value}']").click
end
def select_row_by_name(name)
expanded_component.locator(".select-kit-row[data-name='#{name}']").click
end
def select_row_by_index(index)
expanded_component.locator(".select-kit-row[data-index='#{index}']").click
end
def unselect_by_name(name)
expanded_component.locator(".selected-choice[data-name='#{name}']").click
end
def clear
choices = expanded_component.locator(".selected-choice")
choices.first.click while choices.count > 0
end
def has_selected_row_name?(name)
expanded_component
has_css?("#{@context}.is-expanded .select-kit-row.is-selected[data-name='#{name}']")
end
def has_disabled_row_name?(name)
expanded_component
has_css?("#{@context}.is-expanded .select-kit-row.disabled[data-name='#{name}']")
end
def has_no_disabled_row_name?(name)
expanded_component
has_no_css?("#{@context}.is-expanded .select-kit-row.disabled[data-name='#{name}']")
end
def has_row_synonym_hint?(name, hint_text)
expanded_component
has_css?(
"#{@context}.is-expanded .select-kit-row[data-name='#{name}'] .synonym-hint",
text: hint_text,
)
end
def has_no_selected_row?
expanded_component
has_no_css?("#{@context}.is-expanded .select-kit-row.is-selected")
end
def has_disabled_row_reason?(name)
expanded_component
has_css?(
"#{@context}.is-expanded .select-kit-row.disabled[data-name='#{name}'] .disabled-reason",
)
end
def option_names
expanded_component
.locator(".select-kit-row")
.all
.map { |row| row.get_attribute("data-name") }
end
end
end
end