discourse/plugins/chat/spec/models/direct_message_spec.rb
Gerhard Schlager 7ef482a292
REFACTOR: Fix pluralized strings in chat plugin (#20357)
* FIX: Use pluralized string

* REFACTOR: Fix misuse of pluralized string

* REFACTOR: Fix misuse of pluralized string

* DEV: Remove linting of `one` key in MessageFormat string, it doesn't work

* REFACTOR: Fix misuse of pluralized string

This also ensures that the URL works on subfolder and shows the site setting link only for admins instead of staff. The string is quite complicated, so the best option was to switch to MessageFormat.

* REFACTOR: Fix misuse of pluralized string

* FIX: Use pluralized string

This also ensures that the URL works on subfolder and shows the site setting link only for admins instead of staff.

* REFACTOR: Correctly pluralize reaction tooltips in chat

This also ensures that maximum 5 usernames are shown and fixes the number of "others" which was off by 1 if the current user reacted on a message.

* REFACTOR: Use translatable string as comma separator

* DEV: Add comment to translation to clarify the meaning of `%{identifier}`

* REFACTOR: Use translatable comma separator and use explicit interpolation keys

* REFACTOR: Don't interpolate lowercase channel status

* REFACTOR: Fix misuse of pluralized string

* REFACTOR: Don't interpolate channel status

* REFACTOR: Use %{count} interpolation key

* REFACTOR: Fix misuse of pluralized string

* REFACTOR: Correctly pluralize DM chat channel titles
2023-02-20 10:31:02 +01:00

79 lines
2.7 KiB
Ruby

# frozen_string_literal: true
require "rails_helper"
describe DirectMessage do
fab!(:user1) { Fabricate(:user, username: "chatdmfellow1") }
fab!(:user2) { Fabricate(:user, username: "chatdmuser") }
fab!(:chat_channel) { Fabricate(:direct_message_channel) }
it_behaves_like "a chatable model" do
fab!(:chatable) { Fabricate(:direct_message) }
let(:channel_class) { DirectMessageChannel }
end
describe "#chat_channel_title_for_user" do
it "returns a nicely formatted name if it's more than one user" do
user3 = Fabricate.build(:user, username: "chatdmregent")
direct_message = Fabricate(:direct_message, users: [user1, user2, user3])
expect(direct_message.chat_channel_title_for_user(chat_channel, user1)).to eq(
I18n.t(
"chat.channel.dm_title.multi_user",
comma_separated_usernames:
[user3, user2].map { |u| "@#{u.username}" }.join(I18n.t("word_connector.comma")),
),
)
end
it "returns a nicely formatted truncated name if it's more than 5 users" do
user3 = Fabricate.build(:user, username: "chatdmregent")
users = [user1, user2, user3].concat(
5.times.map.with_index { |i| Fabricate(:user, username: "chatdmuser#{i}") },
)
direct_message = Fabricate(:direct_message, users: users)
expect(direct_message.chat_channel_title_for_user(chat_channel, user1)).to eq(
I18n.t(
"chat.channel.dm_title.multi_user_truncated",
comma_separated_usernames:
users[1..5]
.sort_by(&:username)
.map { |u| "@#{u.username}" }
.join(I18n.t("word_connector.comma")),
count: 2,
),
)
end
it "returns the other user's username if it's a dm to that user" do
direct_message = Fabricate(:direct_message, users: [user1, user2])
expect(direct_message.chat_channel_title_for_user(chat_channel, user1)).to eq(
I18n.t("chat.channel.dm_title.single_user", username: "@#{user2.username}"),
)
end
it "returns the current user's username if it's a dm to self" do
direct_message = Fabricate(:direct_message, users: [user1])
expect(direct_message.chat_channel_title_for_user(chat_channel, user1)).to eq(
I18n.t("chat.channel.dm_title.single_user", username: "@#{user1.username}"),
)
end
context "when user is deleted" do
it "returns a placeholder username" do
direct_message = Fabricate(:direct_message, users: [user1, user2])
user2.destroy!
direct_message.reload
expect(direct_message.chat_channel_title_for_user(chat_channel, user1)).to eq(
"@#{I18n.t("chat.deleted_chat_username")}",
)
end
end
end
end