Skip to content

Commit

Permalink
Add specs for collaborators and general room channels
Browse files Browse the repository at this point in the history
  • Loading branch information
sammo1235 committed Dec 10, 2024
1 parent 63b68b1 commit 21b863b
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 1 deletion.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ group :development, :test do
end

group :test do
gem "action-cable-testing"
gem "factory_bot_rails"
gem "capybara", "~> 3.39.0"
gem "poltergeist"
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ GEM
remote: https://rubygems.org/
specs:
Ascii85 (1.1.0)
action-cable-testing (0.6.1)
actioncable (>= 5.0)
actioncable (7.0.8.4)
actionpack (= 7.0.8.4)
activesupport (= 7.0.8.4)
Expand Down Expand Up @@ -727,6 +729,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
action-cable-testing
active_hash
amoeba (= 3.0.0)
binding_of_caller
Expand Down
138 changes: 137 additions & 1 deletion spec/channels/collaborators_channel_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,141 @@
require "rails_helper"

RSpec.describe CollaboratorsChannel, type: :channel do
pending "add some examples to (or delete) #{__FILE__}"
let(:channel_name) { "presence-chat-development-1-sep-step-consent-due-diligence" }
let(:user) { create(:user) }
let(:user_two) { create(:user) }
let(:tab_one) { "EiIKeLF" }
let(:tab_two) { "isdSJa2" }
let(:current_editor) { "#{user.id}:#{tab_one}:#{user.email}:#{user.full_name}:EDITOR" }

before do
stub_connection
allow(Collaborators::BroadcastCollabWorker).to receive(:perform_async)
allow(Rails).to receive(:cache).and_return(ActiveSupport::Cache::MemoryStore.new)
end

describe "#subscribed" do
context "when there are no subscribed users" do
before do
Rails.cache.write(channel_name, "")
end

it "makes the first joining user an editor" do
subscribe({ "channel_name" => channel_name, "user_id" => user.id.to_s, "current_tab" => tab_one })

expect(Rails.cache.read(channel_name)).to eq("#{user.id}:#{tab_one}:#{user.email}:#{user.full_name}:EDITOR")
end

it "broadcasts the collaborators for the room" do
expect(Collaborators::BroadcastCollabWorker).to receive(:perform_async)
.with(channel_name, "#{user.id}:#{tab_one}:#{user.email}:#{user.full_name}:EDITOR")

subscribe({ "channel_name" => channel_name, "user_id" => user.id.to_s, "current_tab" => tab_one })
end
end

context "when there is already an editor for the channel" do
let(:second_user_tab) { "2392j123" }
before do
Rails.cache.write(channel_name, current_editor)
end

it "adds the second user as a non-editor" do
subscribe({ "channel_name" => channel_name, "user_id" => user_two.id.to_s, "current_tab" => second_user_tab })

expect(Rails.cache.read(channel_name)).to eq("#{current_editor}/#{user_two.id}:#{second_user_tab}:#{user_two.email}:#{user_two.full_name}")
end

it "broadcasts the collaborators for the room" do
expect(Collaborators::BroadcastCollabWorker).to receive(:perform_async)
.with(channel_name, "#{current_editor}/#{user_two.id}:#{second_user_tab}:#{user_two.email}:#{user_two.full_name}")

subscribe({ "channel_name" => channel_name, "user_id" => user_two.id, "current_tab" => second_user_tab })
end
end

context "when the same user opens two tabs" do
it "adds the same user to the cache but as a non-editor" do
subscribe({ "channel_name" => channel_name, "user_id" => user.id.to_s, "current_tab" => tab_one })
subscribe({ "channel_name" => channel_name, "user_id" => user.id.to_s, "current_tab" => tab_two })

expect(Rails.cache.read(channel_name)).to eq("#{current_editor}/#{user.id}:#{tab_two}:#{user.email}:#{user.full_name}")
end
end
end

describe "#unsubscribed" do
context "when the editor leaves the channel" do
before do
Rails.cache.write(channel_name, "")
subscribe({ "channel_name" => channel_name, "user_id" => user.id.to_s, "current_tab" => tab_one })
end

it "removes the user from the redis cache" do
expect(subscription).to be_confirmed
subscription.unsubscribe_from_channel

expect(Rails.cache.read(channel_name)).to eq ""
end

it "broadcasts the new collaborator list" do
expect(Collaborators::BroadcastCollabWorker).to receive(:perform_async)
.with(channel_name, "")

subscription.unsubscribe_from_channel
end
end

context "when the editor leaves the channel and another user is present" do
before do
Rails.cache.write(channel_name, "")
end

it "makes the second user the new editor" do
sub_one = subscribe({ "channel_name" => channel_name, "user_id" => user.id.to_s, "current_tab" => tab_one })
subscribe({ "channel_name" => channel_name, "user_id" => user_two.id.to_s, "current_tab" => tab_two })

expect(Rails.cache.read(channel_name)).to eq("#{current_editor}/#{user_two.id}:#{tab_two}:#{user_two.email}:#{user_two.full_name}")

sub_one.unsubscribe_from_channel

expect(Rails.cache.read(channel_name)).to eq("#{user_two.id}:#{tab_two}:#{user_two.email}:#{user_two.full_name}:EDITOR")
end
end

context "when a non-editor leaves the channel" do
it "keeps the editor the same as before" do
subscribe({ "channel_name" => channel_name, "user_id" => user.id.to_s, "current_tab" => tab_one })
sub_two = subscribe({ "channel_name" => channel_name, "user_id" => user_two.id.to_s, "current_tab" => tab_two })

sub_two.unsubscribe_from_channel

