From 2e79dbc3108cac7d3e13a8ecf0ad571cdbc95b2e Mon Sep 17 00:00:00 2001 From: elweyn Date: Wed, 2 Aug 2023 11:52:16 +0200 Subject: [PATCH] Correct tests. --- .../mentions/extractMentionedUsers.ts | 2 +- .../notifications/notificationsMiddleware.ts | 34 +++++++++++++------ webapp/components/Editor/Editor.spec.js | 9 +++++ 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/backend/src/middleware/notifications/mentions/extractMentionedUsers.ts b/backend/src/middleware/notifications/mentions/extractMentionedUsers.ts index d875c4ed2..d4c112afa 100644 --- a/backend/src/middleware/notifications/mentions/extractMentionedUsers.ts +++ b/backend/src/middleware/notifications/mentions/extractMentionedUsers.ts @@ -17,7 +17,7 @@ export const queryAllUserIds = async (context, offset = -1, pageSize = -1) => { const ids = await writeTxResultPromise return ids } catch (error) { - throw new Error(error) + throw new Error(error.message) } finally { session.close() } diff --git a/backend/src/middleware/notifications/notificationsMiddleware.ts b/backend/src/middleware/notifications/notificationsMiddleware.ts index 8a3c8710c..aa269b1fa 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.ts +++ b/backend/src/middleware/notifications/notificationsMiddleware.ts @@ -120,25 +120,37 @@ const handleContentDataOfPost = async (resolve, root, args, context, resolveInfo const handleContentDataOfComment = async (resolve, root, args, context, resolveInfo) => { const { content } = args - let idsOfUsers = extractMentionedUsers(content) const comment = await resolve(root, args, context, resolveInfo) const [postAuthor] = await postAuthorOfComment(comment.id, { context }) - idsOfUsers = idsOfUsers.filter((id) => id !== postAuthor.id) - if (idsOfUsers.find((id) => id === 'all')) { + const mentionedUserIds = extractMentionedUsers(content).filter((id) => id !== postAuthor.id) + + let notifications + if (mentionedUserIds.includes('all')) { if (context.user.role !== 'admin') { throw new AuthenticationError('You are not allowed to use the "@all" mention!') } - let userToNotify = await queryAllUserIds(context) - userToNotify = userToNotify.filter((id) => id !== postAuthor.id) - await publishNotifications(context, [ - notifyUsersOfMention('Comment', comment.id, userToNotify, 'mentioned_in_comment', context), - ]) + const userToNotify = (await queryAllUserIds(context)).filter((id) => id !== postAuthor.id) + const notification = notifyUsersOfMention( + 'Comment', + comment.id, + userToNotify, + 'mentioned_in_comment', + context, + ) + notifications = [notification] } else { - await publishNotifications(context, [ - notifyUsersOfMention('Comment', comment.id, idsOfUsers, 'mentioned_in_comment', context), + notifications = [ + notifyUsersOfMention( + 'Comment', + comment.id, + mentionedUserIds, + 'mentioned_in_comment', + context, + ), notifyUsersOfComment('Comment', comment.id, postAuthor.id, 'commented_on_post', context), - ]) + ] } + await publishNotifications(context, notifications) return comment } diff --git a/webapp/components/Editor/Editor.spec.js b/webapp/components/Editor/Editor.spec.js index 98c287ced..2507b1e36 100644 --- a/webapp/components/Editor/Editor.spec.js +++ b/webapp/components/Editor/Editor.spec.js @@ -1,5 +1,6 @@ import { mount } from '@vue/test-utils' import Editor from './Editor' +import Vuex from 'vuex' import MutationObserver from 'mutation-observer' import Vue from 'vue' @@ -7,6 +8,7 @@ import Vue from 'vue' global.MutationObserver = MutationObserver const localVue = global.localVue +localVue.use(Vuex) describe('Editor.vue', () => { let wrapper @@ -14,6 +16,12 @@ describe('Editor.vue', () => { let mocks const Wrapper = () => { + const store = new Vuex.Store({ getters: { + 'auth/user': () => { + return { id: 'u343', name: deleteAccountName } + }, + 'auth/isAdmin': () => false, + }}) return (wrapper = mount(Editor, { mocks, propsData, @@ -22,6 +30,7 @@ describe('Editor.vue', () => { stubs: { transition: false, }, + store })) }