Class: TurboRspec::Matchers::HaveStimulusAction

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

Overview

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

Accepts a full descriptor (+click->hello#greet+) or a shorthand without an event (+hello#greet+), which matches any event prefix.

Examples:

Full descriptor

expect(response).to have_stimulus_action("click->hello#greet")

Shorthand (any event)

expect(response).to have_stimulus_action("hello#greet")

Instance Method Summary collapse

Constructor Details

#initialize(action_descriptor) ⇒ HaveStimulusAction

Returns a new instance of HaveStimulusAction.



20
21
22
# File 'lib/turbo_rspec/matchers/have_stimulus_action.rb', line 20

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

Instance Method Details

#descriptionString

Returns:

  • (String)


48
49
50
# File 'lib/turbo_rspec/matchers/have_stimulus_action.rb', line 48

def description
  "have Stimulus action #{@action_descriptor.inspect}"
end

#does_not_match?(response_or_body) ⇒ Boolean

Parameters:

  • response_or_body (#body, String)

Returns:

  • (Boolean)


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

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

#failure_messageString

Returns:

  • (String)


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

def failure_message
  "expected response to have Stimulus action #{@action_descriptor.inspect}"
end

#failure_message_when_negatedString

Returns:

  • (String)


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

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

#matches?(response_or_body) ⇒ Boolean

Parameters:

  • response_or_body (#body, String)

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/turbo_rspec/matchers/have_stimulus_action.rb', line 26

def matches?(response_or_body)
  @body = extract_body(response_or_body)
  Nokogiri::HTML5.fragment(@body).css(selector).any?
end