From fa914157b0187c98996b7513ed009081867c4cef Mon Sep 17 00:00:00 2001 From: Tom de Bruijn Date: Wed, 10 Jul 2024 09:03:44 +0200 Subject: [PATCH] Test Action Cable action in Rails app Make sure we add a scenario that actually triggers an Action Cable action so that we can test if it's instrumented. --- ruby/rails7-sidekiq/app/Gemfile.lock | 2 +- ruby/rails7-sidekiq/app/app/channels/chat_room_channel.rb | 8 ++++++++ .../app/app/javascript/channels/chat_room_channel.js | 3 +++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/ruby/rails7-sidekiq/app/Gemfile.lock b/ruby/rails7-sidekiq/app/Gemfile.lock index 12b97aa2..8e9763ef 100644 --- a/ruby/rails7-sidekiq/app/Gemfile.lock +++ b/ruby/rails7-sidekiq/app/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: /integration specs: - appsignal (3.9.1) + appsignal (3.10.0) rack GEM diff --git a/ruby/rails7-sidekiq/app/app/channels/chat_room_channel.rb b/ruby/rails7-sidekiq/app/app/channels/chat_room_channel.rb index e716bbd0..93f27244 100644 --- a/ruby/rails7-sidekiq/app/app/channels/chat_room_channel.rb +++ b/ruby/rails7-sidekiq/app/app/channels/chat_room_channel.rb @@ -17,4 +17,12 @@ def echo(data) data.delete("action") transmit data end + + def add_connected_message + message = ChatMessage.new( + :body => "You're connected to the chat room!", + :created_at => Time.zone.now, + ) + transmit message + end end diff --git a/ruby/rails7-sidekiq/app/app/javascript/channels/chat_room_channel.js b/ruby/rails7-sidekiq/app/app/javascript/channels/chat_room_channel.js index 98336999..79851777 100644 --- a/ruby/rails7-sidekiq/app/app/javascript/channels/chat_room_channel.js +++ b/ruby/rails7-sidekiq/app/app/javascript/channels/chat_room_channel.js @@ -8,6 +8,9 @@ if (chatRoomId) { consumer.subscriptions.create({ channel: "ChatRoomChannel", chat_room_id: chatRoomId }, { connected() { console.log(`Subscribed to ${chatRoomId}`) + // You would do this from the channel itself, but this triggers an action + // in the Channel so that we can test if instrument the action. + this.perform("add_connected_message") }, disconnected() {