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

## 2.0.0.beta3

### Added

- `AttributesTable#row_keys`: the row keys in page order.
- `AttributesTable#empty?(key)`, `#has_empty_row?(key)` and `#has_no_empty_row?(key)`: whether a row shows ActiveAdmin's empty marker for a blank value.
- `Form#has_input?(name)` and `#has_no_input?(name)`: whether the field of `f.input :name` is on the page. A form that declares its fields takes only declared names.
- `Form#has_submit?(label = nil)` and `#has_no_submit?`: the form's submit button, by label ignoring case.

## 2.0.0.beta2

### Added
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,24 @@ expect(table).to have_row(:name, exact_text: "Acme")
expect(table).to have_no_row(:secret)
table.row(:category) # the <tr>
table.value(:email) # => "acme@example.com"
table.row_keys # => ["name", "email", ...], in page order
expect(table).to have_empty_row(:category) # ActiveAdmin's empty marker; table.empty?(:category) does not wait
```

A page with several attributes tables or a `table_for` in a panel declares the table inside its panel's section:

```ruby
class AccountShow < ActiveAdminPageObjects::Page
section :details, ActiveAdminPageObjects::Panel, :active_admin_panel, "Details" do
section :attributes_table, ActiveAdminPageObjects::AttributesTable
end
section :users, ActiveAdminPageObjects::Panel, :active_admin_panel, "Users" do
section :table, ActiveAdminPageObjects::Table
end
end

show.details.attributes_table.value(:state)
expect(show.users.table).to have_row_with(:email, "admin@example.com")
```

### Panel
Expand Down Expand Up @@ -373,6 +391,8 @@ form = form_page.form # section :form, ActiveAdminPageObjects::Form
form.field(:email).fill("acme@example.com")
form.fill(name: "Acme", category: "Retail", active: false) # in this order
form.submit
expect(form).to have_submit("Create Customer") # the submit button, ignoring case
expect(form).to have_no_input(:password) # the field of `f.input :password` is absent
```

`field(name)` returns the section typed by the formtastic input class. `fill(value)` sets the field the way its type needs. A Symbol name stays as it is, so `field(:_memo)` finds `#customer__memo_input`. The field ids and names start with the form's object name, which the gem reads from the first control named like `customer[name]`. When a form names its fields otherwise, set it on the form class: `object_name "batch_action_form_inputs"`.
Expand Down
38 changes: 38 additions & 0 deletions lib/active_admin_page_objects/attributes_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,49 @@ def value(key, visible: nil)
text_of(find(:css, cell_selector(key), **visibility), visibility).strip
end

# The row keys in page order, as `data-row` writes them: ["id", "uuid"].
# A row without a title has no key and is left out.
# @return [Array<String>]
def row_keys(visible: nil)
page_element_keys(**given(visible: visible))
end

# True when row +key+ shows ActiveAdmin's empty marker for a blank value.
# Does not wait. A hidden row is found with `visible: :all`.
# @raise [Capybara::ElementNotFound] when there is no such row
def empty?(key, visible: nil)
visibility = visibility_of(key, visible)
empty_marker_in?(find(:css, cell_selector(key), wait: false, **visibility), visibility)
end

# True when row +key+ shows the empty marker. Takes `visible:` and `wait:`.
def has_empty_row?(key, visible: nil, **options)
visibility = visibility_of(key, visible)
eventually(**options) { empty_row?(key, visibility) }
end

# Waits until row +key+ shows no empty marker, or is gone.
def has_no_empty_row?(key, visible: nil, **options)
visibility = visibility_of(key, visible)
eventually(**options) { !empty_row?(key, visibility) }
end

# `has_declared_rows?`, `has_exact_rows?` and their `has_no_` forms.
Declarations.define_predicates(self, :rows)

private

def empty_row?(key, visibility)
cell = first(:css, cell_selector(key), minimum: 0, wait: false, **visibility)
!cell.nil? && empty_marker_in?(cell, visibility)
end

# Looks inside the cell, so a theme's marker selector with a comma stays
# scoped to the row.
def empty_marker_in?(cell, visibility)
cell.has_css?(sel(:attributes_table_empty_value), wait: false, **visibility)
end

