Class: TurboRspec::Matchers::HaveBroadcastedTurboStreamTo

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

Instance Method Summary collapse

Constructor Details

#initialize(stream_or_object) ⇒ HaveBroadcastedTurboStreamTo

Returns a new instance of HaveBroadcastedTurboStreamTo.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/turbo_rspec/matchers/have_broadcasted_turbo_stream_to.rb', line 8

def initialize(stream_or_object)
  @stream_or_object = stream_or_object
  @action = nil
  @target = nil
  @target_all = nil
  @content = nil
  @partial = nil
  @attributes = {}
  @children_only = false
  @expected_count = nil
  @count_type = :at_least
end

Instance Method Details

#at_least(n) ⇒ Object



79
80
81
82
83
# File 'lib/turbo_rspec/matchers/have_broadcasted_turbo_stream_to.rb', line 79

def at_least(n)
  @expected_count = n
  @count_type = :at_least
  self
end

#at_most(n) ⇒ Object



85
86
87
88
89
# File 'lib/turbo_rspec/matchers/have_broadcasted_turbo_stream_to.rb', line 85

def at_most(n)
  @expected_count = n
  @count_type = :at_most
  self
end

#children_onlyObject



58
59
60
61
# File 'lib/turbo_rspec/matchers/have_broadcasted_turbo_stream_to.rb', line 58

def children_only
  @children_only = true
  self
end

#descriptionObject



120
121
122
# File 'lib/turbo_rspec/matchers/have_broadcasted_turbo_stream_to.rb', line 120

def description
  "broadcast a turbo stream to #{stream_name.inspect}#{constraint_description}"
end

#does_not_match?(block) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/turbo_rspec/matchers/have_broadcasted_turbo_stream_to.rb', line 108

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

#exactly(n) ⇒ Object



73
74
75
76
77
# File 'lib/turbo_rspec/matchers/have_broadcasted_turbo_stream_to.rb', line 73

def exactly(n)
  @expected_count = n
  @count_type = :exactly
  self
end

#failure_messageObject



112
113
114
# File 'lib/turbo_rspec/matchers/have_broadcasted_turbo_stream_to.rb', line 112

def failure_message
  "expected block to broadcast a turbo stream to #{stream_name.inspect}#{constraint_description}#{count_description}\n#{found_message}"
end

#failure_message_when_negatedObject



116
117
118
# File 'lib/turbo_rspec/matchers/have_broadcasted_turbo_stream_to.rb', line 116

def failure_message_when_negated
  "expected block not to broadcast a turbo stream to #{stream_name.inspect}#{constraint_description}"
end

#matches?(block) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
# File 'lib/turbo_rspec/matchers/have_broadcasted_turbo_stream_to.rb', line 99

def matches?(block)
  before = snapshot
  block.call
  @new_broadcasts = snapshot - before
  @all_streams = parse_all_streams(@new_broadcasts)
  @matching = @new_broadcasts.select { |msg| message_matches?(msg) }
  count_matches?(@matching.size)
end

#onceObject

Count qualifiers



65
66
67
# File 'lib/turbo_rspec/matchers/have_broadcasted_turbo_stream_to.rb', line 65

def once
  exactly(1)
end

#rendering(partial) ⇒ Object



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

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

#supports_block_expectations?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/turbo_rspec/matchers/have_broadcasted_turbo_stream_to.rb', line 95

def supports_block_expectations?
  true
end

#targeting(dom_id) ⇒ Object



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

def targeting(dom_id)
  @target = dom_id.to_s
  self
end

#targeting_all(selector) ⇒ Object



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

def targeting_all(selector)
  @target_all = selector.to_s
  self
end

#timesObject



91
92
93
# File 'lib/turbo_rspec/matchers/have_broadcasted_turbo_stream_to.rb', line 91

def times
  self
end

#twiceObject



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

def twice
  exactly(2)
end

#with_action(action) ⇒ Object

Stream constraints (mirrors HaveTurboStream)



23
24
25
26
27
28
29
30
31
# File 'lib/turbo_rspec/matchers/have_broadcasted_turbo_stream_to.rb', line 23

def with_action(action)
  action_str = action.to_s
  unless TurboRspec.known_actions.include?(action_str)
    raise ArgumentError, "Unknown Turbo stream action #{action_str.inspect}. " \
      "Register custom actions with TurboRspec.register_action(:#{action_str})."
  end
  @action = action_str
  self
end

#with_attributes(attrs) ⇒ Object



53
54
55
56
# File 'lib/turbo_rspec/matchers/have_broadcasted_turbo_stream_to.rb', line 53

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

#with_content(text) ⇒ Object



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

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