# frozen_string_literal: true
Then(/^I should see a table header with "([^"]*)"$/) do |content|
  expect(page).to have_xpath "//th", text: content
end

Then(/^I should not see a table header with "([^"]*)"$/) do |content|
  expect(page).to have_no_xpath "//th", text: content
end

Then(/^I should see a sortable table header with "([^"]*)"$/) do |content|
  expect(page).to have_css "th[data-sortable]", text: content
end

Then(/^I should not see a sortable table header with "([^"]*)"$/) do |content|
  expect(page).to have_no_css "th[data-sortable]", text: content
end

Then(/^I should not see a sortable table header$/) do
  step %{I should not see "th[data-sortable]"}
end

Then(/^the table "([^"]*)" should have (\d+) rows/) do |selector, count|
  trs = page.find(selector).all :css, "tr"
  expect(trs.size).to eq count.to_i
end

Then(/^the table "([^"]*)" should have (\d+) columns/) do |selector, count|
  tds = page.find(selector).find("tr:first").all :css, "td"
  expect(tds.size).to eq count.to_i
end

Then(/^there should be (\d+) "([^"]*)" tags?$/) do |count, tag|
  expect(page.all(:css, tag).size).to eq count.to_i
end

Then(/^I should see a link to "([^"]*)"$/) do |link|
  if Capybara.current_driver == Capybara.javascript_driver
    expect(page).to have_xpath "//a", text: link, wait: 30
  else
    expect(page).to have_xpath "//a", text: link
  end
end

Then(/^an "([^"]*)" exception should be raised when I follow "([^"]*)"$/) do |error, link|
  expect do
    step "I follow \"#{link}\""
  end.to raise_error(error.constantize)
end

Then(/^I should be in the resource section for (.+)$/) do |resource_name|
  expect(current_url).to include resource_name.tr(" ", "").underscore.pluralize
end

Then(/^I should see the page title "([^"]*)"$/) do |title|
  within("[data-test-page-header]") do
    expect(page).to have_content title
  end
end

Then(/^I should see a fieldset titled "([^"]*)"$/) do |title|
  expect(page).to have_css "fieldset legend", text: title
end

Then(/^the "([^"]*)" field should contain the option "([^"]*)"$/) do |field, option|
  field = find_field(field)
  expect(field).to have_css "option", text: option
end

Then(/^I should see the content "([^"]*)"$/) do |content|
  expect(page).to have_css "[data-test-page-content]", text: content
end

Then(/^I should see a validation error "([^"]*)"$/) do |error_message|
  expect(page).to have_css ".inline-errors", text: error_message
end

Then(/^I should see a table with id "([^"]*)"$/) do |dom_id|
  page.find("table##{dom_id}")
end