def row_selector(key)
declaration_for(key)
sel(:attributes_table_row, slug(key))
Expand Down
31 changes: 31 additions & 0 deletions lib/active_admin_page_objects/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,30 @@ def submit
find(:active_admin, :form_submit, match: :first).click
end

# True when the field +name+ (`f.input :name`) is on the page. Not named
# has_field?, which is Capybara's lookup by label, id or name. A form that
# declares its fields takes only declared names, so declare a field that
# may be absent.
def has_input?(name, **options)
has_css?(field_selector(name), **visibility_of(name), **options)
end

# Waits until the field +name+ is not on the page.
def has_no_input?(name, **options)
has_no_css?(field_selector(name), **visibility_of(name), **options)
end

# True when the form has a submit button, or one labelled +label+
# (ignoring case): "Create Customer".
def has_submit?(label = nil, **options)
eventually(**options) { submit_labels.any? { |text| label.nil? || same_text?(text, label) } }
end

# Waits until the form has no submit button, or none labelled +label+.
def has_no_submit?(label = nil, **options)
eventually(**options) { submit_labels.none? { |text| label.nil? || same_text?(text, label) } }
end

# The `has_many` block of +association+, e.g. `has_many(:line_items)`. A form
# that declares has_many blocks takes the declared name. A hidden block is
# found with `visible: :all`.
Expand Down Expand Up @@ -207,6 +231,13 @@ def semantic_error_expectation(messages)

private

# An `<input type=submit>` is labelled by its value, a `<button>` by its text.
def submit_labels
all(:active_admin, :form_submit, wait: false).map do |button|
button.tag_name == "input" ? button.value.to_s : button.text
end
end

def semantic_error_count(messages, count)
raise ArgumentError, "count: #{count} takes one message, got #{messages.size}" unless messages.size == 1

Expand Down
1 change: 1 addition & 0 deletions lib/active_admin_page_objects/selectors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ module Selectors
attributes_table: ".attributes-table",
attributes_table_row: 'tr[data-row="%s"]',
attributes_table_rows: "tr[data-row]",
attributes_table_empty_value: ".attributes-table-empty-value",

panel: ".panel",
panel_title: ".panel-title",
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 = "2.0.0.beta2"
VERSION = "2.0.0.beta3"
end
61 changes: 61 additions & 0 deletions spec/active_admin_page_objects/attributes_table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,67 @@ def declared_table(&)
it_behaves_like "attributes table"
end

shared_examples "row order, empty values and tables in panels" do
it "lists the row keys in page order" do
show.load(id: customer.id)
expect(show.attributes_table.row_keys).to eq(%w[name email external_id status balance category signed_up])
expect(show.attributes_table.row_keys.each_cons(2)).to include(%w[name email])
end

it "tells a row with ActiveAdmin's empty marker" do
show.load(id: create_customer(category: nil).id)
table = show.attributes_table
expect(table.empty?(:category)).to be(true)
expect(table.empty?(:name)).to be(false)
expect(table).to have_empty_row(:category)
expect(table).to have_no_empty_row(:name)
expect(table.has_empty_row?(:name, wait: 0)).to be(false)
end

it "reads the attributes table of a panel on a page with several" do
document_show = Pages::DocumentShow.new
document_show.load(id: create_document.id)
expect(page).to have_css(ActiveAdminPageObjects.selector(:attributes_table), count: 2)
details = document_show.details.attributes_table
expect(details).to be_a(described_class)
expect(details.row_keys).to eq(%w[state code])
expect(details.value(:state)).to eq("draft")
expect(details).to have_empty_row(:code)
expect(details).to have_exact_rows
end
end

it_behaves_like "row order, empty values and tables in panels"

context "with rack_test", driver: :rack_test do
it_behaves_like "row order, empty values and tables in panels"
end

# These change the page with JavaScript.
it "tells a hidden empty row with visible: :all" do
show.load(id: create_customer(category: nil).id)
hide('tr[data-row="category"]')
table = show.attributes_table
expect(table.empty?(:category, visible: :all)).to be(true)
expect { table.empty?(:category) }.to raise_error(Capybara::ElementNotFound)
expect(table).to have_empty_row(:category, visible: :all)
expect(table.has_empty_row?(:category, wait: 0)).to be(false)
end

