Class: StimulusSpec::Capybara::Matchers::HaveStimulusTarget

Inherits:
Object
  • Object
show all
Defined in:
lib/stimulus_spec/capybara/matchers/have_stimulus_target.rb

Overview

Capybara matcher for +data-controller-target+ attributes.

Examples:

expect(page).to have_stimulus_target("hello", "name")

Instance Method Summary collapse

Constructor Details

#initialize(controller, target) ⇒ HaveStimulusTarget

Returns a new instance of HaveStimulusTarget.

Parameters:

  • controller (String)

    controller name

  • target (String)

    target name



13
14
15
16
# File 'lib/stimulus_spec/capybara/matchers/have_stimulus_target.rb', line 13

def initialize(controller, target)
  @controller = controller.to_s
  @target = target.to_s
end

Instance Method Details

#descriptionObject



44
45
46
# File 'lib/stimulus_spec/capybara/matchers/have_stimulus_target.rb', line 44

def description
  "have Stimulus target \"#{@target}\" for controller \"#{@controller}\""
end

#does_not_match?(page) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/stimulus_spec/capybara/matchers/have_stimulus_target.rb', line 32

def does_not_match?(page)
  page.has_no_css?(scoped_selector, wait: 0)
end

#failure_messageObject



36
37
38
# File 'lib/stimulus_spec/capybara/matchers/have_stimulus_target.rb', line 36

def failure_message
  "expected to find an element with data-#{@controller}-target=\"#{@target}\" on the page"
end

#failure_message_when_negatedObject



40
41
42
# File 'lib/stimulus_spec/capybara/matchers/have_stimulus_target.rb', line 40

def failure_message_when_negated
  "expected not to find an element with data-#{@controller}-target=\"#{@target}\" on the page"
end

#matches?(page) ⇒ Boolean

Parameters:

  • page (Capybara::Session, Capybara::Node::Simple)

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/stimulus_spec/capybara/matchers/have_stimulus_target.rb', line 27

def matches?(page)
  @page = page
  page.has_css?(scoped_selector, wait: 0)
end

#within(selector) ⇒ self

Parameters:

  • selector (String)

    CSS selector for the scope element

Returns:

  • (self)


20
21
22
23
# File 'lib/stimulus_spec/capybara/matchers/have_stimulus_target.rb', line 20

def within(selector)
  @scope = selector
  self
end