From cdc0959135a1bb9c3fd0c4750a9cb6ede5d189cb Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Sun, 19 Nov 2023 17:27:51 +0200 Subject: fix tests --- .../notification_utils/notification_utils.spec.js | 66 ++++++++++------------ 1 file changed, 31 insertions(+), 35 deletions(-) (limited to 'test/unit/specs/services/notification_utils/notification_utils.spec.js') diff --git a/test/unit/specs/services/notification_utils/notification_utils.spec.js b/test/unit/specs/services/notification_utils/notification_utils.spec.js index 00628d83..ba7f7d75 100644 --- a/test/unit/specs/services/notification_utils/notification_utils.spec.js +++ b/test/unit/specs/services/notification_utils/notification_utils.spec.js @@ -5,26 +5,24 @@ describe('NotificationUtils', () => { it('should return sorted notifications with configured types', () => { const store = { state: { - statuses: { - notifications: { - data: [ - { - id: 1, - action: { id: '1' }, - type: 'like' - }, - { - id: 2, - action: { id: '2' }, - type: 'mention' - }, - { - id: 3, - action: { id: '3' }, - type: 'repeat' - } - ] - } + notifications: { + data: [ + { + id: 1, + action: { id: '1' }, + type: 'like' + }, + { + id: 2, + action: { id: '2' }, + type: 'mention' + }, + { + id: 3, + action: { id: '3' }, + type: 'repeat' + } + ] }, config: { notificationVisibility: { @@ -55,21 +53,19 @@ describe('NotificationUtils', () => { it('should return only notifications not marked as seen', () => { const store = { state: { - statuses: { - notifications: { - data: [ - { - action: { id: '1' }, - type: 'like', - seen: false - }, - { - action: { id: '2' }, - type: 'mention', - seen: true - } - ] - } + notifications: { + data: [ + { + action: { id: '1' }, + type: 'like', + seen: false + }, + { + action: { id: '2' }, + type: 'mention', + seen: true + } + ] }, config: { notificationVisibility: { -- cgit v1.2.3-70-g09d2 From c25170d7d9cb35d7209a63628905136e5fcab80a Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Wed, 22 Nov 2023 21:56:48 +0200 Subject: fix tests and make utils consistent in where they pull configuration from --- .../notification_utils/notification_utils.js | 41 +++++++++++++--------- .../notification_utils/notification_utils.spec.js | 12 ++++--- 2 files changed, 32 insertions(+), 21 deletions(-) (limited to 'test/unit/specs/services/notification_utils/notification_utils.spec.js') diff --git a/src/services/notification_utils/notification_utils.js b/src/services/notification_utils/notification_utils.js index 959d28db..5ec98d9f 100644 --- a/src/services/notification_utils/notification_utils.js +++ b/src/services/notification_utils/notification_utils.js @@ -10,18 +10,21 @@ let cachedBadgeUrl = null export const notificationsFromStore = store => store.state.notifications.data export const visibleTypes = store => { - const rootState = store.rootState || store.state + // When called from within a module we need rootGetters to access wider scope + // however when called from a component (i.e. this.$store) we already have wider scope + const rootGetters = store.rootGetters || store.getters + const { notificationVisibility } = rootGetters.mergedConfig return ([ - rootState.config.notificationVisibility.likes && 'like', - rootState.config.notificationVisibility.mentions && 'mention', - rootState.config.notificationVisibility.repeats && 'repeat', - rootState.config.notificationVisibility.follows && 'follow', - rootState.config.notificationVisibility.followRequest && 'follow_request', - rootState.config.notificationVisibility.moves && 'move', - rootState.config.notificationVisibility.emojiReactions && 'pleroma:emoji_reaction', - rootState.config.notificationVisibility.reports && 'pleroma:report', - rootState.config.notificationVisibility.polls && 'poll' + notificationVisibility.likes && 'like', + notificationVisibility.mentions && 'mention', + notificationVisibility.repeats && 'repeat', + notificationVisibility.follows && 'follow', + notificationVisibility.followRequest && 'follow_request', + notificationVisibility.moves && 'move', + notificationVisibility.emojiReactions && 'pleroma:emoji_reaction', + notificationVisibility.reports && 'pleroma:report', + notificationVisibility.polls && 'poll' ].filter(_ => _)) } @@ -54,17 +57,19 @@ const sortById = (a, b) => { const isMutedNotification = (store, notification) => { if (!notification.status) return - return notification.status.muted || muteWordHits(notification.status, store.rootGetters.mergedConfig.muteWords).length > 0 + const rootGetters = store.rootGetters || store.getters + return notification.status.muted || muteWordHits(notification.status, rootGetters.mergedConfig.muteWords).length > 0 } export const maybeShowNotification = (store, notification) => { const rootState = store.rootState || store.state + const rootGetters = store.rootGetters || store.getters if (notification.seen) return if (!visibleTypes(store).includes(notification.type)) return if (notification.type === 'mention' && isMutedNotification(store, notification)) return - const notificationObject = prepareNotificationObject(notification, store.rootGetters.i18n) + const notificationObject = prepareNotificationObject(notification, rootGetters.i18n) showDesktopNotification(rootState, notificationObject) } @@ -78,7 +83,8 @@ export const filteredNotificationsFromStore = (store, types) => { } export const unseenNotificationsFromStore = store => { - const ignoreInactionableSeen = store.getters.mergedConfig.ignoreInactionableSeen + const rootGetters = store.rootGetters || store.getters + const ignoreInactionableSeen = rootGetters.mergedConfig.ignoreInactionableSeen return filteredNotificationsFromStore(store).filter(({ seen, type }) => { if (!ignoreInactionableSeen) return !seen @@ -149,15 +155,16 @@ export const prepareNotificationObject = (notification, i18n) => { } export const countExtraNotifications = (store) => { - const mergedConfig = store.getters.mergedConfig + const rootGetters = store.rootGetters || store.getters + const mergedConfig = rootGetters.mergedConfig if (!mergedConfig.showExtraNotifications) { return 0 } return [ - mergedConfig.showChatsInExtraNotifications ? store.getters.unreadChatCount : 0, - mergedConfig.showAnnouncementsInExtraNotifications ? store.getters.unreadAnnouncementCount : 0, - mergedConfig.showFollowRequestsInExtraNotifications ? store.getters.followRequestCount : 0 + mergedConfig.showChatsInExtraNotifications ? rootGetters.unreadChatCount : 0, + mergedConfig.showAnnouncementsInExtraNotifications ? rootGetters.unreadAnnouncementCount : 0, + mergedConfig.showFollowRequestsInExtraNotifications ? rootGetters.followRequestCount : 0 ].reduce((a, c) => a + c, 0) } diff --git a/test/unit/specs/services/notification_utils/notification_utils.spec.js b/test/unit/specs/services/notification_utils/notification_utils.spec.js index ba7f7d75..60db7380 100644 --- a/test/unit/specs/services/notification_utils/notification_utils.spec.js +++ b/test/unit/specs/services/notification_utils/notification_utils.spec.js @@ -23,8 +23,10 @@ describe('NotificationUtils', () => { type: 'repeat' } ] - }, - config: { + } + }, + getters: { + mergedConfig: { notificationVisibility: { likes: true, repeats: true, @@ -66,8 +68,10 @@ describe('NotificationUtils', () => { seen: true } ] - }, - config: { + } + }, + getters: { + mergedConfig: { notificationVisibility: { likes: true, repeats: true, -- cgit v1.2.3-70-g09d2