Class: StimulusSpec::Matchers::HaveStimulusAction
- Inherits:
-
Object
- Object
- StimulusSpec::Matchers::HaveStimulusAction
- Defined in:
- lib/stimulus_spec/matchers/have_stimulus_action.rb
Instance Method Summary collapse
- #description ⇒ Object
- #does_not_match?(subject) ⇒ Boolean
- #failure_message ⇒ Object
- #failure_message_when_negated ⇒ Object
-
#initialize(descriptor) ⇒ HaveStimulusAction
constructor
A new instance of HaveStimulusAction.
- #matches?(subject) ⇒ Boolean
- #within(selector) ⇒ Object
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
#description ⇒ Object
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
28 29 30 |
# File 'lib/stimulus_spec/matchers/have_stimulus_action.rb', line 28 def does_not_match?(subject) !matches?(subject) end |
#failure_message ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/stimulus_spec/matchers/have_stimulus_action.rb', line 32 def 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_negated ⇒ Object
40 41 42 |
# File 'lib/stimulus_spec/matchers/have_stimulus_action.rb', line 40 def "expected not to find an element with data-action=\"#{@descriptor}\" but found one" end |
#matches?(subject) ⇒ 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 |