diff --git a/Gemfile b/Gemfile index 603fca4..a492f25 100644 --- a/Gemfile +++ b/Gemfile @@ -27,7 +27,7 @@ group :development, :test do gem 'rspec-expectations' # Drives the dummy app in the tests of active_admin_theme/page_objects. Not on # RubyGems yet, so pinned to its tag. - gem 'active_admin_page_objects', github: 'activeadmin-plugins/active_admin_page_objects', tag: 'v2.0.0.beta1' + gem 'active_admin_page_objects', github: 'activeadmin-plugins/active_admin_page_objects', tag: 'v2.0.0.beta2' # Tests the searchable select with its None and Present filter options. Not on RubyGems, so pinned to its tag. gem 'active_admin_filter_extensions', github: 'activeadmin-plugins/active_admin_filter_extensions', tag: 'v1.3.0' end diff --git a/README.md b/README.md index 5e57f2f..00da90f 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Theme 4 needs ActiveAdmin 4 (`>= 4.0.0.beta23`) and Ruby 3.3 or newer. To upgrad | ActiveAdmin | Theme | Install | Docs | | --- | --- | --- | --- | -| 4 | 4.0.0.beta2 | `gem "active_admin_theme", github: "activeadmin-plugins/active_admin_theme", tag: "v4.0.0.beta2"` | this README | +| 4 | 4.0.0.beta3 | `gem "active_admin_theme", github: "activeadmin-plugins/active_admin_theme", tag: "v4.0.0.beta3"` | this README | | 3 | 3.3.0, maintained on the [`3.x`](https://github.com/activeadmin-plugins/active_admin_theme/tree/3.x) branch | `gem "active_admin_theme", "~> 3.3"` (RubyGems) | [v3.3.0 README](https://github.com/activeadmin-plugins/active_admin_theme/blob/v3.3.0/README.md) | ## Screenshots @@ -41,7 +41,7 @@ Pages of the demo app in `test/dummy`. `bundle exec rake screenshots` regenerate Theme 4 is in beta and not on RubyGems yet, so install it from its GitHub tag: ```ruby -gem "active_admin_theme", github: "activeadmin-plugins/active_admin_theme", tag: "v4.0.0.beta2" +gem "active_admin_theme", github: "activeadmin-plugins/active_admin_theme", tag: "v4.0.0.beta3" ``` The gem requires `active_admin` first, so its views and partials win over ActiveAdmin's own. Do not reorder that. @@ -496,6 +496,35 @@ end `group_headings:` on the sidebar decides whether each group gets a heading and its own list. The default, `:auto`, shows headings only when there is more than one group and more than five links, so a short sidebar stays one plain list. `true` always shows them and `false` never does; without headings the links form one list in the order they were declared. A group whose links are all left out disappears with them. The titles are "Related records" and "External links", from `active_admin_theme.sidebars.related_records` and `.external_links`, or from `title:`. +### Sticky table header + +From 768px up, the first header row of an index table stays under the top bar while the page scrolls. To turn it off everywhere, set this in an initializer: + +```ruby +ActiveAdminTheme.sticky_table_header = false +``` + +To turn it off for one index, add the `skip-sticky-headers` class: + +```ruby +index do |table| + table.add_class "skip-sticky-headers" + # columns +end +``` + +A table outside a panel or a tab opts in with the `with-sticky-headers` class, as in v3. A panel or a tab scrolls sideways on its own, so the class does nothing there. + +### Row click + +A click on an index row ticks its batch checkbox, as in ActiveAdmin 4, and a row with a checked box is highlighted. The click also highlights the row and column of the cell. To keep the v3 behavior, where the click only highlights, set this in an initializer: + +```ruby +ActiveAdminTheme.row_click_selects = false +``` + +The setting needs the `active_admin_theme/table_crisscross` script, which is on by default. + ### Sticky first column Add the `with-sticky-rows` class to a wide table, and its first column stays in place while the table scrolls sideways: @@ -752,6 +781,7 @@ The sections of active_admin_page_objects then work on theme pages. Their README - `main_menu` is the top menu on desktop and the drawer on a narrow screen, at any depth. `labels`, `has_item?` and `go` take a label path, and `current_labels` returns one. `click` hovers each parent of the top menu; in the drawer it opens the drawer and each collapsed parent first. - `ActiveAdminPageObjects::Tabs` drives the theme's `tabs`. Every page has it as `tabs`. `select`, `content`, `has_tab?` and `has_selected?` take a String, the whole title ignoring case, or a Regexp. `select_matching` is `select` for a Regexp. +- `comments.add` works when the comments sit in a tab, even a nested one: after the page reloads, it selects each tab around them. This needs active_admin_page_objects 2.0.0.beta2 or newer. - `form.field(name)` and `filters.filter(name)` return the searchable select classes for an input rendered `as: :searchable_select`. The theme registers them for that wrapper class with `register_field_class` and `register_filter_class`, so a page object that declares the base gem's `SelectField` or `SelectFilter` for such an input raises `DeclaredClassMismatch`. - `flashes` also takes `:warning`. The theme renders every flash type, while ActiveAdmin 4 drops all but `notice`, `alert` and `error`. For another type your app sets, call `ActiveAdminTheme::PageObjects.register_flash_type(:info)` in your spec helper; an unregistered type still raises `ArgumentError`. - `attributes_table.secret(key)` and `row.secret(key)` return the cell's `SecretReveal`. `has_secret_mask?(key, mask = nil)`, `has_secret_value?(key, value)` and their `has_no_` forms wait as Capybara does. diff --git a/app/javascript/active_admin_theme/table_crisscross.js b/app/javascript/active_admin_theme/table_crisscross.js index 337b747..f0fc1ed 100644 --- a/app/javascript/active_admin_theme/table_crisscross.js +++ b/app/javascript/active_admin_theme/table_crisscross.js @@ -2,9 +2,9 @@ // column with data-selected, so a value in a wide table is easy to trace to its // headers. The clicked cell gets data-selected-cell. // -// ActiveAdmin ticks a row's checkbox when any cell is clicked; the handler -// clicks it again to undo that, so highlighting never leaves rows selected for -// batch actions. +// ActiveAdmin ticks a row's checkbox when any cell is clicked. When +// ActiveAdminTheme.row_click_selects is false, the handler clicks it again to +// undo that, so highlighting never leaves rows selected for batch actions. const MARK = "data-selected" const CENTER = "data-selected-cell" @@ -21,7 +21,7 @@ document.addEventListener("click", (event) => { // ActiveAdmin's row toggler has already handled this click; revert it. const checkbox = row.querySelector("input[type=checkbox]") - if (checkbox) checkbox.click() + if (checkbox && !cell.closest("[data-aat-row-click-selects]")) checkbox.click() clear(table) const index = cell.cellIndex @@ -36,4 +36,7 @@ document.addEventListener("click", (event) => { document.addEventListener("change", (event) => { const toggleAll = event.target.closest(".batch-actions-toggle-all") if (toggleAll) clear(toggleAll.closest("table")) + // ActiveAdmin sets a row's selected class only from the select-all box. + const checkbox = event.target.closest(".batch-actions-resource-selection") + checkbox?.closest("tr")?.classList.toggle("selected", checkbox.checked) }) diff --git a/app/tailwind/active_admin_theme.css b/app/tailwind/active_admin_theme.css index 504c39c..d0f5eb5 100644 --- a/app/tailwind/active_admin_theme.css +++ b/app/tailwind/active_admin_theme.css @@ -173,14 +173,18 @@ .data-table :where(tbody > tr):hover > td { background-color: var(--aat-surface-hover); } -.data-table :where(tbody > tr.selected) > td { +/* ActiveAdmin 4 marks rows selected only from the select-all box, so a + * checked batch checkbox marks its own row. */ +.data-table :where(tbody > tr.selected, tbody > tr:has(.batch-actions-resource-selection:checked)) > td { background-color: var(--aat-selected-row); } /* A wide index table widens the page, and the window scrolls sideways, as in * v3. The main column takes the table's width, and the bars around the * content stay at the viewport width. layout.js sets --aat-viewport-width, - * which leaves out a classic scrollbar. Below 768px the table scrolls in its - * wrapper. */ + * which leaves out a classic scrollbar. A sticky bar is a stacking context: + * the title bar sits above the flashes, the page content and its sticky + * header row, so its dropdowns take clicks over them, and below #site-header. + * Below 768px the table scrolls in its wrapper. */ @media (width >= 48rem) { :has(> [data-aat-page] .index-as-table) { min-width: fit-content; @@ -188,8 +192,18 @@ :has(> [data-aat-page] .index-as-table) > :not([data-aat-page], #main-menu, script) { position: sticky; inset-inline-start: 0; + z-index: 10; width: var(--aat-viewport-width, 100vw); } + :has(> [data-aat-page] .index-as-table) > #page-header { + z-index: 20; + } + /* An open confirm_dialog's backdrop is a child of body at z-index 40, so + * the title bar gives up its stacking context while the dialog is open. */ + :has(> [data-aat-page] .index-as-table) > #page-header:has(.aat-confirm-modal:not(.hidden)) { + position: relative; + z-index: auto; + } [data-aat-page][data-aat-has-sidebar]:not([data-aat-sidebar-collapsed]):has(.index-as-table) { grid-template-columns: 270px minmax(min-content, 1fr); } @@ -206,6 +220,30 @@ .aat-tab-panel:has(> .data-table) { overflow-x: auto; } +/* The first header row of an index table stays under the top bar while the + * page scrolls (ActiveAdminTheme.sticky_table_header). As in v3, an index + * opts out with skip-sticky-headers and another table opts in with + * with-sticky-headers, except in a panel or a tab, which scrolls on its own. + * A shadow draws the bottom line, which a sticky cell's collapsed border + * would leave behind. Below 768px the table scrolls in its wrapper, which a + * sticky header cannot leave. */ +@media (width >= 48rem) { + [data-aat-sticky-thead] .index-as-table > .data-table:not(.skip-sticky-headers) > thead > tr:first-child > th, + table.with-sticky-headers:not(:is(.panel-body, .aat-tab-panel) table) > thead > tr:first-child > th { + position: sticky; + top: var(--aa-header-height); + z-index: 2; + box-shadow: inset 0 -1px 0 var(--aat-border); + } + table.with-sticky-headers:not(:is(.panel-body, .aat-tab-panel) table) > thead > tr:first-child > th { + background-color: var(--aat-table-header); + } + :is([data-aat-sticky-thead] .index-as-table > .data-table:not(.skip-sticky-headers), table.with-sticky-headers:not(:is(.panel-body, .aat-tab-panel) table)).with-sticky-rows > thead > tr:first-child > th:first-child { + z-index: 3; + box-shadow: inset -1px 0 0 var(--aat-border), inset 0 -1px 0 var(--aat-border); + } +} + /* Opt-in sticky first column. Body cells keep the stripe, hover and selected fills above. */ table.with-sticky-rows :is(td, th):first-child { position: sticky; diff --git a/app/views/active_admin/_page_content.html.erb b/app/views/active_admin/_page_content.html.erb index 226b481..d145888 100644 --- a/app/views/active_admin/_page_content.html.erb +++ b/app/views/active_admin/_page_content.html.erb @@ -1,6 +1,6 @@ <%= active_admin_theme_config_tag %> <% has_sidebar = !(skip_sidebar? || sidebar_sections_for_action.blank?) %> -
class="px-2.5 md:px-4 grid grid-cols-1 gap-3 md:gap-4 md:data-aat-has-sidebar:grid-cols-[270px_minmax(0,1fr)] md:data-aat-sidebar-collapsed:grid-cols-1"> +
<%= "data-aat-sticky-thead" if ActiveAdminTheme.sticky_table_header %> <%= "data-aat-row-click-selects" if ActiveAdminTheme.row_click_selects %> class="px-2.5 md:px-4 grid grid-cols-1 gap-3 md:gap-4 md:data-aat-has-sidebar:grid-cols-[270px_minmax(0,1fr)] md:data-aat-sidebar-collapsed:grid-cols-1"> <%= render "active_admin/sidebar" %>
<% if has_sidebar %> diff --git a/docs/migrating-from-v3.md b/docs/migrating-from-v3.md index 76c7cc2..aa7267b 100644 --- a/docs/migrating-from-v3.md +++ b/docs/migrating-from-v3.md @@ -6,7 +6,7 @@ Upgrade ActiveAdmin first, and follow ActiveAdmin's own [upgrade guide](https:// ## Checklist -1. In the `Gemfile`, set `gem "active_admin_theme", github: "activeadmin-plugins/active_admin_theme", tag: "v4.0.0.beta2"` (the beta is not on RubyGems) and `activeadmin` to 4.0.0.beta23 or newer. Ruby 3.3 or newer is required. +1. In the `Gemfile`, set `gem "active_admin_theme", github: "activeadmin-plugins/active_admin_theme", tag: "v4.0.0.beta3"` (the beta is not on RubyGems) and `activeadmin` to 4.0.0.beta23 or newer. Ruby 3.3 or newer is required. 2. Remove the theme 3 lines from your stylesheet and script manifests: - `@import "wigu/active_admin_theme";` and any `$skin*` variable assignments - `//= require wigu/all` (or the individual `wigu/*` files) @@ -57,7 +57,7 @@ ActiveAdmin 4 dropped menus deeper than one submenu level, but the theme keeps t - The theme loads as ES modules through `ActiveAdmin.importmap`, not through Sprockets. A pin in your own `config/importmap.rb` does not load on ActiveAdmin pages; pin your scripts into `ActiveAdmin.importmap` (README, "JavaScript"). - `theme_toggle.js` is rewritten as `active_admin_theme/theme_toggle`. The header switch cycles auto, light and dark, and keeps ActiveAdmin 4's `theme` key in `localStorage`, so a choice made in theme 3 is not carried over. - `scrollbar_width.js` is gone. The header no longer uses `100vw`, so it is not needed. -- `table_crisscross.js` is rewritten without jQuery as `active_admin_theme/table_crisscross`, and loads by default. The `td.selected` marker is now `td[data-selected]`. +- `table_crisscross.js` is rewritten without jQuery as `active_admin_theme/table_crisscross`, and loads by default. The `td.selected` marker is now `td[data-selected]`. A cell click now also ticks the row's batch checkbox; `ActiveAdminTheme.row_click_selects = false` restores the v3 behavior. - `server_clock.js` is rewritten as `active_admin_theme/server_clock`. It reads `data-servertime-epoch` and `data-servertime-zone` from `[data-server-clock]`; the old `data-servertime` wall-clock format is not supported. ## DSL diff --git a/lib/active_admin_theme.rb b/lib/active_admin_theme.rb index 7c22217..90f81ad 100644 --- a/lib/active_admin_theme.rb +++ b/lib/active_admin_theme.rb @@ -35,7 +35,8 @@ class << self # Page size of a searchable_select_options endpoint that declares no per_page:. Nil means 50. attr_accessor :searchable_select_per_page - attr_writer :javascripts, :searchable_select_all, :searchable_select_min_chars + attr_writer :javascripts, :searchable_select_all, :searchable_select_min_chars, :sticky_table_header, + :row_click_selects # nil keeps ActiveAdmin's "Powered by" footer; any of the three replaces it. # footer_extra and site_title_link are a String, or a Proc run in the view. # site_title_link false or nil leaves the title as plain text. @@ -51,6 +52,18 @@ def searchable_select_all @searchable_select_all.nil? ? true : @searchable_select_all end + # True when the header row of an index table stays under the top bar while + # the page scrolls, from 768px up. False turns it off. + def sticky_table_header + @sticky_table_header.nil? ? true : @sticky_table_header + end + + # True when a click on an index row ticks its batch checkbox, as in + # ActiveAdmin 4. False keeps the v3 behavior: the click only highlights. + def row_click_selects + @row_click_selects.nil? ? true : @row_click_selects + end + # Characters to type before a searchable select asks its endpoint. def searchable_select_min_chars @searchable_select_min_chars || 3 diff --git a/lib/active_admin_theme/page_objects/tabs.rb b/lib/active_admin_theme/page_objects/tabs.rb index 5d553f7..ef2a87d 100644 --- a/lib/active_admin_theme/page_objects/tabs.rb +++ b/lib/active_admin_theme/page_objects/tabs.rb @@ -77,4 +77,29 @@ def tab_button(title) end end +module ActiveAdminTheme + module PageObjects + # Prepended to ActiveAdminPageObjects::Comments. Comments placed in a tab + # are hidden while another tab is selected, so Comments#add selects every + # tab around them after it reloads the page. Shown comments need nothing. + module CommentsPatch + TAB_PANELS_XPATH = "ancestor::*[@role='tabpanel']".freeze + + private + + def reveal_comments(panel) + return if panel.visible? + + # Outermost first: a nested tab's button is hidden until its parent tab shows. + panel.all(:xpath, TAB_PANELS_XPATH, visible: :all, wait: false).each do |tab_panel| + next if tab_panel.visible? + + panel.session.find(:css, "[role='tab'][aria-controls='#{tab_panel[:id]}']").click + end + end + end + end +end + ActiveAdminPageObjects::Tabs.prepend(ActiveAdminTheme::PageObjects::TabsPatch) +ActiveAdminPageObjects::Comments.prepend(ActiveAdminTheme::PageObjects::CommentsPatch) diff --git a/lib/active_admin_theme/version.rb b/lib/active_admin_theme/version.rb index e53616b..55de888 100644 --- a/lib/active_admin_theme/version.rb +++ b/lib/active_admin_theme/version.rb @@ -1,3 +1,3 @@ module ActiveAdminTheme - VERSION = "4.0.0.beta2" + VERSION = "4.0.0.beta3" end diff --git a/test/dummy/app/admin/menu_orders.rb b/test/dummy/app/admin/menu_orders.rb new file mode 100644 index 0000000..4368a77 --- /dev/null +++ b/test/dummy/app/admin/menu_orders.rb @@ -0,0 +1,40 @@ +# An index with an action-item dropdown whose items hang over the table +# toolbar, which the batch actions render, a confirm dialog, and a table in a cell +# (test/layout_test.rb). +ActiveAdmin.register Order, as: "MenuOrder" do + menu false + config.batch_actions = true + + filter :status, as: :select, collection: Order::STATUSES + + # ?flash=1 shows flash messages under the title bar. + controller do + before_action(only: :index) do + flash.now[:notice] = "Export queued" if params[:flash] + flash.now[:alert] = "Another export is running" if params[:flash] + end + end + + action_item :new_export, only: :index do + dropdown_menu_tag "New export", id: "new-export" do + item "CSV", "/admin/dropdown_menu/echo?to=csv" + item "XML", "/admin/dropdown_menu/echo?to=xml" + item "JSON", "/admin/dropdown_menu/echo?to=json" + item "PDF", "/admin/dropdown_menu/echo?to=pdf" + end + end + + action_item :purge, only: :index do + confirm_dialog_tag "Purge exports", id: "purge-exports", title: "Purge exports?", url: "/admin/confirm_dialog/echo", + class: "action-item-button" + end + + index do + selectable_column + id_column + column :reference + column :status + # A table in a cell keeps its own header: only the index header sticks. + column("Lines") { |order| table_for [order.reference], class: "nested-lines" do column("Line") { |line| line } end } + end +end diff --git a/test/dummy/app/admin/tabs_demo.rb b/test/dummy/app/admin/tabs_demo.rb index 4bc84d6..70520ce 100644 --- a/test/dummy/app/admin/tabs_demo.rb +++ b/test/dummy/app/admin/tabs_demo.rb @@ -19,3 +19,25 @@ end end end + +# Comments markup in a tab of a nested tab, for the page objects' reveal_comments +# (test/page_objects_components_test.rb). The dummy has no comments table. +ActiveAdmin.register_page "Tabbed comments" do + menu false + + content do + tabs do + tab("Details") { para "Details" } + tab("History") do + tabs do + tab("Changes") { para "Changes" } + tab("Notes") do + div id: "active-admin-comments-form" do + para "A comment", class: "tabbed-comment" + end + end + end + end + end + end +end diff --git a/test/dummy/app/admin/wide_table.rb b/test/dummy/app/admin/wide_table.rb index 781d8e8..b0352eb 100644 --- a/test/dummy/app/admin/wide_table.rb +++ b/test/dummy/app/admin/wide_table.rb @@ -31,12 +31,18 @@ "" + row.map { |cell| "#{ERB::Util.html_escape(cell)}" }.join + "" end.join text_node( - # with-sticky-headers: the one place in the repo's test surface where - # the sticky-thead selector matches a real element (AA's own index - # tables need a database-backed resource the dummy doesn't have). + # with-sticky-headers does nothing in a panel: the header scrolls away + # (test/layout_test.rb). (%() + head + "" + body + "
") .html_safe ) end + + panel "A table_for with with-sticky-headers, which a panel ignores" do + table_for (1..30).to_a, class: "with-sticky-headers", id: "sticky-table-for" do + column("Row") { |n| "Row #{n}" } + column("Value") { |n| "Value #{n}" } + end + end end end diff --git a/test/layout_test.rb b/test/layout_test.rb index 69ffce2..3103a13 100644 --- a/test/layout_test.rb +++ b/test/layout_test.rb @@ -168,6 +168,76 @@ def test_wide_table_outside_an_index_keeps_its_own_scroll assert_operator page.evaluate_script("document.documentElement.scrollWidth"), :<=, 1100 end + def test_action_item_dropdown_items_take_clicks_over_the_index_toolbar_and_flashes + page.current_window.resize_to(1280, 800) + ["/admin/menu_orders", "/admin/menu_orders?flash=1"].each do |path| + visit path + find("button", text: "New export").click + hits = page.evaluate_script(<<~JS) + [...document.querySelectorAll("#new-export a, [data-test-action-items] a[href*='to=']")].filter((a) => a.offsetParent).map((a) => { + const box = a.getBoundingClientRect() + const hit = document.elementFromPoint(box.x + box.width / 2, box.y + box.height / 2) + return a.contains(hit) ? a.textContent.trim() : hit.className + }) + JS + assert_equal %w[CSV XML JSON PDF], hits, path + end + assert_selector ".aat-flash", count: 2 + find("a[href$='to=xml']").click + assert_text "xml" + end + + def test_a_confirm_dialog_from_an_index_title_bar_takes_clicks + page.current_window.resize_to(1280, 800) + visit "/admin/menu_orders" + find("button", text: "Purge exports").click + hits = page.evaluate_script(<<~JS) + [...document.querySelectorAll("#purge-exports button")].filter((b) => b.offsetParent).map((b) => { + const box = b.getBoundingClientRect() + const hit = document.elementFromPoint(box.x + box.width / 2, box.y + box.height / 2) + return b.contains(hit) ? b.textContent.trim() : String(hit.className) + }) + JS + assert_equal %w[Confirm Cancel], hits + within("#purge-exports") { click_button "Cancel" } + assert_no_selector "#purge-exports", visible: :visible + assert_equal "sticky", page.evaluate_script("getComputedStyle(document.getElementById('page-header')).position") + end + + def test_index_table_header_sticks_under_the_top_bar + page.current_window.resize_to(1280, 500) + visit "/admin/customers" + assert_selector "[data-aat-sticky-thead]" + th = "document.querySelector('.data-table thead th').getBoundingClientRect().top" + page.execute_script("window.scrollTo(0, 600)") + assert_in_delta 40, page.evaluate_script(th), 1 + + # A panel scrolls on its own, so with-sticky-headers in it does nothing. + visit "/admin/wide_table" + page.execute_script("const t = document.getElementById('sticky-table-for'); window.scrollTo({top: t.getBoundingClientRect().top + window.scrollY + 200, behavior: 'instant'})") + assert_equal "static", page.evaluate_script("getComputedStyle(document.querySelector('#sticky-table-for thead th')).position") + assert_equal "auto", page.evaluate_script("getComputedStyle(document.getElementById('sticky-table-for').parentElement).overflowX") + assert_equal "static", page.evaluate_script("getComputedStyle(document.querySelector('table.wide-demo thead th')).position") + + visit "/admin/menu_orders" + assert_equal "sticky", page.evaluate_script("getComputedStyle(document.querySelector('.index-as-table > .data-table > thead th')).position") + assert_equal "static", page.evaluate_script("getComputedStyle(document.querySelector('table.nested-lines thead th')).position") + + page.current_window.resize_to(700, 500) + visit "/admin/customers" + assert_equal "static", page.evaluate_script("getComputedStyle(document.querySelector('.data-table thead th')).position") + end + + def test_index_table_header_can_be_turned_off + ActiveAdminTheme.sticky_table_header = false + visit "/admin/customers" + assert_selector "[data-aat-page]" + assert_no_selector "[data-aat-sticky-thead]" + assert_equal "static", page.evaluate_script("getComputedStyle(document.querySelector('.data-table thead th')).position") + ensure + ActiveAdminTheme.sticky_table_header = nil + end + def test_wide_index_tables_scroll_inside_their_wrapper_not_the_page page.current_window.resize_to(390, 800) visit "/admin/customers" @@ -227,13 +297,57 @@ def test_action_items_are_buttons refute_equal "rgba(0, 0, 0, 0)", background end - def test_crisscross_marks_row_and_column + def test_crisscross_marks_row_and_column_and_the_row_click_ticks_its_checkbox visit "/admin/customers" cells = all("table.data-table tbody tr:nth-child(2) td") cells[3].click assert_selector "table.data-table td[data-selected-cell]", count: 1 assert_selector "table.data-table tbody td[data-selected]", minimum: 30 - assert_no_selector "table.data-table tbody tr.selected" + assert find("table.data-table tbody tr:nth-child(2) input[type=checkbox]").checked? + cells[3].click + refute find("table.data-table tbody tr:nth-child(2) input[type=checkbox]").checked? + end + + def test_a_row_unchecked_after_select_all_loses_the_selected_class + visit "/admin/customers" + find(".batch-actions-toggle-all").click + assert_selector "table.data-table tbody tr.selected", count: 30 + all("table.data-table tbody tr:nth-child(2) td")[3].click + refute find("table.data-table tbody tr:nth-child(2) input[type=checkbox]").checked? + assert_selector "table.data-table tbody tr.selected", count: 29 + assert_no_selector "table.data-table tbody tr:nth-child(2).selected" + end + + def test_a_checked_row_is_highlighted + visit "/admin/customers" + checkbox = find("table.data-table tbody tr:nth-child(2) input[type=checkbox]") + refute_equal selected_row_color, row_color(3) + checkbox.click + find("[data-test-page-header] h2").hover # a hovered row shows the hover color + assert_equal selected_row_color, row_color(3) + checkbox.click + find("[data-test-page-header] h2").hover + refute_equal selected_row_color, row_color(3) + end + + def selected_row_color + page.evaluate_script("(() => { const d = document.createElement('div'); d.style.color = 'var(--aat-selected-row)'; document.body.append(d); const c = getComputedStyle(d).color; d.remove(); return c })()") + end + + # The background of row 2's cell +index+. + def row_color(index) + page.evaluate_script("getComputedStyle(document.querySelectorAll('table.data-table tbody tr:nth-child(2) td')[#{index}]).backgroundColor") + end + + def test_row_click_selects_can_be_turned_off + ActiveAdminTheme.row_click_selects = false + visit "/admin/customers" + assert_selector "[data-aat-page]" + all("table.data-table tbody tr:nth-child(2) td")[3].click + assert_selector "table.data-table td[data-selected-cell]", count: 1 + refute find("table.data-table tbody tr:nth-child(2) input[type=checkbox]").checked? + ensure + ActiveAdminTheme.row_click_selects = nil end def test_tabs_switch_panels diff --git a/test/page_objects_components_test.rb b/test/page_objects_components_test.rb index 9e9e1b9..8038867 100644 --- a/test/page_objects_components_test.rb +++ b/test/page_objects_components_test.rb @@ -288,6 +288,20 @@ def test_confirm_dialog_submits_with_its_method assert_equal "Suspend customer", dialogs.confirm_dialog(dialog: "action-confirm").trigger_text end + def test_comments_in_a_nested_tab_are_revealed_and_shown_ones_are_left_alone + page_object = Pages::TabbedComments.new + page_object.load + panel = find("#active-admin-comments-form", visible: :all) + refute panel.visible? + comments = page_object.comments(visible: :all) + comments.send(:reveal_comments, panel) + assert_selector "#active-admin-comments-form .tabbed-comment", visible: :visible + + page.execute_script("window.tabClicks = 0; document.addEventListener('click', (e) => { if (e.target.closest('[role=tab]')) window.tabClicks++ })") + comments.send(:reveal_comments, find("#active-admin-comments-form")) + assert_equal 0, page.evaluate_script("window.tabClicks") + end + def test_confirm_dialog_waits_for_its_buttons_to_be_enabled dialogs = Pages::ConfirmDialogs.new dialogs.load diff --git a/test/page_objects_helper.rb b/test/page_objects_helper.rb index 5fd2f5e..eaf63ab 100644 --- a/test/page_objects_helper.rb +++ b/test/page_objects_helper.rb @@ -108,6 +108,11 @@ class ConfirmDialogs < PO::Page class ColumnsDemo < PO::Page set_url "/admin/columns" end + + class TabbedComments < PO::Page + set_url "/admin/tabbed_comments" + section :comments, PO::Comments + end end class PageObjectsTestCase < Minitest::Test