Class: StimulusSpec::Matchers::HaveStimulusController
- Inherits:
-
Object
- Object
- StimulusSpec::Matchers::HaveStimulusController
- Defined in:
- lib/stimulus_spec/matchers/have_stimulus_controller.rb
Instance Method Summary collapse
- #description ⇒ Object
- #does_not_match?(subject) ⇒ Boolean
- #failure_message ⇒ Object
- #failure_message_when_negated ⇒ Object
-
#initialize(name) ⇒ HaveStimulusController
constructor
A new instance of HaveStimulusController.
- #matches?(subject) ⇒ Boolean
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
#description ⇒ Object
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
17 18 19 |
# File 'lib/stimulus_spec/matchers/have_stimulus_controller.rb', line 17 def does_not_match?(subject) !matches?(subject) end |
#failure_message ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/stimulus_spec/matchers/have_stimulus_controller.rb', line 21 def 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_negated ⇒ Object
30 31 32 |
# File 'lib/stimulus_spec/matchers/have_stimulus_controller.rb', line 30 def "expected not to find an element with data-controller=\"#{@name}\" but found one" end |
#matches?(subject) ⇒ 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 |