Skip to content
Open
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 1.2.1 (2026-12-02)

### Fixed

- `HasMany#fieldset(label:)` finds a fieldset after a validation error, when Rails wraps the field's label in `div.field_with_errors`.
- `Comments#add` passes when the comments panel sits in a tab that the reload hides: it checks the new comment's text whether or not it is visible.

## 1.2.0 (2026-12-02)

### Added
Expand Down
7 changes: 5 additions & 2 deletions lib/active_admin_page_objects/comments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ def add(text)
find(:active_admin, :comment_input).set(text)
session = root_element.session
find(:active_admin, :comment_submit).click
# The submit reloads the page, so this panel's node goes stale: look in the new page.
session.assert_selector(:css, "#{sel(:comments)} #{sel(:comment_body)}", text: text)
# The submit reloads the page, so this panel's node goes stale: look in the
# new page. The panel may sit in a tab that the reload hides.
session.assert_selector(:css, "#{sel(:comments)} #{sel(:comment_body)}", visible: :all) do |body|
body.text(:all).include?(text)
end
end

# Deletes the comment whose body contains +text+, accepting the browser confirmation.
Expand Down
4 changes: 3 additions & 1 deletion lib/active_admin_page_objects/has_many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ def labelled_fieldset(label)

# The container's own fieldsets, and their own field labels (a radio
# group's too), so a nested has_many or a check box choice never matches.
# Rails' field_error_proc wraps the label of a field with an error in
# `div.field_with_errors`.
OWN_FIELDSETS_XPATH = "./fieldset[contains(concat(' ', normalize-space(@class), ' '), ' has_many_fields ')]"
OWN_LABELS_XPATH = "./ol/li/label | ./ol/li/fieldset/legend/label"
OWN_LABELS_XPATH = "./ol/li/label | ./ol/li/div/label | ./ol/li/fieldset/legend/label"
private_constant :OWN_FIELDSETS_XPATH, :OWN_LABELS_XPATH

def find_labelled_fieldset(label)
Expand Down
2 changes: 1 addition & 1 deletion lib/active_admin_page_objects/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module ActiveAdminPageObjects
VERSION = "1.2.0"
VERSION = "1.2.1"
end
14 changes: 14 additions & 0 deletions spec/active_admin_page_objects/comments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ def create_comment(body)
Capybara.automatic_reload = true
end

# After the reload, the first tab is open and the comments tab is hidden.
it "adds a comment in a tab that is hidden after the reload" do
tabbed = Class.new(ActiveAdminPageObjects::Page) do
set_url "/admin/customers/{id}{?query*}"
section :comments, ActiveAdminPageObjects::Comments
section :tabs, ActiveAdminPageObjects::Tabs
end.new
tabbed.load(id: customer.id, query: { comments_in_tab: 1 })
tabbed.tabs.switch("Comments")
tabbed.comments.add("In a tab")
expect(tabbed.tabs.current).to eq("Overview")
expect(ActiveAdmin::Comment.pluck(:body)).to eq(["In a tab"])
end

it "reads existing comments" do
create_comment("First comment")
create_comment("Second comment")
Expand Down
13 changes: 12 additions & 1 deletion spec/active_admin_page_objects/has_many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ def declared_orders(&)
end

describe "a fieldset by the label of one of its fields" do
let(:customer) { create_customer(status: "pending", external_id: "LABELLED-ORDERS") }

before do
create_order("REF-1", "paid", 2)
create_order("REF-B1", "new", 1)
form_page.load(id: customer.id, action: "edit", query: { labelled_orders: 1 })
form_page.load(id: customer.id, action: "edit")
end

it "finds it ignoring case and the required marker" do
Expand All @@ -34,6 +36,15 @@ def declared_orders(&)
expect { orders.fieldset }.to raise_error(ArgumentError)
end

# Rails wraps the label of a field with an error in `div.field_with_errors`.
it "finds it after a validation error" do
orders.fieldset(label: "REF-B1").field(:quantity).fill("0")
form.submit
expect(form).to have_field_errors
expect(page).to have_css("div.field_with_errors > label")
expect(orders.fieldset(label: "REF-B1").field(:quantity).value).to eq("0")
end

it "looks at the fieldset's own fields, not at a nested has_many" do
page.execute_script(<<~JS)
document.querySelector("fieldset.has_many_fields > ol").insertAdjacentHTML("beforeend",
Expand Down
2 changes: 1 addition & 1 deletion spec/active_admin_page_objects/text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@
end

it "has a version" do
expect(ActiveAdminPageObjects::VERSION).to eq("1.2.0")
expect(ActiveAdminPageObjects::VERSION).to eq("1.2.1")
end
end
9 changes: 6 additions & 3 deletions spec/dummy/app/admin/customers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,10 @@
column :state
end
end
# `?comments_in_tab=1` moves the comments into a tab that is hidden on load.
tab("Comments") { active_admin_comments } if params[:comments_in_tab]
end
active_admin_comments
active_admin_comments unless params[:comments_in_tab]
end

form do |f|
Expand Down Expand Up @@ -150,8 +152,9 @@
o.input :tags, as: :check_boxes, collection: %w[Gift Rush] if f.object.external_id == "ORDER-TAGS"
o.input :reference
o.input :state, as: :select, collection: Order::STATES
# `?labelled_orders=1` labels each quantity with its order's reference.
quantity_label = params[:labelled_orders] ? { label: o.object.reference.to_s, required: true } : {}
# A customer with external id "LABELLED-ORDERS" labels each quantity with
# its order's reference, also on the form re-rendered with errors.
quantity_label = f.object.external_id == "LABELLED-ORDERS" ? { label: o.object.reference.to_s, required: true } : {}
o.input :quantity, **quantity_label, input_html: { disabled: params[:lock_quantity].present? }
end
f.actions
Expand Down
Loading