Class: StimulusSpec::Matchers::HaveStimulusAction

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

Instance Method Summary collapse

Constructor Details

#initialize(descriptor) ⇒ HaveStimulusAction

Returns a new instance of HaveStimulusAction.



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

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

Instance Method Details

#descriptionObject



44
45
46
# File 'lib/stimulus_spec/matchers/have_stimulus_action.rb', line 44

def description
  "have Stimulus action \"#{@descriptor}\""
end

#does_not_match?(subject) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/stimulus_spec/matchers/have_stimulus_action.rb', line 28

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

#failure_messageObject



32
33
34
35
36
37
38
# File 'lib/stimulus_spec/matchers/have_stimulus_action.rb', line 32

def failure_message
  found_actions = @doc.css("[data-action]").flat_map { |el| el["data-action"].split }
  msg = "expected to find an element with data-action=\"#{@descriptor}\""
  msg += "\n  found actions: #{found_actions.uniq.map { |a| "\"#{a}\"" }.join(", ")}" if found_actions.any?
  msg += "\n  in:\n#{snippet}"
  msg
end

#failure_message_when_negatedObject



40
41
42
# File 'lib/stimulus_spec/matchers/have_stimulus_action.rb', line 40

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

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/stimulus_spec/matchers/have_stimulus_action.rb', line 15

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

  if @descriptor.include?("->")
    !root.at_css("[data-action~='#{@descriptor}']").nil?
  else
    !root.at_css("[data-action*='#{@descriptor}']").nil?
  end
end

#within(selector) ⇒ Object



10
11
12
13
# File 'lib/stimulus_spec/matchers/have_stimulus_action.rb', line 10

def within(selector)
  @scope = selector
  self
end