discourse/plugins/chat/spec/system/message_notifications_mobile_spec.rb
chapoi 4359fd7c75
DEV: Split mobile chat unread indicators by current vs other channels (#39115)
- On mobile, the `navbar back-button` unread indicator now only reflects
unreads from other channels, excluding the
currently viewed one (addresses
https://meta.discourse.org/t/notification-about-chat-messages-in-the-header/386167/8?u=chapoi)
- The `scroll-to-bottom button` now displays a "N new messages" text
label for unread messages in the current channel
(only when the original arrow-button is rendered)
- Both indicators render independently — if there are unreads in both
the current and other channels, both appear
- Fixed || to ?? fallback logic in ChatHeaderIconUnreadIndicator so that
explicit 0 counts are respected instead of
    falling through to global aggregates 

<img width="730" height="1572" alt="CleanShot 2026-04-06 at 17 31 29@2x"
src="https://github.com/user-attachments/assets/5071985d-733f-4e9b-9ff4-38e543521610"
/>

This means that this new indicator _within_ the current channel doesn't
distinguish between unread or mention. This is an intentional
simplification so it represents all new activity.

---------

Co-authored-by: David Battersby <info@davidbattersby.com>
2026-04-07 18:46:17 +04:00

266 lines
8.7 KiB
Ruby
Vendored
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# frozen_string_literal: true
RSpec.describe "Message notifications - mobile", mobile: true do
fab!(:current_user, :user)
let!(:chat_page) { PageObjects::Pages::Chat.new }
let!(:chat_channel_page) { PageObjects::Pages::ChatChannel.new }
let!(:channels_index_page) { PageObjects::Components::Chat::ChannelsIndex.new }
before do
SiteSetting.navigation_menu = "sidebar"
chat_system_bootstrap
end
def create_message(chat_channel, message: nil, user: Fabricate(:user))
Fabricate(:chat_message_with_service, chat_channel:, message:, user:)
end
context "as a user" do
before { sign_in(current_user) }
context "when on homepage" do
context "with public channel" do
fab!(:channel_1, :category_channel)
fab!(:channel_2, :category_channel)
fab!(:user_1, :user)
before { channel_1.add(user_1) }
context "when not member of the channel" do
context "when a message is created" do
it "doesn't show anything" do
visit("/chat")
create_message(channel_1, user: user_1)
expect(page).to have_no_css(".chat-header-icon .chat-channel-unread-indicator")
expect(page).to have_no_css(channels_index_page.channel_row_selector(channel_1))
end
end
end
context "when member of the channel" do
before { channel_1.add(current_user) }
context "when user is in DnD" do
before do
Fabricate(
:do_not_disturb_timing,
user: current_user,
starts_at: 1.week.ago,
ends_at: 1.week.from_now,
)
end
it "doesnt show indicator in header" do
visit("/chat")
create_message(channel_1, user: user_1)
expect(page).to have_css(".do-not-disturb-background")
expect(page).to have_no_css(".chat-header-icon .chat-channel-unread-indicator")
end
end
context "when channel is muted" do
before { channel_1.membership_for(current_user).update!(muted: true) }
context "when a message is created" do
it "doesn't show anything" do
visit("/chat")
create_message(channel_1, user: user_1)
expect(page).to have_no_css(".chat-header-icon .chat-channel-unread-indicator")
expect(channels_index_page).to have_no_unread_channel(channel_1)
end
end
end
context "when a message is created" do
it "correctly renders notifications" do
visit("/chat/channels")
create_message(channel_1, user: user_1)
expect(channels_index_page).to have_unread_channel(channel_1)
expect(chat_page.footer).to have_unread_channels
end
end
context "when a message with mentions is created" do
it "correctly renders notifications" do
Jobs.run_immediately!
visit("/chat/channels")
create_message(
channel_1,
user: user_1,
message: "hello @#{current_user.username} what's up?",
)
expect(channels_index_page).to have_unread_channel(channel_1, count: 1)
expect(chat_page.footer).to have_unread_channels
end
it "shows correct count when there are multiple messages but only 1 is urgent" do
Jobs.run_immediately!
visit("/chat/channels")
create_message(
channel_1,
user: user_1,
message: "Are you busy @#{current_user.username}?",
)
3.times { create_message(channel_1, user: user_1) }
expect(channels_index_page).to have_unread_channel(channel_1, count: 1)
expect(chat_page.footer).to have_unread_channels
end
end
end
end
context "with dm channel" do
fab!(:current_user, :admin)
fab!(:user_1, :user)
fab!(:user_2, :user)
fab!(:dm_channel_1) { Fabricate(:direct_message_channel, users: [current_user, user_1]) }
fab!(:dm_channel_2) { Fabricate(:direct_message_channel, users: [current_user, user_2]) }
context "when a message is created" do
it "correctly renders notifications" do
visit("/chat/direct-messages")
create_message(dm_channel_1, user: user_1)
expect(channels_index_page).to have_unread_channel(dm_channel_1, count: 1, wait: 25)
expect(chat_page.footer).to have_unread_dms("1")
create_message(dm_channel_1, user: user_1)
expect(channels_index_page).to have_unread_channel(dm_channel_1, count: 2, wait: 25)
expect(chat_page.footer).to have_unread_dms("2")
end
it "reorders channels" do
visit("/chat/direct-messages")
expect(page).to have_css(
".chat-channel-row:nth-child(1)[data-chat-channel-id=\"#{dm_channel_1.id}\"]",
)
expect(page).to have_css(
".chat-channel-row:nth-child(2)[data-chat-channel-id=\"#{dm_channel_2.id}\"]",
)
create_message(dm_channel_2, user: user_2)
expect(page).to have_css(
".chat-channel-row:nth-child(1)[data-chat-channel-id=\"#{dm_channel_2.id}\"]",
)
expect(page).to have_css(
".chat-channel-row:nth-child(2)[data-chat-channel-id=\"#{dm_channel_1.id}\"]",
)
end
context "with threads" do
fab!(:message) do
Fabricate(:chat_message, chat_channel: dm_channel_1, user: current_user)
end
fab!(:thread) do
Fabricate(:chat_thread, channel: dm_channel_1, original_message: message)
end
before { dm_channel_1.membership_for(current_user).mark_read!(message.id) }
it "shows urgent badge for mentions" do
Jobs.run_immediately!
visit("/chat/direct-messages")
expect(channels_index_page).to have_no_unread_channel(dm_channel_1)
Fabricate(
:chat_message_with_service,
chat_channel: dm_channel_1,
thread: thread,
message: "hello @#{current_user.username}",
user: user_1,
)
expect(channels_index_page).to have_unread_channel(dm_channel_1, urgent: true)
end
end
end
end
context "with dm and public channel" do
fab!(:current_user, :admin)
fab!(:user_1, :user)
fab!(:channel_1, :category_channel)
fab!(:dm_channel_1) { Fabricate(:direct_message_channel, users: [current_user, user_1]) }
before do
channel_1.add(user_1)
channel_1.add(current_user)
end
context "when messages are created" do
it "correctly renders notifications" do
visit("/chat/channels")
create_message(channel_1, user: user_1)
expect(channels_index_page).to have_unread_channel(channel_1)
visit("/chat/direct-messages")
create_message(dm_channel_1, user: user_1)
expect(channels_index_page).to have_unread_channel(dm_channel_1)
end
end
end
end
context "when viewing a channel" do
fab!(:channel_1, :category_channel)
fab!(:channel_2, :category_channel)
fab!(:user_1, :user)
before do
channel_1.add(current_user)
channel_1.add(user_1)
channel_2.add(current_user)
channel_2.add(user_1)
Jobs.run_immediately!
end
it "shows back button urgent indicator for mentions in other channels" do
chat_page.visit_channel(channel_1)
create_message(channel_2, user: user_1, message: "hey @#{current_user.username}")
expect(page).to have_css(".c-navbar__back-button .chat-channel-unread-indicator.-urgent")
end
it "shows back button unread indicator for messages in other channels" do
chat_page.visit_channel(channel_1)
create_message(channel_2, user: user_1)
expect(page).to have_css(
".c-navbar__back-button .chat-channel-unread-indicator:not(.-urgent)",
)
end
it "does not show back button indicator for current channel unreads" do
create_message(channel_1, user: user_1, message: "hey @#{current_user.username}")
chat_page.visit_channel(channel_1)
expect(page).to have_no_css(".c-navbar__back-button .chat-channel-unread-indicator")
end
end
end
end