docbrown/spec/services/user_blocks/channel_handler_spec.rb
Andy Zhao 73caa9a864 New Feature: Block Users (#4411)
* Prevent need for eager loading

* Add initial implementation of user block

* Remove debugger oops

* Add index and foreign keys for user_block table

* Use private method instead of exists

* Move channel handling logic to service object

* Update styling a bit

* Use profileDropdown file for future proofing

* Remove commented out code

* Render json: { result } for all endpoints

* Add statuses for sad paths

* Add better sad path handling in the JS

* Remove accidentally committed spec file

* Don't wait for DOM to load block button

* Use equality for accuracy in slug query

* Add status code for unauthorized requests

* Add missing comma sigh
2019-10-23 17:14:28 -04:00

44 lines
1.7 KiB
Ruby

require "rails_helper"
RSpec.describe UserBlocks::ChannelHandler, type: :service do
before do
create_list(:user, 2)
blocker = User.first
blocked = User.second
chat_channel = create(:chat_channel, channel_type: "direct", status: "active", slug: "#{blocker.username}/#{blocked.username}")
create(:chat_channel_membership, user: blocker, chat_channel: chat_channel)
create(:chat_channel_membership, user: blocked, chat_channel: chat_channel)
create(:user_block, blocker: blocker, blocked: blocked)
end
describe ".get_potential_chat_channel" do
it "returns the correct channel" do
expect(described_class.new(UserBlock.first).get_potential_chat_channel).to eq(ChatChannel.first)
end
end
describe ".block_chat_channel" do
it "updates the chat channel status to blocked" do
described_class.new(UserBlock.first).block_chat_channel
expect(ChatChannel.first.status).to eq "blocked"
end
it "removes the related chat channel memberships" do
expect { described_class.new(UserBlock.first).block_chat_channel }.
to change(ChatChannelMembership.where(status: "active"), :count).to 0
end
end
describe ".unblock_chat_channel" do
it "updates the chat channel status to be active" do
described_class.new(UserBlock.first).unblock_chat_channel
expect(ChatChannel.first.status).to eq "active"
end
it "updates the related chat channel memberships" do
ChatChannelMembership.update_all(status: "left-channel")
expect { described_class.new(UserBlock.first).unblock_chat_channel }.
to change(ChatChannelMembership.where(status: "left-channel"), :count).to 0
end
end
end