-
Notifications
You must be signed in to change notification settings - Fork 329
/
Copy pathbroadcasts_test.rb
150 lines (113 loc) · 5.07 KB
/
broadcasts_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
require "application_system_test_case"
class BroadcastsTest < ApplicationSystemTestCase
include ActiveJob::TestHelper
test "Message broadcasts Turbo Streams" do
visit messages_path
wait_for_stream_to_be_connected
assert_broadcasts_text "Message 1", to: :messages do |text, target|
Message.create(content: text).broadcast_append_to(target)
end
end
test "Message broadcasts with html: render option" do
visit messages_path
wait_for_stream_to_be_connected
assert_broadcasts_text "Hello, with html: option", to: :messages do |text, target|
Message.create(content: "Ignored").broadcast_append_to(target, html: text)
end
end
test "Message broadcasts with renderable: render option" do
visit messages_path
wait_for_stream_to_be_connected
assert_broadcasts_text "Test message", to: :messages do |text, target|
Message.create(content: "Ignored").broadcast_append_to(target, renderable: MessageComponent.new(text))
end
end
test "Does not render the layout twice when passed a component" do
visit messages_path
wait_for_stream_to_be_connected
Message.create(content: "Ignored").broadcast_append_to(:messages, renderable: MessageComponent.new("test"))
assert_selector("title", count: 1, visible: false, text: "Dummy")
end
test "Message broadcasts with extra attributes to turbo stream tag" do
visit messages_path
wait_for_stream_to_be_connected
assert_broadcasts_text "Message 1", to: :messages do |text, target|
Message.create(content: text).broadcast_action_to(target, action: :append, attributes: { "data-foo": "bar" })
end
end
test "Message broadcasts with correct extra attributes to turbo stream tag" do
visit messages_path
wait_for_stream_to_be_connected
assert_forwards_turbo_stream_tag_attribute attr_key: "data-foo", attr_value: "bar", to: :messages do |attr_key, attr_value, target|
Message.create(content: text).broadcast_action_to(target, action: :test, attributes: { attr_key => attr_value })
end
end
test "Message broadcasts with no rendering" do
visit messages_path
wait_for_stream_to_be_connected
assert_forwards_turbo_stream_tag_attribute attr_key: "data-foo", attr_value: "bar", to: :messages do |attr_key, attr_value, target|
Message.create(content: text).broadcast_action_to(target, action: :test, render: false, partial: "non_existant", attributes: { attr_key => attr_value })
end
end
test "Message broadcasts later with extra attributes to turbo stream tag" do
visit messages_path
wait_for_stream_to_be_connected
perform_enqueued_jobs do
assert_broadcasts_text "Message 1", to: :messages do |text, target|
Message.create(content: text).broadcast_action_later_to(target, action: :append, attributes: { "data-foo": "bar" })
end
end
end
test "Message broadcasts later with correct extra attributes to turbo stream tag" do
visit messages_path
wait_for_stream_to_be_connected
perform_enqueued_jobs do
assert_forwards_turbo_stream_tag_attribute attr_key: "data-foo", attr_value: "bar", to: :messages do |attr_key, attr_value, target|
Message.create(content: text).broadcast_action_later_to(target, action: :test, attributes: { attr_key => attr_value })
end
end
end
test "Message broadcasts later with no rendering" do
visit messages_path
wait_for_stream_to_be_connected
perform_enqueued_jobs do
assert_forwards_turbo_stream_tag_attribute attr_key: "data-foo", attr_value: "bar", to: :messages do |attr_key, attr_value, target|
Message.create(content: text).broadcast_action_to(target, action: :test, render: false, partial: "non_existant", attributes: { attr_key => attr_value })
end
end
end
test "Users::Profile broadcasts Turbo Streams" do
visit users_profiles_path
wait_for_stream_to_be_connected
assert_broadcasts_text "Profile 1", to: :users_profiles do |text, target|
Users::Profile.new(id: 1, name: text).broadcast_append_to(target)
end
end
test "passing extra parameters to channel" do
visit section_messages_path
wait_for_stream_to_be_connected
assert_broadcasts_text "In a section", to: :messages do |text|
Message.create(content: text).broadcast_append_to(:important_messages)
end
end
private
def wait_for_stream_to_be_connected
assert_selector "turbo-cable-stream-source[connected]", visible: false
end
def assert_broadcasts_text(text, to:, &block)
within(:element, id: to) { assert_no_text text }
[text, to].yield_self(&block)
within(:element, id: to) { assert_text text }
end
def assert_forwards_turbo_stream_tag_attribute(attr_key:, attr_value:, to:, &block)
execute_script(<<~SCRIPT)
Turbo.StreamActions.test = function () {
const attribute = this.getAttribute('#{attr_key}')
document.getElementById('#{to}').innerHTML = attribute
}
SCRIPT
within(:element, id: to) { assert_no_text attr_value }
[attr_key, attr_value, to].yield_self(&block)
within(:element, id: to) { assert_text attr_value }
end
end