Class: StimulusSpec::Matchers::HaveStimulusController

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

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ HaveStimulusController

Returns a new instance of HaveStimulusController.



6
7
8
# File 'lib/stimulus_spec/matchers/have_stimulus_controller.rb', line 6

def initialize(name)
  @name = name.to_s
end

Instance Method Details

#descriptionObject



34
35
36
# File 'lib/stimulus_spec/matchers/have_stimulus_controller.rb', line 34

def description
  "have Stimulus controller \"#{@name}\""
end

#does_not_match?(subject) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#failure_messageObject



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

def failure_message
  msg = "expected to find an element with data-controller=\"#{@name}\""
  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



30
31
32
# File 'lib/stimulus_spec/matchers/have_stimulus_controller.rb', line 30

def failure_message_when_negated
  "expected not to find an element with data-controller=\"#{@name}\" but found one"
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
# File 'lib/stimulus_spec/matchers/have_stimulus_controller.rb', line 10

def matches?(subject)
  @body = extract_body(subject)
  @doc = Nokogiri::HTML5.fragment(@body)
  @found_controllers = @doc.css("[data-controller]").flat_map { |el| el["data-controller"].split }
  !@doc.at_css("[data-controller~='#{@name}']").nil?
end