Class: TurboRspec::Matchers::HaveStimulusController

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

Overview

RSpec matcher for asserting that a response body contains an element with the given Stimulus controller (+data-controller+ contains the name as a space-separated token).

Examples:

expect(response).to have_stimulus_controller("hello")
expect(response).not_to have_stimulus_controller("missing")

Instance Method Summary collapse

Constructor Details

#initialize(controller_name) ⇒ HaveStimulusController

Returns a new instance of HaveStimulusController.



15
16
17
# File 'lib/turbo_rspec/matchers/have_stimulus_controller.rb', line 15

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

Instance Method Details

#descriptionString

Returns:

  • (String)


43
44
45
# File 'lib/turbo_rspec/matchers/have_stimulus_controller.rb', line 43

def description
  "have Stimulus controller #{@controller_name.inspect}"
end

#does_not_match?(response_or_body) ⇒ Boolean

Parameters:

  • response_or_body (#body, String)

Returns:

  • (Boolean)


28
29
30
# File 'lib/turbo_rspec/matchers/have_stimulus_controller.rb', line 28

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

#failure_messageString

Returns:

  • (String)


33
34
35
# File 'lib/turbo_rspec/matchers/have_stimulus_controller.rb', line 33

def failure_message
  "expected response to have Stimulus controller #{@controller_name.inspect}"
end

#failure_message_when_negatedString

Returns:

  • (String)


38
39
40
# File 'lib/turbo_rspec/matchers/have_stimulus_controller.rb', line 38

def failure_message_when_negated
  "expected response not to have Stimulus controller #{@controller_name.inspect}"
end

#matches?(response_or_body) ⇒ Boolean

Parameters:

  • response_or_body (#body, String)

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/turbo_rspec/matchers/have_stimulus_controller.rb', line 21

def matches?(response_or_body)
  @body = extract_body(response_or_body)
  Nokogiri::HTML5.fragment(@body).css("[data-controller~='#{@controller_name}']").any?
end