Class: StimulusSpec::Matchers::HaveStimulusTarget
- Inherits:
-
Object
- Object
- StimulusSpec::Matchers::HaveStimulusTarget
- Defined in:
- lib/stimulus_spec/matchers/have_stimulus_target.rb
Instance Method Summary collapse
- #description ⇒ Object
- #does_not_match?(subject) ⇒ Boolean
- #failure_message ⇒ Object
- #failure_message_when_negated ⇒ Object
-
#initialize(controller, target) ⇒ HaveStimulusTarget
constructor
A new instance of HaveStimulusTarget.
- #matches?(subject) ⇒ Boolean
Constructor Details
#initialize(controller, target) ⇒ HaveStimulusTarget
Returns a new instance of HaveStimulusTarget.
6 7 8 9 10 |
# File 'lib/stimulus_spec/matchers/have_stimulus_target.rb', line 6 def initialize(controller, target) @controller = controller.to_s @target = target.to_s @attr = "data-#{@controller}-target" end |
Instance Method Details
#description ⇒ Object
34 35 36 |
# File 'lib/stimulus_spec/matchers/have_stimulus_target.rb', line 34 def description "have Stimulus target \"#{@target}\" for controller \"#{@controller}\"" end |
#does_not_match?(subject) ⇒ Boolean
18 19 20 |
# File 'lib/stimulus_spec/matchers/have_stimulus_target.rb', line 18 def does_not_match?(subject) !matches?(subject) end |
#failure_message ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/stimulus_spec/matchers/have_stimulus_target.rb', line 22 def found_targets = @doc.css("[#{@attr}]").flat_map { |el| el[@attr].split } msg = "expected to find an element with #{@attr}=\"#{@target}\"" msg += "\n found targets: #{found_targets.uniq.map { |t| "\"#{t}\"" }.join(", ")}" if found_targets.any? msg += "\n in:\n#{snippet}" msg end |
#failure_message_when_negated ⇒ Object
30 31 32 |
# File 'lib/stimulus_spec/matchers/have_stimulus_target.rb', line 30 def "expected not to find an element with #{@attr}=\"#{@target}\" but found one" end |
#matches?(subject) ⇒ Boolean
12 13 14 15 16 |
# File 'lib/stimulus_spec/matchers/have_stimulus_target.rb', line 12 def matches?(subject) @body = extract_body(subject) @doc = Nokogiri::HTML5.fragment(@body) !@doc.at_css("[#{@attr}~='#{@target}']").nil? end |