Module: Dommy::TestHelpers

Defined in:
lib/dommy/test_helpers.rb

Overview

Lightweight helpers for using Dommy from RSpec / Minitest test suites.

Examples:

RSpec

require "dommy/test_helpers"

RSpec.configure do |c|
  c.include Dommy::TestHelpers
end

RSpec.describe MyComponent do
  it "renders the heading" do
    dom = parse_html(render(MyComponent.new))
    expect(dom.query_selector("h1").text_content).to eq("Welcome")
  end
end

Minitest

require "dommy/test_helpers"

class MyComponentTest < Minitest::Test
  include Dommy::TestHelpers

  def test_renders_the_heading
    dom = parse_html(render(MyComponent.new))
    assert_equal "Welcome", dom.query_selector("h1").text_content
  end
end

Instance Method Summary collapse

Instance Method Details

#advance_time(window, ms) ⇒ Object

Advance the window's virtual clock. Timers that come due and any queued microtasks are run as part of the advance. Use this to test code that schedules work with setTimeout / setInterval.

Parameters:

  • window (Dommy::Window)
  • ms (Integer)

    milliseconds to advance



74
75
76
# File 'lib/dommy/test_helpers.rb', line 74

def advance_time(window, ms)
  window.scheduler.advance_time(ms)
end

#flush_microtasks(window) ⇒ Object

Drain pending microtasks on the window's scheduler. Use this after a mutation if you need MutationObserver callbacks (scheduled as microtasks) to fire before your assertions.

Parameters:



64
65
66
# File 'lib/dommy/test_helpers.rb', line 64

def flush_microtasks(window)
  window.scheduler.drain_microtasks
end

#make_window(body_html = "") {|window| ... } ⇒ Dommy::Window

Build a fresh Window with the given body HTML. When a block is given, yields the window first; the same window is returned in both cases so callers can choose their style.

Parameters:

  • body_html (String) (defaults to: "")

    HTML to insert inside

Yield Parameters:

Returns:



52
53
54
55
56
57
# File 'lib/dommy/test_helpers.rb', line 52

def make_window(body_html = "")
  window = Dommy::Window.new
  window.document.body.inner_html = body_html.to_s
  yield window if block_given?
  window
end

#parse_html(html = "") ⇒ Dommy::Document

Parse an HTML string into a fresh Document and return it.

When the input starts with <!doctype or <html>, it is parsed as a full HTML document (preserving , , etc.). Otherwise the input is treated as a body fragment and inserted into a fresh document's <body>.</p> </div> </div> <div class="tags"> <p class="tag_title">Parameters:</p> <ul class="param"> <li> <span class='name'>html</span> <span class='type'>(<tt>String</tt>)</span> <em class="default">(defaults to: <tt>""</tt>)</em> — <div class='inline'><p>HTML to parse (full document or body fragment)</p></div> </li> </ul> <p class="tag_title">Returns:</p> <ul class="return"> <li> <span class='type'>(<tt><span class='object_link'><a href="Document.html" title="Dommy::Document (class)">Dommy::Document</a></span></tt>)</span> — <div class='inline'><p>a fresh Document with the parsed content</p></div> </li> </ul> </div><table class="source_code"> <tr> <td> <pre class="lines"> 41 42 43</pre> </td> <td> <pre class="code"><span class="info file"># File 'lib/dommy/test_helpers.rb', line 41</span> <span class='kw'>def</span> <span class='id identifier rubyid_parse_html'>parse_html</span><span class='lparen'>(</span><span class='id identifier rubyid_html'>html</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_end'>"</span></span><span class='rparen'>)</span> <span class='const'><span class='object_link'><a href="../Dommy.html" title="Dommy (module)">Dommy</a></span></span><span class='period'>.</span><span class='id identifier rubyid_parse'><span class='object_link'><a href="../Dommy.html#parse-class_method" title="Dommy.parse (method)">parse</a></span></span><span class='lparen'>(</span><span class='id identifier rubyid_html'>html</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_document'>document</span> <span class='kw'>end</span></pre> </td> </tr> </table> </div> </div> </div> <div id="footer"> Generated on Mon Jul 13 13:56:42 2026 by <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a> 0.9.44 (ruby-4.0.2). </div> </div> </body> </html>