Files
grecia/spec/rails_helper.rb
Javi Martín 2b962f2789 Use a <main> tag on every page
Many pages had this tag, but many other didn't, which made navigation
inconsistent for people using screen readers.

Note that there are slight changes in two pages:

* The homepage now includes the banner and the content of the
  `shared/header` element inside the <main> tag
* The budgets index now includes the banner inside the <main> tag

I see both potential advantages and disadvantages of this approach,
since banners aren't necessarily related to the main content of a page
but on the other hand they aren't the same across pages and people using
screen readers might accidentally skip them if they jump to the <main>
tag.

So I'm choosing the option that is easier to implement.

Note we're adding a `public-content` class to the <main> element in the
application layout. This might be redundat because the element could
already be accessed through the `.public main` selector, but this is
consistent with the `admin-content` class used in the admin section, and
without it the <main> element would sometimes have an empty class
attribute and we'd have to use `if content_for?(:main_class)` or
`tag.main` which IMHO makes the code less consistent.

The Capybara::DSL monkey-patch is only done on the `visit` method
because it's the only reliable one. Other methods like `click_link`
generate AJAX requests, so `expect(page).to have_css "main", count: 1`
might be executed before the AJAX request is finished, meaning it
wouldn't properly test anything.
2024-03-23 00:35:43 +01:00

98 lines
2.5 KiB
Ruby

ENV["RAILS_ENV"] ||= "test"
if ENV["TEST_COVERAGE"] && !ENV["TEST_COVERAGE"].empty?
require "simplecov"
require "simplecov-lcov"
SimpleCov::Formatter::LcovFormatter.config.report_with_single_file = true
SimpleCov::Formatter::LcovFormatter.config do |config|
config.output_directory = "coverage"
config.lcov_file_name = "lcov.info"
end
SimpleCov.formatter = SimpleCov::Formatter::LcovFormatter
SimpleCov.start("rails")
end
require File.expand_path("../../config/environment", __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require "rspec/rails"
require "spec_helper"
require "custom_spec_helper"
require "capybara/rails"
require "capybara/rspec"
require "selenium/webdriver"
require "view_component/test_helpers"
module ViewComponent
module TestHelpers
def sign_in(user)
allow(vc_test_controller).to receive(:current_user).and_return(user)
end
def within(...)
raise "`within` doesn't work in component tests. Use `page.find` instead."
end
end
end
RSpec.configure do |config|
config.include ViewComponent::TestHelpers, type: :component
end
Rails.application.load_tasks if Rake::Task.tasks.empty?
include Warden::Test::Helpers
Warden.test_mode!
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.infer_spec_type_from_file_location!
config.after do
Warden.test_reset!
end
end
FactoryBot.use_parent_strategy = false
module Capybara
module DSL
alias_method :original_visit, :visit
def visit(url, ...)
original_visit(url, ...)
unless url.match?("robots.txt") || url.match?("active_storage/representations")
expect(page).to have_css "main", count: 1
end
end
end
end
Capybara.register_driver :headless_chrome do |app|
options = Selenium::WebDriver::Chrome::Options.new.tap do |opts|
opts.add_argument "--headless"
opts.add_argument "--no-sandbox"
opts.add_argument "--window-size=1200,800"
opts.add_argument "--proxy-server=#{Capybara.app_host}:#{Capybara::Webmock.port_number}"
end
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end
Capybara.exact = true
Capybara.enable_aria_label = true
Capybara.disable_animation = true
Capybara.app_host ||= "http://127.0.0.1"
OmniAuth.config.test_mode = true
def with_subdomain(subdomain, &block)
app_host = Capybara.app_host
begin
Capybara.app_host = "http://#{subdomain}.lvh.me"
block.call
ensure
Capybara.app_host = app_host
end
end