diff --git a/Gemfile.lock b/Gemfile.lock index 958bfb6056d..ed44c90dd22 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -191,6 +191,7 @@ GEM simpleidn (~> 0.2) jwt (2.7.1) kgio (2.11.4) + language_server-protocol (3.17.0.3) libv8-node (18.16.0.0) libv8-node (18.16.0.0-aarch64-linux) libv8-node (18.16.0.0-arm64-darwin) @@ -409,8 +410,9 @@ GEM rspec-core (>= 2.14) rtlcss (0.2.1) mini_racer (>= 0.6.3) - rubocop (1.52.1) + rubocop (1.53.0) json (~> 2.3) + language_server-protocol (>= 3.17.0) parallel (~> 1.10) parser (>= 3.2.2.3) rainbow (>= 2.2.2, < 4.0) @@ -423,7 +425,7 @@ GEM parser (>= 3.2.1.0) rubocop-capybara (2.18.0) rubocop (~> 1.41) - rubocop-discourse (3.2.0) + rubocop-discourse (3.3.0) rubocop (>= 1.1.0) rubocop-rspec (>= 2.0.0) rubocop-factory_bot (2.23.1) diff --git a/plugins/chat/spec/jobs/regular/chat/auto_join_channel_batch_spec.rb b/plugins/chat/spec/jobs/regular/chat/auto_join_channel_batch_spec.rb index 9ccb28332c8..b1ed2bdea9f 100644 --- a/plugins/chat/spec/jobs/regular/chat/auto_join_channel_batch_spec.rb +++ b/plugins/chat/spec/jobs/regular/chat/auto_join_channel_batch_spec.rb @@ -3,13 +3,15 @@ require "rails_helper" describe Jobs::Chat::AutoJoinChannelBatch do + subject(:job) { described_class.new } + describe "#execute" do fab!(:category) { Fabricate(:category) } let!(:user) { Fabricate(:user, last_seen_at: 15.minutes.ago) } let(:channel) { Fabricate(:chat_channel, auto_join_users: true, chatable: category) } it "joins all valid users in the batch" do - subject.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user.id) + job.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user.id) assert_users_follows_channel(channel, [user]) end @@ -17,7 +19,7 @@ describe Jobs::Chat::AutoJoinChannelBatch do it "doesn't join users outside the batch" do another_user = Fabricate(:user, last_seen_at: 15.minutes.ago) - subject.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user.id) + job.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user.id) assert_users_follows_channel(channel, [user]) assert_user_skipped(channel, another_user) @@ -26,7 +28,7 @@ describe Jobs::Chat::AutoJoinChannelBatch do it "doesn't join suspended users" do user.update!(suspended_till: 1.year.from_now) - subject.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user.id) + job.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user.id) assert_user_skipped(channel, user) end @@ -34,7 +36,7 @@ describe Jobs::Chat::AutoJoinChannelBatch do it "doesn't join users last_seen more than 3 months ago" do user.update!(last_seen_at: 4.months.ago) - subject.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user.id) + job.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user.id) assert_user_skipped(channel, user) end @@ -42,13 +44,13 @@ describe Jobs::Chat::AutoJoinChannelBatch do it "joins users with last_seen set to null" do user.update!(last_seen_at: nil) - subject.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user.id) + job.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user.id) assert_users_follows_channel(channel, [user]) end it "does nothing if the channel is invalid" do - subject.execute(chat_channel_id: -1, starts_at: user.id, ends_at: user.id) + job.execute(chat_channel_id: -1, starts_at: user.id, ends_at: user.id) assert_user_skipped(channel, user) end @@ -57,13 +59,13 @@ describe Jobs::Chat::AutoJoinChannelBatch do direct_message = Fabricate(:direct_message) channel.update!(chatable: direct_message) - subject.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user.id) + job.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user.id) assert_user_skipped(channel, user) end it "enqueues the user count update job and marks the channel user count as stale" do - subject.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user.id) + job.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user.id) expect_job_enqueued( job: Jobs::Chat::UpdateChannelUserCount, args: { @@ -81,7 +83,7 @@ describe Jobs::Chat::AutoJoinChannelBatch do args: { chat_channel_id: channel.id, }, - ) { subject.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user_2.id) } + ) { job.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user_2.id) } expect(channel.reload.user_count_stale).to eq(false) end @@ -89,13 +91,13 @@ describe Jobs::Chat::AutoJoinChannelBatch do it "ignores users without chat_enabled" do user.user_option.update!(chat_enabled: false) - subject.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user.id) + job.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user.id) assert_user_skipped(channel, user) end it "sets the join reason to automatic" do - subject.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user.id) + job.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user.id) new_membership = Chat::UserChatChannelMembership.find_by(user: user, chat_channel: channel) expect(new_membership.automatic?).to eq(true) @@ -104,7 +106,7 @@ describe Jobs::Chat::AutoJoinChannelBatch do it "skips anonymous users" do user_2 = Fabricate(:anonymous) - subject.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user_2.id) + job.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user_2.id) assert_users_follows_channel(channel, [user]) assert_user_skipped(channel, user_2) @@ -113,7 +115,7 @@ describe Jobs::Chat::AutoJoinChannelBatch do it "skips non-active users" do user_2 = Fabricate(:user, active: false, last_seen_at: 15.minutes.ago) - subject.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user_2.id) + job.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user_2.id) assert_users_follows_channel(channel, [user]) assert_user_skipped(channel, user_2) @@ -122,7 +124,7 @@ describe Jobs::Chat::AutoJoinChannelBatch do it "skips staged users" do user_2 = Fabricate(:user, staged: true, last_seen_at: 15.minutes.ago) - subject.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user_2.id) + job.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user_2.id) assert_users_follows_channel(channel, [user]) assert_user_skipped(channel, user_2) @@ -131,7 +133,7 @@ describe Jobs::Chat::AutoJoinChannelBatch do it "adds every user in the batch" do user_2 = Fabricate(:user, last_seen_at: 15.minutes.ago) - subject.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user_2.id) + job.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user_2.id) assert_users_follows_channel(channel, [user, user_2]) end @@ -139,7 +141,7 @@ describe Jobs::Chat::AutoJoinChannelBatch do it "publishes a message only to joined users" do messages = MessageBus.track_publish("/chat/new-channel") do - subject.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user.id) + job.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user.id) end expect(messages.size).to eq(1) @@ -156,7 +158,7 @@ describe Jobs::Chat::AutoJoinChannelBatch do it "only joins group members with access to the category" do another_user = Fabricate(:user, last_seen_at: 15.minutes.ago) - subject.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: another_user.id) + job.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: another_user.id) assert_users_follows_channel(channel, [user]) assert_user_skipped(channel, another_user) @@ -167,7 +169,7 @@ describe Jobs::Chat::AutoJoinChannelBatch do Fabricate(:category_group, category: category, group: second_chatters_group) second_chatters_group.add(user) - subject.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user.id) + job.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: user.id) assert_users_follows_channel(channel, [user]) end @@ -176,7 +178,7 @@ describe Jobs::Chat::AutoJoinChannelBatch do another_user = Fabricate(:user, last_seen_at: 15.minutes.ago) chatters_group.add(another_user) - subject.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: another_user.id) + job.execute(chat_channel_id: channel.id, starts_at: user.id, ends_at: another_user.id) assert_users_follows_channel(channel, [user, another_user]) end @@ -195,7 +197,7 @@ describe Jobs::Chat::AutoJoinChannelBatch do ) non_chatters_group.add(another_user) - subject.execute( + job.execute( chat_channel_id: readonly_channel.id, starts_at: another_user.id, ends_at: another_user.id, @@ -227,7 +229,7 @@ describe Jobs::Chat::AutoJoinChannelBatch do ) other_group.add(another_user) - subject.execute( + job.execute( chat_channel_id: private_channel.id, starts_at: another_user.id, ends_at: another_user.id, diff --git a/plugins/chat/spec/jobs/regular/chat/delete_user_messages_spec.rb b/plugins/chat/spec/jobs/regular/chat/delete_user_messages_spec.rb index 224d382a3c5..1cb3f1c8da3 100644 --- a/plugins/chat/spec/jobs/regular/chat/delete_user_messages_spec.rb +++ b/plugins/chat/spec/jobs/regular/chat/delete_user_messages_spec.rb @@ -2,12 +2,14 @@ RSpec.describe Jobs::Chat::DeleteUserMessages do describe "#execute" do + subject(:execute) { described_class.new.execute(user_id: user_1) } + fab!(:user_1) { Fabricate(:user) } fab!(:channel) { Fabricate(:chat_channel) } fab!(:chat_message) { Fabricate(:chat_message, chat_channel: channel, user: user_1) } it "deletes messages from the user" do - subject.execute(user_id: user_1) + execute expect { chat_message.reload }.to raise_error(ActiveRecord::RecordNotFound) end @@ -16,7 +18,7 @@ RSpec.describe Jobs::Chat::DeleteUserMessages do user_2 = Fabricate(:user) user_2_message = Fabricate(:chat_message, chat_channel: channel, user: user_2) - subject.execute(user_id: user_1) + execute expect(user_2_message.reload).to be_present end @@ -24,7 +26,7 @@ RSpec.describe Jobs::Chat::DeleteUserMessages do it "deletes trashed messages" do chat_message.trash! - subject.execute(user_id: user_1) + execute expect(Chat::Message.with_deleted.where(id: chat_message.id)).to be_empty end diff --git a/plugins/chat/spec/jobs/regular/chat/notify_mentioned_spec.rb b/plugins/chat/spec/jobs/regular/chat/notify_mentioned_spec.rb index 7ac9d674613..db432fbdcda 100644 --- a/plugins/chat/spec/jobs/regular/chat/notify_mentioned_spec.rb +++ b/plugins/chat/spec/jobs/regular/chat/notify_mentioned_spec.rb @@ -3,6 +3,8 @@ require "rails_helper" describe Jobs::Chat::NotifyMentioned do + subject(:job) { described_class.new } + fab!(:user_1) { Fabricate(:user) } fab!(:user_2) { Fabricate(:user) } fab!(:public_channel) { Fabricate(:category_channel) } @@ -47,7 +49,7 @@ describe Jobs::Chat::NotifyMentioned do ) MessageBus .track_publish("/chat/notification-alert/#{user.id}") do - subject.execute( + job.execute( chat_message_id: message.id, timestamp: message.created_at, to_notify_ids_map: to_notify_ids_map, @@ -58,7 +60,7 @@ describe Jobs::Chat::NotifyMentioned do end def track_core_notification(user: user_2, message:, to_notify_ids_map:) - subject.execute( + job.execute( chat_message_id: message.id, timestamp: message.created_at, to_notify_ids_map: to_notify_ids_map, @@ -175,7 +177,7 @@ describe Jobs::Chat::NotifyMentioned do PostAlerter.expects(:push_notification).never - subject.execute( + job.execute( chat_message_id: message.id, timestamp: message.created_at, to_notify_ids_map: to_notify_ids_map, @@ -204,7 +206,7 @@ describe Jobs::Chat::NotifyMentioned do PostAlerter.expects(:push_notification).never - subject.execute( + job.execute( chat_message_id: message.id, timestamp: message.created_at, to_notify_ids_map: to_notify_ids_map, @@ -248,7 +250,7 @@ describe Jobs::Chat::NotifyMentioned do }, ) - subject.execute( + job.execute( chat_message_id: message.id, timestamp: message.created_at, to_notify_ids_map: to_notify_ids_map, diff --git a/plugins/chat/spec/jobs/regular/chat/send_message_notifications_spec.rb b/plugins/chat/spec/jobs/regular/chat/send_message_notifications_spec.rb index 2739339fc35..93d395da9ac 100644 --- a/plugins/chat/spec/jobs/regular/chat/send_message_notifications_spec.rb +++ b/plugins/chat/spec/jobs/regular/chat/send_message_notifications_spec.rb @@ -1,13 +1,15 @@ # frozen_string_literal: true RSpec.describe Jobs::Chat::SendMessageNotifications do + subject(:job) { described_class.new } + describe "#execute" do context "when the message doesn't exist" do it "does nothing" do Chat::Notifier.any_instance.expects(:notify_new).never Chat::Notifier.any_instance.expects(:notify_edit).never - subject.execute(eason: "new", timestamp: 1.minute.ago) + job.execute(reason: "new", timestamp: 1.minute.ago) end end @@ -18,32 +20,28 @@ RSpec.describe Jobs::Chat::SendMessageNotifications do Chat::Notifier.expects(:notify_new).never Chat::Notifier.expects(:notify_edit).never - subject.execute( - chat_message_id: chat_message.id, - reason: "invalid", - timestamp: 1.minute.ago, - ) + job.execute(chat_message_id: chat_message.id, reason: "invalid", timestamp: 1.minute.ago) end it "does nothing if there is no timestamp" do Chat::Notifier.any_instance.expects(:notify_new).never Chat::Notifier.any_instance.expects(:notify_edit).never - subject.execute(chat_message_id: chat_message.id, reason: "new") + job.execute(chat_message_id: chat_message.id, reason: "new") end it "calls notify_new when the reason is 'new'" do Chat::Notifier.any_instance.expects(:notify_new).once Chat::Notifier.any_instance.expects(:notify_edit).never - subject.execute(chat_message_id: chat_message.id, reason: "new", timestamp: 1.minute.ago) + job.execute(chat_message_id: chat_message.id, reason: "new", timestamp: 1.minute.ago) end it "calls notify_edit when the reason is 'edit'" do Chat::Notifier.any_instance.expects(:notify_new).never Chat::Notifier.any_instance.expects(:notify_edit).once - subject.execute(chat_message_id: chat_message.id, reason: "edit", timestamp: 1.minute.ago) + job.execute(chat_message_id: chat_message.id, reason: "edit", timestamp: 1.minute.ago) end end end diff --git a/plugins/chat/spec/jobs/scheduled/auto_join_users_spec.rb b/plugins/chat/spec/jobs/scheduled/auto_join_users_spec.rb index 1807f0e74d5..4abbdc37405 100644 --- a/plugins/chat/spec/jobs/scheduled/auto_join_users_spec.rb +++ b/plugins/chat/spec/jobs/scheduled/auto_join_users_spec.rb @@ -3,6 +3,8 @@ require "rails_helper" describe Jobs::Chat::AutoJoinUsers do + subject(:job) { described_class.new } + it "works" do Jobs.run_immediately! channel = Fabricate(:category_channel, auto_join_users: true) @@ -11,7 +13,7 @@ describe Jobs::Chat::AutoJoinUsers do membership = Chat::UserChatChannelMembership.find_by(user: user, chat_channel: channel) expect(membership).to be_nil - subject.execute({}) + job.execute({}) membership = Chat::UserChatChannelMembership.find_by(user: user, chat_channel: channel) expect(membership.following).to eq(true) diff --git a/plugins/chat/spec/lib/chat/channel_archive_service_spec.rb b/plugins/chat/spec/lib/chat/channel_archive_service_spec.rb index 8db6a804fc3..1911e2fa3c6 100644 --- a/plugins/chat/spec/lib/chat/channel_archive_service_spec.rb +++ b/plugins/chat/spec/lib/chat/channel_archive_service_spec.rb @@ -9,8 +9,8 @@ describe Chat::ChannelArchiveService do fab!(:channel) { Fabricate(:category_channel) } fab!(:user) { Fabricate(:user, admin: true) } fab!(:category) { Fabricate(:category) } + let(:topic_params) { { topic_title: "This will be a new topic", category_id: category.id } } - subject { Chat::ChannelArchiveService } before { SiteSetting.chat_enabled = true } @@ -18,7 +18,7 @@ describe Chat::ChannelArchiveService do before { 3.times { Fabricate(:chat_message, chat_channel: channel) } } it "marks the channel as read_only" do - subject.create_archive_process( + described_class.create_archive_process( chat_channel: channel, acting_user: user, topic_params: topic_params, @@ -27,7 +27,7 @@ describe Chat::ChannelArchiveService do end it "creates the chat channel archive record to save progress and topic params" do - subject.create_archive_process( + described_class.create_archive_process( chat_channel: channel, acting_user: user, topic_params: topic_params, @@ -42,7 +42,7 @@ describe Chat::ChannelArchiveService do it "enqueues the archive job" do channel_archive = - subject.create_archive_process( + described_class.create_archive_process( chat_channel: channel, acting_user: user, topic_params: topic_params, @@ -58,13 +58,13 @@ describe Chat::ChannelArchiveService do end it "does nothing if there is already an archive record for the channel" do - subject.create_archive_process( + described_class.create_archive_process( chat_channel: channel, acting_user: user, topic_params: topic_params, ) expect { - subject.create_archive_process( + described_class.create_archive_process( chat_channel: channel, acting_user: user, topic_params: topic_params, @@ -76,7 +76,7 @@ describe Chat::ChannelArchiveService do new_message = Fabricate(:chat_message, chat_channel: channel) new_message.trash! channel_archive = - subject.create_archive_process( + described_class.create_archive_process( chat_channel: channel, acting_user: user, topic_params: topic_params, @@ -92,7 +92,7 @@ describe Chat::ChannelArchiveService do def start_archive @channel_archive = - subject.create_archive_process( + described_class.create_archive_process( chat_channel: channel, acting_user: user, topic_params: topic_params, @@ -113,7 +113,7 @@ describe Chat::ChannelArchiveService do emoji: "+1", ) stub_const(Chat::ChannelArchiveService, "ARCHIVED_MESSAGES_PER_POST", 5) do - subject.new(@channel_archive).execute + described_class.new(@channel_archive).execute end @channel_archive.reload @@ -146,21 +146,21 @@ describe Chat::ChannelArchiveService do it "does not stop the process if the post length is too high (validations disabled)" do create_messages(50) && start_archive SiteSetting.max_post_length = 1 - subject.new(@channel_archive).execute + described_class.new(@channel_archive).execute expect(@channel_archive.reload.complete?).to eq(true) end it "successfully links uploads from messages to the post" do create_messages(3) && start_archive UploadReference.create!(target: Chat::Message.last, upload: Fabricate(:upload)) - subject.new(@channel_archive).execute + described_class.new(@channel_archive).execute expect(@channel_archive.reload.complete?).to eq(true) expect(@channel_archive.destination_topic.posts.last.upload_references.count).to eq(1) end it "successfully sends a private message to the archiving user" do create_messages(3) && start_archive - subject.new(@channel_archive).execute + described_class.new(@channel_archive).execute expect(@channel_archive.reload.complete?).to eq(true) pm_topic = Topic.private_messages.last expect(pm_topic.topic_allowed_users.first.user).to eq(@channel_archive.archived_by) @@ -174,7 +174,7 @@ describe Chat::ChannelArchiveService do create_messages(3) && start_archive @channel_archive.update!(destination_topic_title: "Wow this is the new title :tada: :joy:") - subject.new(@channel_archive).execute + described_class.new(@channel_archive).execute expect(@channel_archive.reload.complete?).to eq(false) expect(@channel_archive.reload.failed?).to eq(true) expect(@channel_archive.archive_error).to eq("Title can't have more than 1 emoji") @@ -191,7 +191,7 @@ describe Chat::ChannelArchiveService do it "uses the channel slug to autolink a hashtag for the channel in the PM" do create_messages(3) && start_archive - subject.new(@channel_archive).execute + described_class.new(@channel_archive).execute expect(@channel_archive.reload.complete?).to eq(true) pm_topic = Topic.private_messages.last expect(pm_topic.first_post.cooked).to have_tag( @@ -231,7 +231,7 @@ describe Chat::ChannelArchiveService do Chat::UserChatChannelMembership.where(chat_channel: channel, following: true).count, ).to eq(3) start_archive - subject.new(@channel_archive).execute + described_class.new(@channel_archive).execute expect(@channel_archive.reload.complete?).to eq(true) expect( Chat::UserChatChannelMembership.where(chat_channel: channel, following: true).count, @@ -243,7 +243,7 @@ describe Chat::ChannelArchiveService do last_read_message_id: channel.chat_messages.first.id, ) start_archive - subject.new(@channel_archive).execute + described_class.new(@channel_archive).execute expect(@channel_archive.reload.complete?).to eq(true) expect(Chat::UserChatChannelMembership.last.last_read_message_id).to eq( channel.chat_messages.last.id, @@ -257,7 +257,7 @@ describe Chat::ChannelArchiveService do it "archives the topic" do create_messages(3) && start_archive - subject.new(@channel_archive).execute + described_class.new(@channel_archive).execute topic = @channel_archive.destination_topic topic.reload expect(topic.archived).to eq(true) @@ -269,7 +269,7 @@ describe Chat::ChannelArchiveService do it "leaves the topic open" do create_messages(3) && start_archive - subject.new(@channel_archive).execute + described_class.new(@channel_archive).execute topic = @channel_archive.destination_topic topic.reload expect(topic.archived).to eq(false) @@ -282,7 +282,7 @@ describe Chat::ChannelArchiveService do it "closes the topic" do create_messages(3) && start_archive - subject.new(@channel_archive).execute + described_class.new(@channel_archive).execute topic = @channel_archive.destination_topic topic.reload expect(topic.archived).to eq(false) @@ -297,7 +297,7 @@ describe Chat::ChannelArchiveService do destination_topic_title: nil, destination_topic_id: Fabricate(:topic).id, ) - subject.new(@channel_archive).execute + described_class.new(@channel_archive).execute topic = @channel_archive.destination_topic topic.reload expect(topic.archived).to eq(false) @@ -322,7 +322,7 @@ describe Chat::ChannelArchiveService do emoji: "+1", ) stub_const(Chat::ChannelArchiveService, "ARCHIVED_MESSAGES_PER_POST", 5) do - subject.new(@channel_archive).execute + described_class.new(@channel_archive).execute end @channel_archive.reload @@ -363,7 +363,7 @@ describe Chat::ChannelArchiveService do .raises(FakeArchiveError.new("this is a test error")) stub_const(Chat::ChannelArchiveService, "ARCHIVED_MESSAGES_PER_POST", 5) do - expect { subject.new(@channel_archive).execute }.to raise_error(FakeArchiveError) + expect { described_class.new(@channel_archive).execute }.to raise_error(FakeArchiveError) end expect(@channel_archive.reload.archive_error).to eq("this is a test error") @@ -376,7 +376,7 @@ describe Chat::ChannelArchiveService do Chat::ChannelArchiveService.any_instance.unstub(:create_post) stub_const(Chat::ChannelArchiveService, "ARCHIVED_MESSAGES_PER_POST", 5) do - subject.new(@channel_archive).execute + described_class.new(@channel_archive).execute end @channel_archive.reload diff --git a/plugins/chat/spec/lib/chat/message_bookmarkable_spec.rb b/plugins/chat/spec/lib/chat/message_bookmarkable_spec.rb index d6c97f1eb59..492ecce8755 100644 --- a/plugins/chat/spec/lib/chat/message_bookmarkable_spec.rb +++ b/plugins/chat/spec/lib/chat/message_bookmarkable_spec.rb @@ -3,6 +3,8 @@ require "rails_helper" describe Chat::MessageBookmarkable do + subject(:registered_bookmarkable) { RegisteredBookmarkable.new(described_class) } + fab!(:user) { Fabricate(:user) } fab!(:guardian) { Guardian.new(user) } fab!(:other_category) { Fabricate(:private_category, group: Fabricate(:group)) } @@ -25,23 +27,21 @@ describe Chat::MessageBookmarkable do let!(:bookmark2) { Fabricate(:bookmark, user: user, bookmarkable: message2) } let!(:bookmark3) { Fabricate(:bookmark) } - subject { RegisteredBookmarkable.new(described_class) } - describe "#perform_list_query" do it "returns all the user's bookmarks" do - expect(subject.perform_list_query(user, guardian).map(&:id)).to match_array( + expect(registered_bookmarkable.perform_list_query(user, guardian).map(&:id)).to match_array( [bookmark1.id, bookmark2.id], ) end it "does not return bookmarks for messages inside category chat channels the user cannot access" do channel.update(chatable: other_category) - expect(subject.perform_list_query(user, guardian)).to eq(nil) + expect(registered_bookmarkable.perform_list_query(user, guardian)).to eq(nil) other_category.groups.last.add(user) bookmark1.reload user.reload guardian = Guardian.new(user) - expect(subject.perform_list_query(user, guardian).map(&:id)).to match_array( + expect(registered_bookmarkable.perform_list_query(user, guardian).map(&:id)).to match_array( [bookmark1.id, bookmark2.id], ) end @@ -49,12 +49,12 @@ describe Chat::MessageBookmarkable do it "does not return bookmarks for messages inside direct message chat channels the user cannot access" do direct_message = Fabricate(:direct_message) channel.update(chatable: direct_message) - expect(subject.perform_list_query(user, guardian)).to eq(nil) + expect(registered_bookmarkable.perform_list_query(user, guardian)).to eq(nil) Chat::DirectMessageUser.create(user: user, direct_message: direct_message) bookmark1.reload user.reload guardian = Guardian.new(user) - expect(subject.perform_list_query(user, guardian).map(&:id)).to match_array( + expect(registered_bookmarkable.perform_list_query(user, guardian).map(&:id)).to match_array( [bookmark1.id, bookmark2.id], ) end @@ -62,7 +62,9 @@ describe Chat::MessageBookmarkable do it "does not return bookmarks for deleted messages" do message1.trash! guardian = Guardian.new(user) - expect(subject.perform_list_query(user, guardian).map(&:id)).to match_array([bookmark2.id]) + expect(registered_bookmarkable.perform_list_query(user, guardian).map(&:id)).to match_array( + [bookmark2.id], + ) end end @@ -72,8 +74,8 @@ describe Chat::MessageBookmarkable do it "returns bookmarks that match by name" do ts_query = Search.ts_query(term: "gotta", ts_config: "simple") expect( - subject.perform_search_query( - subject.perform_list_query(user, guardian), + registered_bookmarkable.perform_search_query( + registered_bookmarkable.perform_list_query(user, guardian), "%gotta%", ts_query, ).map(&:id), @@ -85,8 +87,8 @@ describe Chat::MessageBookmarkable do ts_query = Search.ts_query(term: "good soup", ts_config: "simple") expect( - subject.perform_search_query( - subject.perform_list_query(user, guardian), + registered_bookmarkable.perform_search_query( + registered_bookmarkable.perform_list_query(user, guardian), "%good soup%", ts_query, ).map(&:id), @@ -94,8 +96,8 @@ describe Chat::MessageBookmarkable do ts_query = Search.ts_query(term: "blah", ts_config: "simple") expect( - subject.perform_search_query( - subject.perform_list_query(user, guardian), + registered_bookmarkable.perform_search_query( + registered_bookmarkable.perform_list_query(user, guardian), "%blah%", ts_query, ).map(&:id), @@ -105,23 +107,23 @@ describe Chat::MessageBookmarkable do describe "#can_send_reminder?" do it "cannot send the reminder if the message or channel is deleted" do - expect(subject.can_send_reminder?(bookmark1)).to eq(true) + expect(registered_bookmarkable.can_send_reminder?(bookmark1)).to eq(true) bookmark1.bookmarkable.trash! bookmark1.reload - expect(subject.can_send_reminder?(bookmark1)).to eq(false) + expect(registered_bookmarkable.can_send_reminder?(bookmark1)).to eq(false) Chat::Message.with_deleted.find_by(id: bookmark1.bookmarkable_id).recover! bookmark1.reload bookmark1.bookmarkable.chat_channel.trash! bookmark1.reload - expect(subject.can_send_reminder?(bookmark1)).to eq(false) + expect(registered_bookmarkable.can_send_reminder?(bookmark1)).to eq(false) end end describe "#reminder_handler" do it "creates a notification for the user with the correct details" do - expect { subject.send_reminder_notification(bookmark1) }.to change { Notification.count }.by( - 1, - ) + expect { registered_bookmarkable.send_reminder_notification(bookmark1) }.to change { + Notification.count + }.by(1) notification = user.notifications.last expect(notification.notification_type).to eq(Notification.types[:bookmark_reminder]) expect(notification.data).to eq( @@ -142,38 +144,44 @@ describe Chat::MessageBookmarkable do describe "#can_see?" do it "returns false if the chat message is in a channel the user cannot see" do - expect(subject.can_see?(guardian, bookmark1)).to eq(true) + expect(registered_bookmarkable.can_see?(guardian, bookmark1)).to eq(true) bookmark1.bookmarkable.chat_channel.update!(chatable: private_category) - expect(subject.can_see?(guardian, bookmark1)).to eq(false) + expect(registered_bookmarkable.can_see?(guardian, bookmark1)).to eq(false) private_category.groups.last.add(user) bookmark1.reload user.reload guardian = Guardian.new(user) - expect(subject.can_see?(guardian, bookmark1)).to eq(true) + expect(registered_bookmarkable.can_see?(guardian, bookmark1)).to eq(true) end end describe "#validate_before_create" do it "raises InvalidAccess if the user cannot see the chat channel" do - expect { subject.validate_before_create(guardian, bookmark1.bookmarkable) }.not_to raise_error + expect { + registered_bookmarkable.validate_before_create(guardian, bookmark1.bookmarkable) + }.not_to raise_error bookmark1.bookmarkable.chat_channel.update!(chatable: private_category) - expect { subject.validate_before_create(guardian, bookmark1.bookmarkable) }.to raise_error( - Discourse::InvalidAccess, - ) + expect { + registered_bookmarkable.validate_before_create(guardian, bookmark1.bookmarkable) + }.to raise_error(Discourse::InvalidAccess) private_category.groups.last.add(user) bookmark1.reload user.reload guardian = Guardian.new(user) - expect { subject.validate_before_create(guardian, bookmark1.bookmarkable) }.not_to raise_error + expect { + registered_bookmarkable.validate_before_create(guardian, bookmark1.bookmarkable) + }.not_to raise_error end it "raises InvalidAccess if the chat message is deleted" do - expect { subject.validate_before_create(guardian, bookmark1.bookmarkable) }.not_to raise_error + expect { + registered_bookmarkable.validate_before_create(guardian, bookmark1.bookmarkable) + }.not_to raise_error bookmark1.bookmarkable.trash! bookmark1.reload - expect { subject.validate_before_create(guardian, bookmark1.bookmarkable) }.to raise_error( - Discourse::InvalidAccess, - ) + expect { + registered_bookmarkable.validate_before_create(guardian, bookmark1.bookmarkable) + }.to raise_error(Discourse::InvalidAccess) end end @@ -182,7 +190,7 @@ describe Chat::MessageBookmarkable do bookmark_post = Fabricate(:bookmark, bookmarkable: Fabricate(:post)) bookmark1.bookmarkable.trash! bookmark1.bookmarkable.update!(deleted_at: 4.days.ago) - subject.cleanup_deleted + registered_bookmarkable.cleanup_deleted expect(Bookmark.exists?(id: bookmark1.id)).to eq(false) expect(Bookmark.exists?(id: bookmark2.id)).to eq(true) expect(Bookmark.exists?(id: bookmark_post.id)).to eq(true) diff --git a/plugins/chat/spec/lib/chat/message_reactor_spec.rb b/plugins/chat/spec/lib/chat/message_reactor_spec.rb index c6715c3ad16..2a6d3cb06ef 100644 --- a/plugins/chat/spec/lib/chat/message_reactor_spec.rb +++ b/plugins/chat/spec/lib/chat/message_reactor_spec.rb @@ -3,39 +3,40 @@ require "rails_helper" describe Chat::MessageReactor do + subject(:message_reactor) { described_class.new(reacting_user, channel) } + fab!(:reacting_user) { Fabricate(:user) } fab!(:channel) { Fabricate(:category_channel) } fab!(:reactor) { described_class.new(reacting_user, channel) } fab!(:message_1) { Fabricate(:chat_message, chat_channel: channel, user: reacting_user) } - let(:subject) { described_class.new(reacting_user, channel) } it "calls guardian ensure_can_join_chat_channel!" do Guardian.any_instance.expects(:ensure_can_join_chat_channel!).once - subject.react!(message_id: message_1.id, react_action: :add, emoji: ":+1:") + message_reactor.react!(message_id: message_1.id, react_action: :add, emoji: ":+1:") end it "raises an error if the user cannot see the channel" do channel.update!(chatable: Fabricate(:private_category, group: Group[:staff])) expect { - subject.react!(message_id: message_1.id, react_action: :add, emoji: ":+1:") + message_reactor.react!(message_id: message_1.id, react_action: :add, emoji: ":+1:") }.to raise_error(Discourse::InvalidAccess) end it "raises an error if the user cannot react" do SpamRule::AutoSilence.new(reacting_user).silence_user expect { - subject.react!(message_id: message_1.id, react_action: :add, emoji: ":+1:") + message_reactor.react!(message_id: message_1.id, react_action: :add, emoji: ":+1:") }.to raise_error(Discourse::InvalidAccess) end it "raises an error if the channel status is not open" do channel.update!(status: Chat::Channel.statuses[:archived]) expect { - subject.react!(message_id: message_1.id, react_action: :add, emoji: ":+1:") + message_reactor.react!(message_id: message_1.id, react_action: :add, emoji: ":+1:") }.to raise_error(Discourse::InvalidAccess) channel.update!(status: Chat::Channel.statuses[:open]) expect { - subject.react!(message_id: message_1.id, react_action: :add, emoji: ":+1:") + message_reactor.react!(message_id: message_1.id, react_action: :add, emoji: ":+1:") }.to change(Chat::MessageReaction, :count).by(1) end diff --git a/plugins/chat/spec/lib/chat/post_notification_handler_spec.rb b/plugins/chat/spec/lib/chat/post_notification_handler_spec.rb index 4d7488bc4b7..47705958594 100644 --- a/plugins/chat/spec/lib/chat/post_notification_handler_spec.rb +++ b/plugins/chat/spec/lib/chat/post_notification_handler_spec.rb @@ -3,10 +3,11 @@ require "rails_helper" describe Chat::PostNotificationHandler do + subject(:handler) { described_class.new(post, notified_users) } + let(:acting_user) { Fabricate(:user) } let(:post) { Fabricate(:post) } let(:notified_users) { [] } - let(:subject) { described_class.new(post, notified_users) } fab!(:channel) { Fabricate(:category_channel) } fab!(:message1) do @@ -24,7 +25,7 @@ describe Chat::PostNotificationHandler do def expect_no_notification return_val = nil - expect { return_val = subject.handle }.not_to change { Notification.count } + expect { return_val = handler.handle }.not_to change { Notification.count } expect(return_val).to eq(false) end @@ -51,7 +52,7 @@ describe Chat::PostNotificationHandler do it "sends notifications to all of the quoted users" do update_post_with_chat_quote([message1, message2]) - subject.handle + handler.handle expect( Notification.where( user: message1.user, @@ -68,8 +69,8 @@ describe Chat::PostNotificationHandler do it "does not send the same chat_quoted notification twice to the same post and user" do update_post_with_chat_quote([message1, message2]) - subject.handle - subject.handle + handler.handle + handler.handle expect( Notification.where( user: message1.user, @@ -87,7 +88,7 @@ describe Chat::PostNotificationHandler do topic: post.topic, user: message1.user, ) - subject.handle + handler.handle expect( Notification.where( user: message1.user, @@ -101,7 +102,7 @@ describe Chat::PostNotificationHandler do it "does not send notifications to those users" do update_post_with_chat_quote([message1, message2]) - subject.handle + handler.handle expect( Notification.where( user: message1.user, diff --git a/plugins/chat/spec/lib/chat/review_queue_spec.rb b/plugins/chat/spec/lib/chat/review_queue_spec.rb index fb147e7df2e..01b560ac6dd 100644 --- a/plugins/chat/spec/lib/chat/review_queue_spec.rb +++ b/plugins/chat/spec/lib/chat/review_queue_spec.rb @@ -3,17 +3,17 @@ require "rails_helper" describe Chat::ReviewQueue do + subject(:queue) { described_class.new } + fab!(:message_poster) { Fabricate(:user) } fab!(:flagger) { Fabricate(:user) } fab!(:chat_channel) { Fabricate(:category_channel) } fab!(:message) { Fabricate(:chat_message, user: message_poster, chat_channel: chat_channel) } - fab!(:admin) { Fabricate(:admin) } + let(:guardian) { Guardian.new(flagger) } let(:admin_guardian) { Guardian.new(admin) } - subject(:queue) { described_class.new } - before do chat_channel.add(message_poster) chat_channel.add(flagger) diff --git a/plugins/chat/spec/models/chat/deleted_chat_user_spec.rb b/plugins/chat/spec/models/chat/deleted_chat_user_spec.rb index 3d3284728e9..9a1beeaf757 100644 --- a/plugins/chat/spec/models/chat/deleted_chat_user_spec.rb +++ b/plugins/chat/spec/models/chat/deleted_chat_user_spec.rb @@ -3,15 +3,19 @@ require "rails_helper" describe Chat::DeletedUser do + subject(:deleted_user) { described_class.new } + describe "#username" do it "returns a default username" do - expect(subject.username).to eq(I18n.t("chat.deleted_chat_username")) + expect(deleted_user.username).to eq(I18n.t("chat.deleted_chat_username")) end end describe "#avatar_template" do it "returns a default path" do - expect(subject.avatar_template).to eq("/plugins/chat/images/deleted-chat-user-avatar.png") + expect(deleted_user.avatar_template).to eq( + "/plugins/chat/images/deleted-chat-user-avatar.png", + ) end end end diff --git a/plugins/chat/spec/queries/chat/channel_unreads_query_spec.rb b/plugins/chat/spec/queries/chat/channel_unreads_query_spec.rb index 10131b31c8e..112b255c3b7 100644 --- a/plugins/chat/spec/queries/chat/channel_unreads_query_spec.rb +++ b/plugins/chat/spec/queries/chat/channel_unreads_query_spec.rb @@ -3,6 +3,15 @@ require "rails_helper" describe Chat::ChannelUnreadsQuery do + subject(:query) do + described_class.call( + channel_ids: channel_ids, + user_id: current_user.id, + include_missing_memberships: include_missing_memberships, + include_read: include_read, + ).map(&:to_h) + end + fab!(:channel_1) { Fabricate(:category_channel) } fab!(:current_user) { Fabricate(:user) } let(:include_missing_memberships) { false } @@ -15,20 +24,11 @@ describe Chat::ChannelUnreadsQuery do channel_1.add(current_user) end - let(:subject) do - described_class.call( - channel_ids: channel_ids, - user_id: current_user.id, - include_missing_memberships: include_missing_memberships, - include_read: include_read, - ).map(&:to_h) - end - context "with unread message" do before { Fabricate(:chat_message, chat_channel: channel_1) } it "returns a correct unread count" do - expect(subject.first).to eq({ mention_count: 0, unread_count: 1, channel_id: channel_1.id }) + expect(query.first).to eq({ mention_count: 0, unread_count: 1, channel_id: channel_1.id }) end context "when the membership has been muted" do @@ -40,7 +40,7 @@ describe Chat::ChannelUnreadsQuery do end it "returns a zeroed unread count" do - expect(subject.first).to eq({ mention_count: 0, unread_count: 0, channel_id: channel_1.id }) + expect(query.first).to eq({ mention_count: 0, unread_count: 0, channel_id: channel_1.id }) end end @@ -49,13 +49,13 @@ describe Chat::ChannelUnreadsQuery do fab!(:thread) { Fabricate(:chat_thread, channel: channel_1, original_message: thread_om) } it "does include the original message in the unread count" do - expect(subject.first).to eq({ mention_count: 0, unread_count: 2, channel_id: channel_1.id }) + expect(query.first).to eq({ mention_count: 0, unread_count: 2, channel_id: channel_1.id }) end it "does not include other thread messages in the unread count" do Fabricate(:chat_message, chat_channel: channel_1, thread: thread) Fabricate(:chat_message, chat_channel: channel_1, thread: thread) - expect(subject.first).to eq({ mention_count: 0, unread_count: 2, channel_id: channel_1.id }) + expect(query.first).to eq({ mention_count: 0, unread_count: 2, channel_id: channel_1.id }) end end @@ -70,7 +70,7 @@ describe Chat::ChannelUnreadsQuery do end it "returns accurate counts" do - expect(subject).to match_array( + expect(query).to match_array( [ { mention_count: 0, unread_count: 1, channel_id: channel_1.id }, { mention_count: 0, unread_count: 2, channel_id: channel_2.id }, @@ -87,7 +87,7 @@ describe Chat::ChannelUnreadsQuery do end it "does not return counts for the channels" do - expect(subject).to match_array( + expect(query).to match_array( [{ mention_count: 0, unread_count: 1, channel_id: channel_1.id }], ) end @@ -96,7 +96,7 @@ describe Chat::ChannelUnreadsQuery do let(:include_missing_memberships) { true } it "does return zeroed counts for the channels" do - expect(subject).to match_array( + expect(query).to match_array( [ { mention_count: 0, unread_count: 1, channel_id: channel_1.id }, { mention_count: 0, unread_count: 0, channel_id: channel_2.id }, @@ -108,7 +108,7 @@ describe Chat::ChannelUnreadsQuery do let(:include_read) { false } it "does not return counts for the channels" do - expect(subject).to match_array( + expect(query).to match_array( [{ mention_count: 0, unread_count: 1, channel_id: channel_1.id }], ) end @@ -135,7 +135,7 @@ describe Chat::ChannelUnreadsQuery do message = Fabricate(:chat_message, chat_channel: channel_1) create_mention(message, channel_1) - expect(subject.first).to eq({ mention_count: 1, unread_count: 1, channel_id: channel_1.id }) + expect(query.first).to eq({ mention_count: 1, unread_count: 1, channel_id: channel_1.id }) end context "for unread mentions in a thread" do @@ -144,7 +144,7 @@ describe Chat::ChannelUnreadsQuery do it "does include the original message in the mention count" do create_mention(thread_om, channel_1) - expect(subject.first).to eq({ mention_count: 1, unread_count: 1, channel_id: channel_1.id }) + expect(query.first).to eq({ mention_count: 1, unread_count: 1, channel_id: channel_1.id }) end it "does not include other thread messages in the mention count" do @@ -152,7 +152,7 @@ describe Chat::ChannelUnreadsQuery do thread_message_2 = Fabricate(:chat_message, chat_channel: channel_1, thread: thread) create_mention(thread_message_1, channel_1) create_mention(thread_message_2, channel_1) - expect(subject.first).to eq({ mention_count: 0, unread_count: 1, channel_id: channel_1.id }) + expect(query.first).to eq({ mention_count: 0, unread_count: 1, channel_id: channel_1.id }) end end @@ -169,7 +169,7 @@ describe Chat::ChannelUnreadsQuery do message_2 = Fabricate(:chat_message, chat_channel: channel_2) create_mention(message_2, channel_2) - expect(subject).to match_array( + expect(query).to match_array( [ { mention_count: 1, unread_count: 1, channel_id: channel_1.id }, { mention_count: 1, unread_count: 2, channel_id: channel_2.id }, @@ -181,14 +181,14 @@ describe Chat::ChannelUnreadsQuery do context "with nothing unread" do it "returns a correct state" do - expect(subject.first).to eq({ mention_count: 0, unread_count: 0, channel_id: channel_1.id }) + expect(query.first).to eq({ mention_count: 0, unread_count: 0, channel_id: channel_1.id }) end context "when include_read is false" do let(:include_read) { false } it "returns nothing" do - expect(subject).to eq([]) + expect(query).to eq([]) end end end diff --git a/plugins/chat/spec/queries/chat/messages_query_spec.rb b/plugins/chat/spec/queries/chat/messages_query_spec.rb index be46468822e..be6f9a7406b 100644 --- a/plugins/chat/spec/queries/chat/messages_query_spec.rb +++ b/plugins/chat/spec/queries/chat/messages_query_spec.rb @@ -1,6 +1,10 @@ # frozen_string_literal: true RSpec.describe Chat::MessagesQuery do + subject(:query) do + described_class.call(guardian: current_user.guardian, channel: channel, **options) + end + fab!(:channel) { Fabricate(:category_channel) } fab!(:current_user) { Fabricate(:user) } @@ -21,10 +25,6 @@ RSpec.describe Chat::MessagesQuery do } end - let(:subject) do - described_class.call(guardian: current_user.guardian, channel: channel, **options) - end - fab!(:message_1) do message = Fabricate(:chat_message, chat_channel: channel) message.update!(created_at: 2.days.ago) @@ -42,7 +42,7 @@ RSpec.describe Chat::MessagesQuery do let(:target_message_id) { target_message.id } it "queries messages in the channel and finds the past and future messages" do - expect(subject).to eq( + expect(query).to eq( past_messages: [message_1], future_messages: [message_3], target_message: target_message, @@ -53,29 +53,29 @@ RSpec.describe Chat::MessagesQuery do it "does not include deleted messages" do message_3.trash! - expect(subject[:future_messages]).to eq([]) + expect(query[:future_messages]).to eq([]) end it "still includes the target message if it is deleted" do target_message.trash! - expect(subject[:target_message]).to eq(target_message) + expect(query[:target_message]).to eq(target_message) end it "can_load_more_past is true when the past messages reach the limit" do stub_const(described_class, "PAST_MESSAGE_LIMIT", 1) do - expect(subject[:can_load_more_past]).to be_truthy + expect(query[:can_load_more_past]).to be_truthy end end it "can_load_more_future is true when the future messages reach the limit" do stub_const(described_class, "FUTURE_MESSAGE_LIMIT", 1) do - expect(subject[:can_load_more_future]).to be_truthy + expect(query[:can_load_more_future]).to be_truthy end end it "limits results of paginated query when page_size is not set" do options[:target_message_id] = nil - stub_const(described_class, "MAX_PAGE_SIZE", 1) { expect(subject[:messages].length).to eq(1) } + stub_const(described_class, "MAX_PAGE_SIZE", 1) { expect(query[:messages].length).to eq(1) } end describe "when some messages are in threads" do @@ -83,7 +83,7 @@ RSpec.describe Chat::MessagesQuery do it "does not include messages which are thread replies but does include thread original messages" do message_3.update!(thread: thread) - expect(subject[:future_messages]).to eq([thread.original_message]) + expect(query[:future_messages]).to eq([thread.original_message]) end context "when include_thread_messages is true" do @@ -94,7 +94,7 @@ RSpec.describe Chat::MessagesQuery do thread: thread, created_at: thread.original_message.created_at + 1.minute, ) - expect(subject[:future_messages]).to eq([thread.original_message, message_3]) + expect(query[:future_messages]).to eq([thread.original_message, message_3]) end end @@ -105,7 +105,7 @@ RSpec.describe Chat::MessagesQuery do thread: thread, created_at: thread.original_message.created_at + 1.minute, ) - expect(subject[:future_messages]).to eq([thread.original_message, message_3]) + expect(query[:future_messages]).to eq([thread.original_message, message_3]) end end end @@ -115,7 +115,7 @@ RSpec.describe Chat::MessagesQuery do it "does include deleted messages" do message_3.trash! - expect(subject[:future_messages]).to eq([message_3]) + expect(query[:future_messages]).to eq([message_3]) end end end @@ -124,7 +124,7 @@ RSpec.describe Chat::MessagesQuery do let(:target_date) { 1.day.ago } it "queries messages in the channel and finds the past and future messages" do - expect(subject).to eq( + expect(query).to eq( past_messages: [message_1], future_messages: [message_2, message_3], target_date: target_date, @@ -136,7 +136,7 @@ RSpec.describe Chat::MessagesQuery do context "when target_message_id not provided" do it "queries messages in the channel" do - expect(subject).to eq( + expect(query).to eq( messages: [message_1, message_2, message_3], can_load_more_past: false, can_load_more_future: false, @@ -147,7 +147,7 @@ RSpec.describe Chat::MessagesQuery do let(:page_size) { 3 } it "can_load_more_past is true" do - expect(subject[:can_load_more_past]).to be_truthy + expect(query[:can_load_more_past]).to be_truthy end end @@ -155,14 +155,14 @@ RSpec.describe Chat::MessagesQuery do let(:direction) { described_class::FUTURE } it "returns messages in ascending order by created_at" do - expect(subject[:messages]).to eq([message_1, message_2, message_3]) + expect(query[:messages]).to eq([message_1, message_2, message_3]) end context "when the messages length is equal to the page_size" do let(:page_size) { 3 } it "can_load_more_future is true" do - expect(subject[:can_load_more_future]).to be_truthy + expect(query[:can_load_more_future]).to be_truthy end end end @@ -171,14 +171,14 @@ RSpec.describe Chat::MessagesQuery do let(:direction) { described_class::PAST } it "returns messages in ascending order by created_at" do - expect(subject[:messages]).to eq([message_1, message_2, message_3]) + expect(query[:messages]).to eq([message_1, message_2, message_3]) end context "when the messages length is equal to the page_size" do let(:page_size) { 3 } it "can_load_more_past is true" do - expect(subject[:can_load_more_past]).to be_truthy + expect(query[:can_load_more_past]).to be_truthy end end end diff --git a/plugins/chat/spec/queries/chat/thread_unreads_query_spec.rb b/plugins/chat/spec/queries/chat/thread_unreads_query_spec.rb index 6b4e83e3f16..2999eef5108 100644 --- a/plugins/chat/spec/queries/chat/thread_unreads_query_spec.rb +++ b/plugins/chat/spec/queries/chat/thread_unreads_query_spec.rb @@ -3,6 +3,16 @@ require "rails_helper" describe Chat::ThreadUnreadsQuery do + subject(:query) do + described_class.call( + channel_ids: channel_ids, + thread_ids: thread_ids, + user_id: current_user.id, + include_missing_memberships: include_missing_memberships, + include_read: include_read, + ) + end + fab!(:channel_1) { Fabricate(:category_channel, threading_enabled: true) } fab!(:channel_2) { Fabricate(:category_channel, threading_enabled: true) } fab!(:thread_1) { Fabricate(:chat_thread, channel: channel_1) } @@ -16,15 +26,6 @@ describe Chat::ThreadUnreadsQuery do let(:include_read) { true } let(:channel_ids) { [] } let(:thread_ids) { [] } - let(:subject) do - described_class.call( - channel_ids: channel_ids, - thread_ids: thread_ids, - user_id: current_user.id, - include_missing_memberships: include_missing_memberships, - include_read: include_read, - ) - end before do SiteSetting.chat_enabled = true @@ -47,7 +48,7 @@ describe Chat::ThreadUnreadsQuery do let(:channel_ids) { [channel_1.id, channel_2.id] } it "gets a count of all the thread unreads across the channels" do - expect(subject.map(&:to_h)).to match_array( + expect(query.map(&:to_h)).to match_array( [ { channel_id: channel_1.id, mention_count: 0, thread_id: thread_1.id, unread_count: 1 }, { channel_id: channel_1.id, mention_count: 0, thread_id: thread_2.id, unread_count: 0 }, @@ -59,17 +60,17 @@ describe Chat::ThreadUnreadsQuery do it "does not count deleted messages" do message_1.trash! - expect(subject.map(&:to_h).find { |tracking| tracking[:thread_id] == thread_1.id }).to eq( + expect(query.map(&:to_h).find { |tracking| tracking[:thread_id] == thread_1.id }).to eq( { channel_id: channel_1.id, mention_count: 0, thread_id: thread_1.id, unread_count: 0 }, ) end it "does not messages in threads where threading_enabled is false on the channel" do channel_1.update!(threading_enabled: false) - expect(subject.map(&:to_h).find { |tracking| tracking[:thread_id] == thread_1.id }).to eq( + expect(query.map(&:to_h).find { |tracking| tracking[:thread_id] == thread_1.id }).to eq( { channel_id: channel_1.id, mention_count: 0, thread_id: thread_1.id, unread_count: 0 }, ) - expect(subject.map(&:to_h).find { |tracking| tracking[:thread_id] == thread_2.id }).to eq( + expect(query.map(&:to_h).find { |tracking| tracking[:thread_id] == thread_2.id }).to eq( { channel_id: channel_1.id, mention_count: 0, thread_id: thread_2.id, unread_count: 0 }, ) end @@ -79,7 +80,7 @@ describe Chat::ThreadUnreadsQuery do .user_chat_thread_memberships .find_by(user: current_user) .update!(last_read_message_id: message_1.id) - expect(subject.map(&:to_h).find { |tracking| tracking[:thread_id] == thread_1.id }).to eq( + expect(query.map(&:to_h).find { |tracking| tracking[:thread_id] == thread_1.id }).to eq( { channel_id: channel_1.id, mention_count: 0, thread_id: thread_1.id, unread_count: 0 }, ) end @@ -87,7 +88,7 @@ describe Chat::ThreadUnreadsQuery do it "does not count the original message ID as unread" do thread_1.original_message.destroy thread_1.update!(original_message: message_1) - expect(subject.map(&:to_h).find { |tracking| tracking[:thread_id] == thread_1.id }).to eq( + expect(query.map(&:to_h).find { |tracking| tracking[:thread_id] == thread_1.id }).to eq( { channel_id: channel_1.id, mention_count: 0, thread_id: thread_1.id, unread_count: 0 }, ) end @@ -96,7 +97,7 @@ describe Chat::ThreadUnreadsQuery do let(:include_read) { false } it "does not get threads with no unread messages" do - expect(subject.map(&:to_h)).not_to include( + expect(query.map(&:to_h)).not_to include( [ { channel_id: channel_1.id, @@ -114,7 +115,7 @@ describe Chat::ThreadUnreadsQuery do let(:thread_ids) { [thread_1.id, thread_3.id] } it "gets a count of all the thread unreads for the specified threads" do - expect(subject.map(&:to_h)).to match_array( + expect(query.map(&:to_h)).to match_array( [ { channel_id: channel_1.id, mention_count: 0, thread_id: thread_1.id, unread_count: 1 }, { channel_id: channel_2.id, mention_count: 0, thread_id: thread_3.id, unread_count: 1 }, @@ -131,7 +132,7 @@ describe Chat::ThreadUnreadsQuery do end it "gets a zeroed out count for the thread" do - expect(subject.map(&:to_h)).to include( + expect(query.map(&:to_h)).to include( { channel_id: channel_1.id, mention_count: 0, thread_id: thread_1.id, unread_count: 0 }, ) end @@ -146,7 +147,7 @@ describe Chat::ThreadUnreadsQuery do end it "gets a zeroed out count for the thread" do - expect(subject.map(&:to_h)).to include( + expect(query.map(&:to_h)).to include( { channel_id: channel_1.id, mention_count: 0, thread_id: thread_1.id, unread_count: 0 }, ) end @@ -156,7 +157,7 @@ describe Chat::ThreadUnreadsQuery do before { thread_1.user_chat_thread_memberships.find_by(user: current_user).destroy! } it "does not get that thread unread count by default" do - expect(subject.map(&:to_h)).to match_array( + expect(query.map(&:to_h)).to match_array( [ { channel_id: channel_2.id, @@ -172,7 +173,7 @@ describe Chat::ThreadUnreadsQuery do let(:include_missing_memberships) { true } it "includes the thread that the user is not a member of with zeroed out counts" do - expect(subject.map(&:to_h)).to match_array( + expect(query.map(&:to_h)).to match_array( [ { channel_id: channel_1.id, @@ -194,7 +195,7 @@ describe Chat::ThreadUnreadsQuery do let(:include_read) { false } it "does not include the thread that the user is not a member of with zeroed out counts" do - expect(subject.map(&:to_h)).to match_array( + expect(query.map(&:to_h)).to match_array( [ { channel_id: channel_2.id, @@ -215,7 +216,7 @@ describe Chat::ThreadUnreadsQuery do let(:thread_ids) { [thread_1.id, thread_3.id] } it "gets a count of all the thread unreads across the channels filtered by thread id" do - expect(subject.map(&:to_h)).to match_array( + expect(query.map(&:to_h)).to match_array( [ { channel_id: channel_1.id, mention_count: 0, thread_id: thread_1.id, unread_count: 1 }, { channel_id: channel_2.id, mention_count: 0, thread_id: thread_3.id, unread_count: 1 }, diff --git a/plugins/chat/spec/queries/chat/tracking_state_report_query_spec.rb b/plugins/chat/spec/queries/chat/tracking_state_report_query_spec.rb index 76d7146616c..5a8ad346cef 100644 --- a/plugins/chat/spec/queries/chat/tracking_state_report_query_spec.rb +++ b/plugins/chat/spec/queries/chat/tracking_state_report_query_spec.rb @@ -1,15 +1,7 @@ # frozen_string_literal: true RSpec.describe Chat::TrackingStateReportQuery do - fab!(:current_user) { Fabricate(:user) } - let(:guardian) { current_user.guardian } - - let(:channel_ids) { [] } - let(:thread_ids) { [] } - let(:include_missing_memberships) { false } - let(:include_threads) { false } - let(:include_read) { true } - let(:subject) do + subject(:query) do described_class.call( guardian: guardian, channel_ids: channel_ids, @@ -20,9 +12,17 @@ RSpec.describe Chat::TrackingStateReportQuery do ) end + fab!(:current_user) { Fabricate(:user) } + let(:guardian) { current_user.guardian } + + let(:channel_ids) { [] } + let(:thread_ids) { [] } + let(:include_missing_memberships) { false } + let(:include_threads) { false } + let(:include_read) { true } context "when channel_ids empty" do it "returns empty object for channel_tracking" do - expect(subject.channel_tracking).to eq({}) + expect(query.channel_tracking).to eq({}) end end @@ -41,7 +41,7 @@ RSpec.describe Chat::TrackingStateReportQuery do include_read: include_read, ) .returns([]) - subject + query end it "generates a correct unread report for the channels the user is a member of" do @@ -50,7 +50,7 @@ RSpec.describe Chat::TrackingStateReportQuery do Fabricate(:chat_message, chat_channel: channel_1) Fabricate(:chat_message, chat_channel: channel_2) - expect(subject.channel_tracking).to eq( + expect(query.channel_tracking).to eq( { channel_1.id => { unread_count: 1, @@ -66,7 +66,7 @@ RSpec.describe Chat::TrackingStateReportQuery do it "does not include threads by default" do Chat::ThreadUnreadsQuery.expects(:call).never - expect(subject.thread_tracking).to eq({}) + expect(query.thread_tracking).to eq({}) end context "when include_threads is true" do @@ -90,7 +90,7 @@ RSpec.describe Chat::TrackingStateReportQuery do include_read: include_read, ) .returns([]) - subject + query end it "generates a correct unread for the threads the user is a member of in the channels" do @@ -101,7 +101,7 @@ RSpec.describe Chat::TrackingStateReportQuery do Fabricate(:chat_message, chat_channel: channel_1, thread: thread_1) Fabricate(:chat_message, chat_channel: channel_2, thread: thread_2) - expect(subject.channel_tracking).to eq( + expect(query.channel_tracking).to eq( { channel_1.id => { unread_count: 1, @@ -113,7 +113,7 @@ RSpec.describe Chat::TrackingStateReportQuery do }, }, ) - expect(subject.thread_tracking).to eq( + expect(query.thread_tracking).to eq( { thread_1.id => { unread_count: 1, @@ -135,7 +135,7 @@ RSpec.describe Chat::TrackingStateReportQuery do it "does not query threads" do Chat::ThreadUnreadsQuery.expects(:call).never - expect(subject.thread_tracking).to eq({}) + expect(query.thread_tracking).to eq({}) end end end diff --git a/plugins/chat/spec/serializer/chat/channel_serializer_spec.rb b/plugins/chat/spec/serializer/chat/channel_serializer_spec.rb index b8c4e9c52b8..8a4160ff9cd 100644 --- a/plugins/chat/spec/serializer/chat/channel_serializer_spec.rb +++ b/plugins/chat/spec/serializer/chat/channel_serializer_spec.rb @@ -3,23 +3,25 @@ require "rails_helper" describe Chat::ChannelSerializer do + subject(:serializer) { described_class.new(chat_channel, scope: guardian, root: nil) } + fab!(:user) { Fabricate(:user) } fab!(:admin) { Fabricate(:admin) } fab!(:chat_channel) { Fabricate(:chat_channel) } + let(:guardian_user) { user } let(:guardian) { Guardian.new(guardian_user) } - subject { described_class.new(chat_channel, scope: guardian, root: nil) } describe "archive status" do context "when user is not staff" do let(:guardian_user) { user } it "does not return any sort of archive status" do - expect(subject.as_json.key?(:archive_completed)).to eq(false) + expect(serializer.as_json.key?(:archive_completed)).to eq(false) end it "includes allow_channel_wide_mentions" do - expect(subject.as_json.key?(:allow_channel_wide_mentions)).to eq(true) + expect(serializer.as_json.key?(:allow_channel_wide_mentions)).to eq(true) end end @@ -27,10 +29,10 @@ describe Chat::ChannelSerializer do let(:guardian_user) { admin } it "includes the archive status if the channel is archived and the archive record exists" do - expect(subject.as_json.key?(:archive_completed)).to eq(false) + expect(serializer.as_json.key?(:archive_completed)).to eq(false) chat_channel.update!(status: Chat::Channel.statuses[:archived]) - expect(subject.as_json.key?(:archive_completed)).to eq(false) + expect(serializer.as_json.key?(:archive_completed)).to eq(false) Chat::ChannelArchive.create!( chat_channel: chat_channel, @@ -39,11 +41,11 @@ describe Chat::ChannelSerializer do total_messages: 10, ) chat_channel.reload - expect(subject.as_json.key?(:archive_completed)).to eq(true) + expect(serializer.as_json.key?(:archive_completed)).to eq(true) end it "includes allow_channel_wide_mentions" do - expect(subject.as_json.key?(:allow_channel_wide_mentions)).to eq(true) + expect(serializer.as_json.key?(:allow_channel_wide_mentions)).to eq(true) end end end @@ -63,7 +65,7 @@ describe Chat::ChannelSerializer do MessageBus.expects(:last_id).with( Chat::Publisher.kick_users_message_bus_channel(chat_channel.id), ) - expect(subject.as_json.dig(:meta, :message_bus_last_ids).keys).to eq( + expect(serializer.as_json.dig(:meta, :message_bus_last_ids).keys).to eq( %i[channel_message_bus_last_id new_messages new_mentions kick], ) end @@ -73,7 +75,7 @@ describe Chat::ChannelSerializer do MessageBus.expects(:last_id).with( Chat::Publisher.kick_users_message_bus_channel(chat_channel.id), ) - expect(subject.as_json[:meta][:message_bus_last_ids].key?(:kick)).to eq(true) + expect(serializer.as_json[:meta][:message_bus_last_ids].key?(:kick)).to eq(true) end it "does not call MessageBus for last_id if all the last IDs are already passed in" do @@ -101,7 +103,7 @@ describe Chat::ChannelSerializer do MessageBus.expects(:last_id).with( Chat::Publisher.new_mentions_message_bus_channel(chat_channel.id), ) - expect(subject.as_json.dig(:meta, :message_bus_last_ids).keys).to eq( + expect(serializer.as_json.dig(:meta, :message_bus_last_ids).keys).to eq( %i[channel_message_bus_last_id new_messages new_mentions], ) end @@ -109,7 +111,7 @@ describe Chat::ChannelSerializer do it "does not get the kick_message_bus_last_id" do MessageBus.expects(:last_id).at_least_once MessageBus.expects(:last_id).never - expect(subject.as_json[:meta][:message_bus_last_ids].key?(:kick)).to eq(false) + expect(serializer.as_json[:meta][:message_bus_last_ids].key?(:kick)).to eq(false) end end end diff --git a/plugins/chat/spec/serializer/chat/chat_message_serializer_spec.rb b/plugins/chat/spec/serializer/chat/chat_message_serializer_spec.rb index 9f1218b0618..89abcb42bc9 100644 --- a/plugins/chat/spec/serializer/chat/chat_message_serializer_spec.rb +++ b/plugins/chat/spec/serializer/chat/chat_message_serializer_spec.rb @@ -3,13 +3,14 @@ require "rails_helper" describe Chat::MessageSerializer do + subject(:serializer) { described_class.new(message_1, scope: guardian, root: nil) } + fab!(:chat_channel) { Fabricate(:category_channel) } fab!(:message_poster) { Fabricate(:user) } fab!(:message_1) { Fabricate(:chat_message, user: message_poster, chat_channel: chat_channel) } fab!(:guardian_user) { Fabricate(:user) } - let(:guardian) { Guardian.new(guardian_user) } - subject { described_class.new(message_1, scope: guardian, root: nil) } + let(:guardian) { Guardian.new(guardian_user) } describe "#reactions" do fab!(:custom_emoji) { CustomEmoji.create!(name: "trout", upload: Fabricate(:upload)) } @@ -21,13 +22,13 @@ describe Chat::MessageSerializer do it "doesn’t return the reaction" do Emoji.clear_cache - trout_reaction = subject.as_json[:reactions].find { |r| r[:emoji] == "trout" } + trout_reaction = serializer.as_json[:reactions].find { |r| r[:emoji] == "trout" } expect(trout_reaction).to be_present custom_emoji.destroy! Emoji.clear_cache - trout_reaction = subject.as_json[:reactions].find { |r| r[:emoji] == "trout" } + trout_reaction = serializer.as_json[:reactions].find { |r| r[:emoji] == "trout" } expect(trout_reaction).to_not be_present end end @@ -49,7 +50,7 @@ describe Chat::MessageSerializer do message_1.user.destroy! message_1.reload - expect(subject.as_json[:user][:username]).to eq(I18n.t("chat.deleted_chat_username")) + expect(serializer.as_json[:user][:username]).to eq(I18n.t("chat.deleted_chat_username")) end end end @@ -60,14 +61,14 @@ describe Chat::MessageSerializer do message_1.user.destroy! message_1.reload - expect(subject.as_json[:deleted_at]).to(be_within(1.second).of(Time.zone.now)) + expect(serializer.as_json[:deleted_at]).to(be_within(1.second).of(Time.zone.now)) end it "is marked as deleted by system user" do message_1.user.destroy! message_1.reload - expect(subject.as_json[:deleted_by_id]).to eq(Discourse.system_user.id) + expect(serializer.as_json[:deleted_by_id]).to eq(Discourse.system_user.id) end end end diff --git a/plugins/chat/spec/serializer/chat/chat_message_user_serializer_spec.rb b/plugins/chat/spec/serializer/chat/chat_message_user_serializer_spec.rb index 94c63dceca6..fa12515bc2b 100644 --- a/plugins/chat/spec/serializer/chat/chat_message_user_serializer_spec.rb +++ b/plugins/chat/spec/serializer/chat/chat_message_user_serializer_spec.rb @@ -3,7 +3,7 @@ require "rails_helper" RSpec.describe Chat::MessageUserSerializer do - subject do + subject(:serializer) do user = Fabricate(:user, **params) guardian = Guardian.new(user) described_class.new(user, scope: guardian, root: nil).as_json @@ -15,11 +15,11 @@ RSpec.describe Chat::MessageUserSerializer do context "with default user" do it "displays user as regular" do - expect(subject[:new_user]).to eq(false) - expect(subject[:staff]).to eq(false) - expect(subject[:admin]).to eq(false) - expect(subject[:moderator]).to eq(false) - expect(subject[:primary_group_name]).to be_blank + expect(serializer[:new_user]).to eq(false) + expect(serializer[:staff]).to eq(false) + expect(serializer[:admin]).to eq(false) + expect(serializer[:moderator]).to eq(false) + expect(serializer[:primary_group_name]).to be_blank end end @@ -27,7 +27,7 @@ RSpec.describe Chat::MessageUserSerializer do before { params[:trust_level] = TrustLevel[0] } it "displays user as new" do - expect(subject[:new_user]).to eq(true) + expect(serializer[:new_user]).to eq(true) end end @@ -35,7 +35,7 @@ RSpec.describe Chat::MessageUserSerializer do before { params[:admin] = true } it "displays user as staff" do - expect(subject[:staff]).to eq(true) + expect(serializer[:staff]).to eq(true) end end @@ -43,7 +43,7 @@ RSpec.describe Chat::MessageUserSerializer do before { params[:admin] = true } it "displays user as admin" do - expect(subject[:admin]).to eq(true) + expect(serializer[:admin]).to eq(true) end end @@ -51,7 +51,7 @@ RSpec.describe Chat::MessageUserSerializer do before { params[:moderator] = true } it "displays user as moderator" do - expect(subject[:moderator]).to eq(true) + expect(serializer[:moderator]).to eq(true) end end @@ -61,7 +61,7 @@ RSpec.describe Chat::MessageUserSerializer do before { params[:primary_group_id] = group.id } it "displays user as moderator" do - expect(subject[:primary_group_name]).to eq(group.name) + expect(serializer[:primary_group_name]).to eq(group.name) end end end diff --git a/plugins/chat/spec/services/chat/channel_view_builder_spec.rb b/plugins/chat/spec/services/chat/channel_view_builder_spec.rb index 99a9eb9e917..9717346cd94 100644 --- a/plugins/chat/spec/services/chat/channel_view_builder_spec.rb +++ b/plugins/chat/spec/services/chat/channel_view_builder_spec.rb @@ -11,6 +11,8 @@ RSpec.describe Chat::ChannelViewBuilder do end describe ".call" do + subject(:result) { described_class.call(params) } + fab!(:current_user) { Fabricate(:user) } fab!(:channel) { Fabricate(:category_channel) } @@ -35,14 +37,12 @@ RSpec.describe Chat::ChannelViewBuilder do } end - subject(:result) { described_class.call(params) } - it "threads_enabled is false by default" do - expect(subject.threads_enabled).to eq(false) + expect(result.threads_enabled).to eq(false) end it "include_thread_messages is true by default" do - expect(subject.include_thread_messages).to eq(true) + expect(result.include_thread_messages).to eq(true) end it "queries messages" do @@ -59,7 +59,7 @@ RSpec.describe Chat::ChannelViewBuilder do target_date: target_date, ) .returns({ messages: [] }) - subject + result end it "returns channel messages and thread replies" do @@ -71,23 +71,23 @@ RSpec.describe Chat::ChannelViewBuilder do chat_channel: channel, thread: Fabricate(:chat_thread, channel: channel), ) - expect(subject.view.chat_messages).to eq( + expect(result.view.chat_messages).to eq( [message_1, message_2, message_3.thread.original_message, message_3], ) end it "does not query thread tracking overview or state by default" do Chat::TrackingStateReportQuery.expects(:call).never - subject + result end it "does not query threads by default" do Chat::Thread.expects(:where).never - subject + result end it "returns a Chat::View" do - expect(subject.view).to be_a(Chat::View) + expect(result.view).to be_a(Chat::View) end context "when page_size is null" do @@ -109,11 +109,11 @@ RSpec.describe Chat::ChannelViewBuilder do end it "threads_enabled is true" do - expect(subject.threads_enabled).to eq(true) + expect(result.threads_enabled).to eq(true) end it "include_thread_messages is false" do - expect(subject.include_thread_messages).to eq(false) + expect(result.include_thread_messages).to eq(false) end it "returns channel messages but not thread replies" do @@ -125,7 +125,7 @@ RSpec.describe Chat::ChannelViewBuilder do chat_channel: channel, thread: Fabricate(:chat_thread, channel: channel), ) - expect(subject.view.chat_messages).to eq( + expect(result.view.chat_messages).to eq( [message_1, message_2, message_3.thread.original_message], ) end @@ -137,7 +137,7 @@ RSpec.describe Chat::ChannelViewBuilder do chat_channel: channel, thread: Fabricate(:chat_thread, channel: channel), ) - expect(subject.view.threads).to eq([message_1.thread]) + expect(result.view.threads).to eq([message_1.thread]) end it "fetches thread memberships for the current user for fetched threads" do @@ -148,7 +148,7 @@ RSpec.describe Chat::ChannelViewBuilder do thread: Fabricate(:chat_thread, channel: channel), ) message_1.thread.add(current_user) - expect(subject.view.thread_memberships).to eq( + expect(result.view.thread_memberships).to eq( [message_1.thread.membership_for(current_user)], ) end @@ -171,21 +171,21 @@ RSpec.describe Chat::ChannelViewBuilder do .with(guardian: guardian, thread_ids: [thread.id], include_threads: true) .returns(Chat::TrackingStateReport.new) .once - subject + result end it "fetches an overview of threads with unread messages in the channel" do thread = Fabricate(:chat_thread, channel: channel) thread.add(current_user) message_1 = Fabricate(:chat_message, chat_channel: channel, thread: thread) - expect(subject.view.unread_thread_ids).to eq([message_1.thread.id]) + expect(result.view.unread_thread_ids).to eq([message_1.thread.id]) end it "fetches the tracking state of threads in the channel" do thread = Fabricate(:chat_thread, channel: channel) thread.add(current_user) Fabricate(:chat_message, chat_channel: channel, thread: thread) - expect(subject.view.tracking.thread_tracking).to eq( + expect(result.view.tracking.thread_tracking).to eq( { thread.id => { channel_id: channel.id, unread_count: 1, mention_count: 0 } }, ) end @@ -194,7 +194,7 @@ RSpec.describe Chat::ChannelViewBuilder do let(:thread_id) { Fabricate(:chat_thread, channel: channel).id } it "include_thread_messages is true" do - expect(subject.include_thread_messages).to eq(true) + expect(result.include_thread_messages).to eq(true) end end end @@ -233,7 +233,7 @@ RSpec.describe Chat::ChannelViewBuilder do context "if the user is not a member of the channel" do it "does not error and still returns messages" do - expect(subject.view.chat_messages).to eq([past_message_2, past_message_1, message]) + expect(result.view.chat_messages).to eq([past_message_2, past_message_1, message]) end end @@ -246,7 +246,7 @@ RSpec.describe Chat::ChannelViewBuilder do before { membership.update!(last_read_message_id: past_message_1.id) } it "uses the last_read_message_id of the user's membership as the target_message_id" do - expect(subject.view.chat_messages).to eq([past_message_2, past_message_1, message]) + expect(result.view.chat_messages).to eq([past_message_2, past_message_1, message]) end end @@ -254,7 +254,7 @@ RSpec.describe Chat::ChannelViewBuilder do before { membership.update!(last_read_message_id: nil) } it "does not error and still returns messages" do - expect(subject.view.chat_messages).to eq([past_message_2, past_message_1, message]) + expect(result.view.chat_messages).to eq([past_message_2, past_message_1, message]) end context "if page_size is nil" do @@ -266,7 +266,7 @@ RSpec.describe Chat::ChannelViewBuilder do .with(has_entries(page_size: Chat::MessagesQuery::MAX_PAGE_SIZE)) .once .returns({ messages: [] }) - subject + result end end end @@ -288,7 +288,7 @@ RSpec.describe Chat::ChannelViewBuilder do let(:target_message_id) { message.id } it "includes the target message as well as past and future messages" do - expect(subject.view.chat_messages).to eq([past_message, message, future_message]) + expect(result.view.chat_messages).to eq([past_message, message, future_message]) end context "when page_size is null" do @@ -303,7 +303,7 @@ RSpec.describe Chat::ChannelViewBuilder do before { message.update!(thread: thread) } it "includes it by default" do - expect(subject.view.chat_messages).to eq( + expect(result.view.chat_messages).to eq( [past_message, message, thread.original_message, future_message], ) end @@ -315,7 +315,7 @@ RSpec.describe Chat::ChannelViewBuilder do end it "does not include the target message" do - expect(subject.view.chat_messages).to eq( + expect(result.view.chat_messages).to eq( [past_message, thread.original_message, future_message], ) end @@ -356,7 +356,7 @@ RSpec.describe Chat::ChannelViewBuilder do let(:target_date) { 2.days.ago } it "includes past and future messages" do - expect(subject.view.chat_messages).to eq([past_message, future_message]) + expect(result.view.chat_messages).to eq([past_message, future_message]) end end end diff --git a/plugins/poll/spec/lib/polls_validator_spec.rb b/plugins/poll/spec/lib/polls_validator_spec.rb index 583a96c80e7..e337db78003 100644 --- a/plugins/poll/spec/lib/polls_validator_spec.rb +++ b/plugins/poll/spec/lib/polls_validator_spec.rb @@ -3,8 +3,9 @@ require "rails_helper" RSpec.describe ::DiscoursePoll::PollsValidator do + subject(:validator) { described_class.new(post) } + let(:post) { Fabricate(:post) } - subject { described_class.new(post) } describe "#validate_polls" do it "ensures that polls have valid arguments" do diff --git a/spec/integration/email_style_spec.rb b/spec/integration/email_style_spec.rb index 1b6aa37a7da..645ed98a70e 100644 --- a/spec/integration/email_style_spec.rb +++ b/spec/integration/email_style_spec.rb @@ -23,11 +23,12 @@ RSpec.describe EmailStyle do end context "with invite" do - fab!(:invite) { Fabricate(:invite) } - let(:invite_mail) { InviteMailer.send_invite(invite) } - subject(:mail_html) { Email::Renderer.new(invite_mail).html } + fab!(:invite) { Fabricate(:invite) } + + let(:invite_mail) { InviteMailer.send_invite(invite) } + it "applies customizations" do expect(mail_html.scan('