module RailsJsdom::TestHelper

Helpers for driving a page in jsdom from an integration test. Their names and options follow the system test methods you already use.

Inherit from RailsJsdom::TestCase, which includes this module, or include it in your own ActionDispatch::IntegrationTest.

Locators

Actions find their element with a locator, tried in this order. The first rule that matches anything wins:

  1. Stimulus action shorthand. click "dropdown#toggle" finds the element whose data-action calls dropdown#toggle.

  2. Text.

  3. For click, click_on, click_link, click_button, hover, double_click, right_click, find_button and find_link: a button, link, submit, checkbox, radio, summary, label or [role=button] whose text, aria-label, title, id or button value equals the locator, or else contains it.

  4. For fill_in, check, uncheck, choose, attach_file, find_field and the from: of select: the field of a label whose text equals, or else contains, the locator; then a field whose placeholder, name, id or aria-label equals it.

  5. CSS selector. click "#checkout".

click_link only matches links, click_button only buttons, click_on both; fill_in only text-like fields; check and uncheck only checkboxes; choose only radio buttons; attach_file only file fields.

More than one match raises, unless you pass match: :first.

Filters

find, all, first, find_field, find_button, find_link, the has_*? predicates and the assert_* element assertions take these filters:

Count assertions also take count:, or minimum: and maximum:.

Scoping

within, within_fieldset and within_table scope every action, finder and assertion in their block, including assert_dom, to one element. Blocks nest.

Dialogs

An alert, confirm or prompt that appears during an action fails it, unless the action runs inside the matching accept_alert, accept_confirm,

dismiss_confirm, accept_prompt or dismiss_prompt.

Settling

Every action returns once the page has settled: due timers have run on a virtual clock, no Rails request is pending, and Turbo Stream broadcasts made so far have been applied. JavaScript errors and unhandled promise rejections raised during the action fail it. Assertions and predicates read the settled page, so they never need to wait or retry.