expect(Rails.cache.read(channel_name)).to eq(current_editor)
end
end

context "when one user has two tabs open" do
context "and they close the editor tab" do
it "makes the non-editing tab the editor" do
sub_one = subscribe({ "channel_name" => channel_name, "user_id" => user.id.to_s, "current_tab" => tab_one })
subscribe({ "channel_name" => channel_name, "user_id" => user.id.to_s, "current_tab" => tab_two })

sub_one.unsubscribe_from_channel

expect(Rails.cache.read(channel_name)).to eq("#{user.id}:#{tab_two}:#{user.email}:#{user.full_name}:EDITOR")
end
end

context "and they close the non-editor tab" do
it "keeps the editing tab as the editor" do
subscribe({ "channel_name" => channel_name, "user_id" => user.id.to_s, "current_tab" => tab_one })
sub_two = subscribe({ "channel_name" => channel_name, "user_id" => user.id.to_s, "current_tab" => tab_two })

sub_two.unsubscribe_from_channel

expect(Rails.cache.read(channel_name)).to eq("#{user.id}:#{tab_one}:#{user.email}:#{user.full_name}:EDITOR")
end
end
end
end
end
81 changes: 81 additions & 0 deletions spec/channels/general_room_channel_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require "rails_helper"

RSpec.describe GeneralRoomChannel, type: :channel do
let(:channel_name) { "presence-chat-development-1-general" }
let(:user) { create(:user) }
let(:user_two) { create(:user) }

before do
stub_connection
allow(Collaborators::BroadcastCollabWorker).to receive(:perform_async)
allow(Rails).to receive(:cache).and_return(ActiveSupport::Cache::MemoryStore.new)
end

describe "#subscribed" do
context "when the room is empty" do
it "adds the subscribing user ID to the redis cache" do
subscribe({ "channel_name" => channel_name, "user_id" => user.id.to_s })

expect(Rails.cache.read(channel_name)).to eq("#{user.id}")

Check failure on line 19 in spec/channels/general_room_channel_spec.rb

View workflow job for this annotation

GitHub Actions / lint

Style/RedundantInterpolation: Prefer `to_s` over string interpolation.
end

it "broadcasts the new room member" do
expect(Collaborators::BroadcastCollabWorker).to receive(:perform_async).with(channel_name, user.id.to_s)

subscribe({ "channel_name" => channel_name, "user_id" => user.id.to_s })
end
end

context "when the room already has a member" do
before do
subscribe({ "channel_name" => channel_name, "user_id" => user.id.to_s })
end

Check failure on line 32 in spec/channels/general_room_channel_spec.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/TrailingWhitespace: Trailing whitespace detected.

Check failure on line 33 in spec/channels/general_room_channel_spec.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
it "adds the subscribing user ID to the redis cache" do
subscribe({ "channel_name" => channel_name, "user_id" => user_two.id.to_s })

expect(Rails.cache.read(channel_name)).to eq("#{user.id}/#{user_two.id}")
end

it "broadcasts the new room member" do
expect(Collaborators::BroadcastCollabWorker).to receive(:perform_async).with(channel_name, "#{user.id.to_s}/#{user_two.id.to_s}")

Check failure on line 41 in spec/channels/general_room_channel_spec.rb

View workflow job for this annotation

GitHub Actions / lint

Lint/RedundantStringCoercion: Redundant use of `Object#to_s` in interpolation.

Check failure on line 41 in spec/channels/general_room_channel_spec.rb

View workflow job for this annotation

GitHub Actions / lint

Lint/RedundantStringCoercion: Redundant use of `Object#to_s` in interpolation.

subscribe({ "channel_name" => channel_name, "user_id" => user_two.id.to_s })
end
end
end

describe "#unsubscribed" do
context "when there is one room member" do
it "clears the room" do
sub_one = subscribe({ "channel_name" => channel_name, "user_id" => user.id.to_s })

Check failure on line 52 in spec/channels/general_room_channel_spec.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
sub_one.unsubscribe_from_channel

expect(Rails.cache.read(channel_name)).to eq ""
end

it "broadcasts the new (empty) room members" do
sub_one = subscribe({ "channel_name" => channel_name, "user_id" => user.id.to_s })

expect(Collaborators::BroadcastCollabWorker).to receive(:perform_async).with(channel_name, "")
sub_one.unsubscribe_from_channel
end
end

context "when there are multiple room members" do
it "removes only the unsubscribing room member" do
sub_one = subscribe({ "channel_name" => channel_name, "user_id" => user.id.to_s })

Check failure on line 68 in spec/channels/general_room_channel_spec.rb

View workflow job for this annotation

GitHub Actions / lint

Lint/UselessAssignment: Useless assignment to variable - `sub_one`. Did you mean `sub_two`?
sub_two = subscribe({ "channel_name" => channel_name, "user_id" => user_two.id.to_s })

expect(Rails.cache.read(channel_name)).to eq("#{user.id}/#{user_two.id}")

expect(Collaborators::BroadcastCollabWorker).to receive(:perform_async).with(channel_name, user.id.to_s)

Check failure on line 74 in spec/channels/general_room_channel_spec.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
sub_two.unsubscribe_from_channel

Check failure on line 76 in spec/channels/general_room_channel_spec.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
expect(Rails.cache.read(channel_name)).to eq("#{user.id}")

Check failure on line 77 in spec/channels/general_room_channel_spec.rb

View workflow job for this annotation

GitHub Actions / lint

Style/RedundantInterpolation: Prefer `to_s` over string interpolation.
end
end
end
end

0 comments on commit 21b863b

Please sign in to comment.