rails/actioncable/test/server/broadcasting_test.rb
Bart de Water 95b6fbd00f Stop building AS::Notifications::Event manually
It's possible since Rails 6 (3ea2857943dc294d7809930b4cc5b318b9c39577) to let the framework create Event objects, but the guides and docs weren't updated to lead with this example.

Manually instantiating an Event doesn't record CPU time and allocations, I've seen it more than once that people copy-pasting the example code get confused about these stats returning 0. The tests here show that - just like the apps I've worked on - the old pattern keeps getting copy-pasted.
2023-09-29 12:34:23 -04:00

55 lines
1.7 KiB
Ruby

# frozen_string_literal: true
require "test_helper"
require "stubs/test_server"
class BroadcastingTest < ActionCable::TestCase
test "fetching a broadcaster converts the broadcasting queue to a string" do
broadcasting = :test_queue
server = TestServer.new
broadcaster = server.broadcaster_for(broadcasting)
assert_equal "test_queue", broadcaster.broadcasting
end
test "broadcast generates notification" do
server = TestServer.new
events = []
ActiveSupport::Notifications.subscribe("broadcast.action_cable") { |event| events << event }
broadcasting = "test_queue"
message = { body: "test message" }
server.broadcast(broadcasting, message)
assert_equal 1, events.length
assert_equal "broadcast.action_cable", events[0].name
assert_equal broadcasting, events[0].payload[:broadcasting]
assert_equal message, events[0].payload[:message]
assert_equal ActiveSupport::JSON, events[0].payload[:coder]
ensure
ActiveSupport::Notifications.unsubscribe "broadcast.action_cable"
end
test "broadcaster from broadcaster_for generates notification" do
server = TestServer.new
events = []
ActiveSupport::Notifications.subscribe("broadcast.action_cable") { |event| events << event }
broadcasting = "test_queue"
message = { body: "test message" }
broadcaster = server.broadcaster_for(broadcasting)
broadcaster.broadcast(message)
assert_equal 1, events.length
assert_equal "broadcast.action_cable", events[0].name
assert_equal broadcasting, events[0].payload[:broadcasting]
assert_equal message, events[0].payload[:message]
assert_equal ActiveSupport::JSON, events[0].payload[:coder]
ensure
ActiveSupport::Notifications.unsubscribe "broadcast.action_cable"
end
end