diff --git a/app/javascript/better_together/notifications.js b/app/javascript/better_together/notifications.js index a89b45071..78809a1e9 100644 --- a/app/javascript/better_together/notifications.js +++ b/app/javascript/better_together/notifications.js @@ -118,6 +118,13 @@ function updateUnreadNotifications(count) { } } +// Initialize unread notification display on page load +document.addEventListener('turbo:load', () => { + const badge = document.getElementById('person_notification_count'); + const count = badge ? parseInt(badge.textContent, 10) : 0; + updateUnreadNotifications(count); +}); + export { displayFlashMessage, updateUnreadNotifications diff --git a/spec/factories/noticed/events.rb b/spec/factories/noticed/events.rb new file mode 100644 index 000000000..c7e1514f1 --- /dev/null +++ b/spec/factories/noticed/events.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module Noticed + FactoryBot.define do + factory :noticed_event, class: 'Noticed::Event' do + type { 'BetterTogether::NewMessageNotifier' } + params { {} } + end + end +end diff --git a/spec/factories/noticed/notifications.rb b/spec/factories/noticed/notifications.rb new file mode 100644 index 000000000..555401de2 --- /dev/null +++ b/spec/factories/noticed/notifications.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module Noticed + FactoryBot.define do + factory :noticed_notification, class: 'Noticed::Notification', aliases: %i[notification] do + type { 'BetterTogether::NewMessageNotifier' } + event { association :noticed_event, type: type } + recipient { association(:better_together_person) } + end + end +end diff --git a/spec/features/notifications/unread_badge_spec.rb b/spec/features/notifications/unread_badge_spec.rb index fd17c43db..67d1abd8e 100644 --- a/spec/features/notifications/unread_badge_spec.rb +++ b/spec/features/notifications/unread_badge_spec.rb @@ -44,4 +44,14 @@ expect(page).to have_title(original_title) end end + + it 'shows unread status in title and favicon on initial load', :js do + person = BetterTogether::User.find_by(email: 'manager@example.test').person + create(:noticed_notification, recipient: person) + + visit conversations_path(locale: I18n.default_locale) + + expect(page).to have_title(/^\(1\)/) + expect(page).to have_css("link[rel~='icon'][href^='data:image']", visible: false) + end end