activeadmin-plugins / capybara_active_admin

Capybara DSL for fast and easy testing Active Admin applications.
https://activeadmin-plugins.github.io/capybara_active_admin
MIT License
13 stars 7 forks source link

add #have_fields_date_range and #within_table_footer helpers #11

Open senid231 opened 4 years ago

senid231 commented 4 years ago
  def have_fields_date_range(label, options = {})
    exact = options[:exact]
    satisfy do |actual|
      expect(actual).to have_selector('div.filter_date_range label', text: label)
      container = actual.find('div.filter_date_range label', text: label).first(:xpath, './/..')
      expect(container.has_css?('.filter_date_range label')).to eq(true)
      base_name = container[:id].gsub(/_input\z/, '')
      expect(container).to have_field("#{base_name}_gteq_datetime", with: options[:from].to_s, exact: exact)
      expect(container).to have_field("#{base_name}_lteq_datetime", with: options[:to].to_s, exact: exact)
    end
  end

  def within_table_footer
    within('tfoot > tr') { yield }
  end

  def click_table_scope(text)
    selector = "#{table_scopes_container_selector} > #{table_scope_selector}"
    page.find(selector, exact_text: text).click
  end

  def table_header_selector(text, options = {})
    column = (options[:column] || text).to_s.tr(' ', '_').downcase
    selector = "th.col.col-#{column}"
    selector += '.sortable' if options[:sortable]
    selector += ".sorted-#{options[:sort_direction].to_s.downcase}" if options[:sort_direction].present?
    "thead > tr > #{selector}"
  end

  def have_table_header(text, options = {})
    selector = table_header_selector text, options
    opts = options.except(:column, :sortable, :sort_direction).merge(exact_text: text)
    have_selector(selector, opts)
  end

  def find_table_header(text, options = {})
    selector = table_header_selector text, options
    opts = options.except(:column, :sortable, :sort_direction).merge(exact_text: text)
    page.find(selector, opts)
  end

  def click_table_header(text, options = {})
    find_table_header(text, options).find('a').click
  end
senid231 commented 4 years ago
def within_sidebar(title)
    sidebar = page.find('#sidebar > .sidebar_section > h3', text: title).ancestor('.sidebar_section')
    within(sidebar) { yield }
  end
senid231 commented 4 years ago
def find_action_item(title, exact: true)
    opts = exact ? { exact_text: title } : { text: title }
    page.find(action_item_selector, opts)
  end

  def have_action_item_link(title, exact: true, href: nil, **options)
    opts = exact ? { exact_text: title } : { text: title }
    opts.merge!(options)
    selector = "#{action_item_selector} > a"
    selector += "[href=\"#{href}\"]" if href.present?
    have_selector(selector, opts)
  end

  def within_action_item_dropdown
    within("#{action_item_selector} .dropdown_menu_list_wrapper") { yield }
  end
senid231 commented 4 years ago
# override Capybara::ActiveAdmin::Selectors::Form#has_many_fields_selector
  # @param association_name [String]
  # @return [String] selector.
  def has_many_fields_selector(association_name)
    ".has_many_container.#{association_name} > fieldset.inputs.has_many_fields"
  end
senid231 commented 3 years ago
def within_attribute_row(label)
    selector = attributes_row_selector(label)
    within(selector) { yield }
  end
senid231 commented 2 years ago
  def have_status_tag(text, type:, **options)
    selector = ".status_tag.#{type}"
    opts = options.merge exact_text: text
    have_selector(selector, opts)
  end
senid231 commented 1 year ago
def have_table_scope(title, selected: false)
    selector = table_scope_selector(selected)
    have_selector(selector, exact_text: title.to_s)
  end

  def table_scope_selector(selected)
    if selected
      'div.scopes li.scope.selected'
    else
      'div.scopes li.scope'
    end
  end
senid231 commented 1 year ago
# @example usage
#   expect(page).to have_status_tag(:error, exact_text: 'DOWN')
def have_status_tag(type, **options)
  have_selector("span.status_tag.#{type}", **options)
end