Class: TurboRspec::Matchers::HaveTurboFrame

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

Overview

RSpec matcher for asserting that a response body contains a ++ element. Use in request or controller specs.

Examples:

Basic usage

expect(response).to have_turbo_frame

With constraints

expect(response).to have_turbo_frame
  .with_id("messages")
  .with_content("Hello")

See Also:

Instance Method Summary collapse

Constructor Details

#initializeHaveTurboFrame

Returns a new instance of HaveTurboFrame.



20
21
22
23
24
25
# File 'lib/turbo_rspec/matchers/have_turbo_frame.rb', line 20

def initialize
  @id = nil
  @content = nil
  @partial = nil
  @attributes = {}
end

Instance Method Details

#descriptionObject



83
84
85
# File 'lib/turbo_rspec/matchers/have_turbo_frame.rb', line 83

def description
  "have turbo frame#{constraint_description}"
end

#does_not_match?(response_or_body) ⇒ Boolean

Parameters:

  • response_or_body (#body, String)

Returns:

  • (Boolean)


69
70
71
# File 'lib/turbo_rspec/matchers/have_turbo_frame.rb', line 69

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

#failure_messageString

Returns:

  • (String)


74
75
76
# File 'lib/turbo_rspec/matchers/have_turbo_frame.rb', line 74

def failure_message
  "expected response to contain a turbo frame#{constraint_description}\n#{found_frames_message}"
end

#failure_message_when_negatedString

Returns:

  • (String)


79
80
81
# File 'lib/turbo_rspec/matchers/have_turbo_frame.rb', line 79

def failure_message_when_negated
  "expected response not to contain a turbo frame#{constraint_description}"
end

#matches?(response_or_body) ⇒ Boolean

Parameters:

  • response_or_body (#body, String)

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/turbo_rspec/matchers/have_turbo_frame.rb', line 61

def matches?(response_or_body)
  @body = extract_body(response_or_body)
  @frames = parse_frames(@body)
  @frames.any? { |frame| frame_matches?(frame) }
end

#rendering(partial) ⇒ self

Constrains the match to frames whose HTML includes the given partial path.

Parameters:

  • partial (String)

Returns:

  • (self)


46
47
48
49
# File 'lib/turbo_rspec/matchers/have_turbo_frame.rb', line 46

def rendering(partial)
  @partial = partial.to_s
  self
end

#with_attributes(attrs) ⇒ self

Constrains the match to frames that have all of the given HTML attributes.

Parameters:

  • attrs (Hash)

Returns:

  • (self)


54
55
56
57
# File 'lib/turbo_rspec/matchers/have_turbo_frame.rb', line 54

def with_attributes(attrs)
  @attributes = attrs.transform_keys(&:to_s).transform_values(&:to_s)
  self
end

#with_content(text) ⇒ self

Constrains the match to frames whose content includes the given text.

Parameters:

  • text (String)

Returns:

  • (self)


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

def with_content(text)
  @content = text.to_s
  self
end

#with_id(id) ⇒ self

Constrains the match to frames with the given id attribute.

Parameters:

  • id (String)

Returns:

  • (self)


30
31
32
33
# File 'lib/turbo_rspec/matchers/have_turbo_frame.rb', line 30

def with_id(id)
  @id = id.to_s
  self
end