Class: StimulusSpec::Matchers::HaveStimulusController

Inherits:
Object
  • Object
show all
Defined in:
lib/stimulus_spec/matchers/have_stimulus_controller.rb

Overview

Asserts that rendered HTML contains a +data-controller+ attribute with the given name(s).

Examples:

Single controller

expect(response).to have_stimulus_controller("hello")

Multiple controllers on one element

expect(response).to have_stimulus_controller("hello", "clipboard")

Scoped

expect(response).to have_stimulus_controller("search").within(".search-form")

Instance Method Summary collapse

Constructor Details

#initialize(*names) ⇒ HaveStimulusController

Returns a new instance of HaveStimulusController.

Parameters:

  • names (Array<String>)

    one or more controller names to match



16
17
18
# File 'lib/stimulus_spec/matchers/have_stimulus_controller.rb', line 16

def initialize(*names)
  @names = names.map(&:to_s)
end

Instance Method Details

#descriptionObject



61
62
63
64
# File 'lib/stimulus_spec/matchers/have_stimulus_controller.rb', line 61

def description
  label = @names.map { |n| "\"#{n}\"" }.join(", ")
  "have Stimulus controller #{label}"
end

#does_not_match?(subject) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/stimulus_spec/matchers/have_stimulus_controller.rb', line 42

def does_not_match?(subject)
  !matches?(subject)
end

#failure_messageObject



46
47
48
49
50
51
52
53
54
# File 'lib/stimulus_spec/matchers/have_stimulus_controller.rb', line 46

def failure_message
  label = @names.map { |n| "\"#{n}\"" }.join(", ")
  msg = "expected to find an element with data-controller=#{label}"
  if @found_controllers&.any?
    msg += "\n  found controllers: #{@found_controllers.uniq.map { |c| "\"#{c}\"" }.join(", ")}"
  end
  msg += "\n  in:\n#{snippet}"
  msg
end

#failure_message_when_negatedObject



56
57
58
59
# File 'lib/stimulus_spec/matchers/have_stimulus_controller.rb', line 56

def failure_message_when_negated
  label = @names.map { |n| "\"#{n}\"" }.join(", ")
  "expected not to find an element with data-controller=#{label} but found one"
end

#matches?(subject) ⇒ Boolean

Parameters:

  • subject (#body, String)

    response object or HTML string

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
# File 'lib/stimulus_spec/matchers/have_stimulus_controller.rb', line 31

def matches?(subject)
  @body = extract_body(subject)
  @doc = Nokogiri::HTML5.fragment(@body)
  root = search_root
  return false unless root

  @found_controllers = root.css("[data-controller]").flat_map { |el| el["data-controller"].split }
  selector = @names.map { |n| "[data-controller~='#{n}']" }.join
  !root.at_css(selector).nil?
end

#within(selector) ⇒ self

Restricts matching to descendants of the given CSS selector.

Parameters:

  • selector (String)

    CSS selector for the scope element

Returns:

  • (self)


24
25
26
27
# File 'lib/stimulus_spec/matchers/have_stimulus_controller.rb', line 24

def within(selector)
  @scope = selector
  self
end