it "keeps a marker selector with a comma scoped to the row" do
saved = ActiveAdminPageObjects::Selectors::MAP.dup
ActiveAdminPageObjects.register_selectors(attributes_table_empty_value: ".attributes-table-empty-value, .other-empty")
show.load(id: create_customer(category: nil).id)
page.execute_script(<<~JS)
document.querySelector('tr[data-row="email"] > td').insertAdjacentHTML("beforeend", '<span class="other-empty">-</span>');
JS
expect(show.attributes_table).to have_no_empty_row(:name)
expect(show.attributes_table.empty?(:name)).to be(false)
expect(show.attributes_table).to have_empty_row(:category)
ensure
ActiveAdminPageObjects::Selectors::MAP.replace(saved)
end

describe "hidden rows" do
let(:table) { show.attributes_table }

Expand Down
19 changes: 19 additions & 0 deletions spec/active_admin_page_objects/form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,25 @@ def declared_form(&)
expect(declared.new(form_page, form.root_element)).to have_declared_fields
end

it "checks that a field is there or absent" do
form_page.load(action: "new")
lenient = described_class.new(form_page, form.root_element)
expect(lenient).to have_input(:name)
expect(lenient).to have_no_input(:_memo)
expect(lenient.has_input?(:_memo, wait: 0)).to be(false)
expect(form).to have_input(:email)
expect(form).to have_field("Email") # Capybara's matcher, by label
expect { form.has_no_input?(:_memo) }.to raise_error(ArgumentError, /not declared/)
end

it "checks the submit button by label" do
form_page.load(action: "new")
expect(form).to have_submit
expect(form).to have_submit("create customer")
expect(form).to have_no_submit("Update Customer")
expect(form.has_submit?("Update Customer", wait: 0)).to be(false)
end

it "takes any key without declarations" do
form_page.load(action: "new")
lenient = described_class.new(form_page, form.root_element)
Expand Down
18 changes: 18 additions & 0 deletions spec/active_admin_page_objects/panel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,24 @@
it_behaves_like "a title with a count"
end

shared_examples "panel table" do
it "reads the table_for of a panel through a nested section" do
show = Pages::DocumentShow.new
show.load(id: create_document(name: "Contract").id)
history = show.history.table
expect(history).to have_header_names("Name", "State")
expect(history).to have_row_with(:name, "Contract")
expect(history.row_with(:name, "Contract")).to have_cell(:state, exact_text: "draft")
expect(history).to have_exact_columns
end
end

it_behaves_like "panel table"

context "with rack_test", driver: :rack_test do
it_behaves_like "panel table"
end

shared_examples "panel status tags" do
it "finds the status tags in the body" do
dashboard = Pages::Dashboard.new
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 @@ -82,6 +82,6 @@
end

it "has a version" do
expect(ActiveAdminPageObjects::VERSION).to eq("2.0.0.beta2")
expect(ActiveAdminPageObjects::VERSION).to eq("2.0.0.beta3")
end
end
13 changes: 13 additions & 0 deletions spec/dummy/app/admin/documents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@
row :name
row :attachment_name
end
# A second attributes table, in a panel. The code is blank: the empty marker.
panel "Details" do
attributes_table_for resource do
row :state
row :code
end
end
panel "History" do
table_for [resource] do
column :name
column :state
end
end
panel "Possible Routes (2)" do
para "Two routes."
end
Expand Down
13 changes: 13 additions & 0 deletions spec/support/pages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ class DocumentShow < ActiveAdminPageObjects::Page
set_url "/admin/documents/{id}"

section :routes, ActiveAdminPageObjects::Panel, :active_admin_panel, /\APossible Routes \(\d+\)\z/
# A page with several tables reaches each through its panel.
section :details, ActiveAdminPageObjects::Panel, :active_admin_panel, "Details" do
section :attributes_table, ActiveAdminPageObjects::AttributesTable do
row :state, "State"
row :code, "Code"
end
end
section :history, ActiveAdminPageObjects::Panel, :active_admin_panel, "History" do
section :table, ActiveAdminPageObjects::Table do
column :name, "Name"
column :state, "State"
end
end
end

class DocumentForm < ActiveAdminPageObjects::Page
Expand Down
Loading