|
1 | 1 | require "application_system_test_case"
|
2 | 2 |
|
3 | 3 | class BroadcastsTest < ApplicationSystemTestCase
|
4 |
| - setup { Message.delete_all } |
5 |
| - |
6 |
| - include ActionCable::TestHelper |
7 |
| - |
8 | 4 | test "Message broadcasts Turbo Streams" do
|
9 | 5 | visit messages_path
|
10 |
| - wait_for_subscriber |
| 6 | + subscribe_to_broadcasts |
11 | 7 |
|
12 |
| - assert_text "Messages" |
13 |
| - assert_broadcasts_text "Message 1" do |text| |
14 |
| - Message.create(content: text).broadcast_append_to(:messages) |
| 8 | + assert_broadcasts_text "Message 1", to: :messages do |text, target| |
| 9 | + Message.create(content: text).broadcast_append_to(target) |
15 | 10 | end
|
16 | 11 | end
|
17 | 12 |
|
18 |
| - test "New messages update the message count with html" do |
| 13 | + test "Message broadcasts with html: render option" do |
19 | 14 | visit messages_path
|
20 |
| - wait_for_subscriber |
21 |
| - |
22 |
| - assert_text "Messages" |
23 |
| - message = Message.create(content: "A new message") |
| 15 | + subscribe_to_broadcasts |
24 | 16 |
|
25 |
| - message.broadcast_update_to(:messages, target: "message-count", |
26 |
| - html: "#{Message.count} messages sent") |
27 |
| - assert_selector("#message-count", text: Message.count, wait: 10) |
| 17 | + assert_broadcasts_text "Hello, with html: option", to: :messages do |text, target| |
| 18 | + Message.create(content: "Ignored").broadcast_append_to(target, html: text) |
| 19 | + end |
28 | 20 | end
|
29 | 21 |
|
30 | 22 | test "Users::Profile broadcasts Turbo Streams" do
|
31 | 23 | visit users_profiles_path
|
32 |
| - wait_for_subscriber |
| 24 | + subscribe_to_broadcasts |
33 | 25 |
|
34 |
| - assert_text "Users::Profiles" |
35 |
| - assert_broadcasts_text "Profile 1" do |text| |
36 |
| - Users::Profile.new(id: 1, name: text).broadcast_append_to(:users_profiles) |
| 26 | + assert_broadcasts_text "Profile 1", to: :users_profiles do |text, target| |
| 27 | + Users::Profile.new(id: 1, name: text).broadcast_append_to(target) |
37 | 28 | end
|
38 | 29 | end
|
39 | 30 |
|
40 | 31 | test "passing extra parameters to channel" do
|
41 | 32 | visit echo_messages_path
|
42 |
| - wait_for_subscriber |
43 | 33 |
|
44 |
| - assert_text "Hello, world!", wait: 100 |
| 34 | + assert_broadcasts_text "Hello, world!", to: :messages do |
| 35 | + subscribe_to_broadcasts |
| 36 | + end |
45 | 37 | end
|
46 | 38 |
|
47 | 39 | private
|
48 | 40 |
|
49 |
| - def assert_broadcasts_text(text, wait: 5, &block) |
50 |
| - assert_no_text text |
51 |
| - perform_enqueued_jobs { block.call(text) } |
52 |
| - assert_text text, wait: wait |
| 41 | + def subscribe_to_broadcasts |
| 42 | + click_on "Start listening for broadcasts" |
| 43 | + |
| 44 | + assert_no_button "Start listening for broadcasts" |
| 45 | + |
| 46 | + Timeout.timeout(Capybara.default_max_wait_time) { wait_for_subscriber } |
| 47 | + end |
| 48 | + |
| 49 | + def assert_broadcasts_text(text, to:, &block) |
| 50 | + within(:element, id: to) { assert_no_text text } |
| 51 | + |
| 52 | + [text, to].yield_self(&block) |
| 53 | + |
| 54 | + within(:element, id: to) { assert_text text } |
53 | 55 | end
|
54 | 56 |
|
55 |
| - def wait_for_subscriber(timeout: 10) |
56 |
| - time = Time.now |
| 57 | + def wait_for_subscriber |
57 | 58 | loop do
|
58 |
| - subscriber_map = pubsub_adapter.instance_variable_get(:@subscriber_map) |
| 59 | + subscriber_map = ActionCable.server.pubsub.instance_variable_get(:@subscriber_map) |
59 | 60 | if subscriber_map.is_a?(ActionCable::SubscriptionAdapter::SubscriberMap)
|
60 | 61 | subscribers = subscriber_map.instance_variable_get(:@subscribers)
|
61 | 62 | sync = subscriber_map.instance_variable_get(:@sync)
|
62 | 63 | sync.synchronize do
|
63 | 64 | return unless subscribers.empty?
|
64 | 65 | end
|
65 | 66 | end
|
66 |
| - assert_operator(Time.now - time, :<, timeout, "subscriber waiting timed out") |
67 | 67 | sleep 0.1
|
68 | 68 | end
|
69 | 69 | end
|
70 |
| - |
71 | 70 | end
|
0 commit comments