DEV: more tests for mentions when updating chat messages (#21185)

This adds these two new test cases:

    context "when updating a mentioned user" do
      it "updates the mention record" do

    # and
    context "when there are duplicate mentions" do
        it "creates a single mention record per mention" do

Apart from that, this groups mention related tests into a context, renames one test, and moves setup of another test into the test case itself from the before block (to make it more clear, that test is the only one that uses that setup). See the PR's commit history.
This commit is contained in:
Andrei Prigorshnev
2023-04-21 16:58:01 +04:00
committed by GitHub
parent 3e414996c2
commit e7292e1682

View File

@ -30,8 +30,6 @@ describe Chat::MessageUpdater do
Fabricate(:user_chat_channel_membership, chat_channel: public_chat_channel, user: user)
end
Group.refresh_automatic_groups!
@direct_message_channel =
Chat::DirectMessageChannelCreator.create!(acting_user: user1, target_users: [user1, user2])
end
def create_chat_message(user, message, channel, upload_ids: nil)
@ -126,6 +124,7 @@ describe Chat::MessageUpdater do
expect(events.map { _1[:event_name] }).to include(:chat_message_edited)
end
context "with mentions" do
it "sends notifications if a message was updated with new mentions" do
message = create_chat_message(user1, "Mentioning @#{user2.username}", public_chat_channel)
@ -165,9 +164,13 @@ describe Chat::MessageUpdater do
expect(mention.notification).to be_nil
end
it "destroys mention notifications that should be removed" do
it "destroys mentions that should be removed" do
chat_message =
create_chat_message(user1, "ping @#{user2.username} @#{user3.username}", public_chat_channel)
create_chat_message(
user1,
"ping @#{user2.username} @#{user3.username}",
public_chat_channel,
)
expect {
Chat::MessageUpdater.update(
guardian: guardian,
@ -179,7 +182,11 @@ describe Chat::MessageUpdater do
it "creates new, leaves existing, and removes old mentions all at once" do
chat_message =
create_chat_message(user1, "ping @#{user2.username} @#{user3.username}", public_chat_channel)
create_chat_message(
user1,
"ping @#{user2.username} @#{user3.username}",
public_chat_channel,
)
Chat::MessageUpdater.update(
guardian: guardian,
chat_message: chat_message,
@ -192,7 +199,9 @@ describe Chat::MessageUpdater do
end
it "doesn't create mention notification in direct message for users without access" do
message = create_chat_message(user1, "ping nobody", @direct_message_channel)
direct_message_channel =
Chat::DirectMessageChannelCreator.create!(acting_user: user1, target_users: [user1, user2])
message = create_chat_message(user1, "ping nobody", direct_message_channel)
Chat::MessageUpdater.update(
guardian: guardian,
@ -204,6 +213,39 @@ describe Chat::MessageUpdater do
expect(mention.notification).to be_nil
end
context "when updating a mentioned user" do
it "updates the mention record" do
chat_message = create_chat_message(user1, "ping @#{user2.username}", public_chat_channel)
Chat::MessageUpdater.update(
guardian: guardian,
chat_message: chat_message,
new_content: "ping @#{user3.username}",
)
user2_mentions = user2.chat_mentions.where(chat_message: chat_message)
expect(user2_mentions.length).to be(0)
user3_mentions = user3.chat_mentions.where(chat_message: chat_message)
expect(user3_mentions.length).to be(1)
end
end
context "when there are duplicate mentions" do
it "creates a single mention record per user" do
chat_message = create_chat_message(user1, "ping @#{user2.username}", public_chat_channel)
Chat::MessageUpdater.update(
guardian: guardian,
chat_message: chat_message,
new_content: "ping @#{user2.username} @#{user2.username} edited",
)
expect(user2.chat_mentions.where(chat_message: chat_message).count).to eq(1)
end
end
end
describe "group mentions" do
it "creates group mentions on update" do
chat_message = create_chat_message(user1, "ping nobody", public_chat_channel)