constraint-validation-example

Expand History Expand History
Collapse History Collapse History

Add System tests for Validation Messages A11y

Add a dependency on the capybara_accessible_selectors gem to gain access to valuable assertions that are concerned with Accessibility. For example, by declaring the validation_error: filter (which utilizes the described_by: filter under the hood) as an option to a find_selector :rich_text_area call, we can assert the references between input and validation message are valid.

Unfortunately, the capybara_accessible_selectors declares a :rich_text selector which is slightly incompatible with <trix-editor> elements. Since it is an almost identical concept to :rich_text_area (and :rich_text_area is not yet available outside of rails/rails@master), there isn’t support for passing filters like validation_error: and described_by: to have_selector :rich_text_area calls.

This commit rectifies that by modifying the :rich_text_area selector to be counted as part of the :capybara_accessible_selectors filter set:

 class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
   driven_by :selenium, using: :chrome, screen_size: [1400, 1400]
+
+  Capybara.modify_selector(:rich_text_area) do
+    filter_set(:capybara_accessible_selectors, %i[focused fieldset described_by validation_error])
+  end
 end

Finally, to ensure a tight testing feedback loop, this commit also configures Rails’ I18n translations to raise errors for keys that reference missing translations.