From fd456ec55940b4ff9d244cdff8375c383fac0291 Mon Sep 17 00:00:00 2001 From: alexandre Date: Tue, 9 May 2023 11:45:15 +0200 Subject: [PATCH] refs #4797 fix unit test --- .../Card/WorkerNotificationsManager.vue | 97 +++---------------- .../Worker/WorkerNotificationsManager.spec.js | 81 +--------------- 2 files changed, 18 insertions(+), 160 deletions(-) diff --git a/src/pages/Worker/Card/WorkerNotificationsManager.vue b/src/pages/Worker/Card/WorkerNotificationsManager.vue index 511144ff0..7dfd8bf10 100644 --- a/src/pages/Worker/Card/WorkerNotificationsManager.vue +++ b/src/pages/Worker/Card/WorkerNotificationsManager.vue @@ -26,84 +26,10 @@ const notifications = ref([]); async function fetch() { try { await axios - .get(`NotificationSubscriptions`, { - params: { - filter: { - include: [ - { - relation: 'notification', - }, - ], - where: { - userFk: entityId.value, - }, - }, - }, - }) + .get(`NotificationSubscriptions/${entityId.value}/getList`) .then(async (res) => { if (res.data) { - res.data.forEach((subscription) => { - notifications.value.push({ - id: subscription.id, - notificationFk: subscription.notificationFk, - name: subscription.notification.name, - description: subscription.notification.description, - active: true, - }); - }); - } - }); - await axios - .get(`RoleMappings`, { - params: { - filter: { - fields: ['roleId'], - where: { - principalId: entityId.value, - }, - }, - }, - }) - .then(async (res) => { - if (res.data) { - await axios - .get(`NotificationAcls`, { - params: { - filter: { - include: [ - { - relation: 'notification', - }, - ], - where: { - roleFk: { - inq: res.data.map((role) => { - return role.roleId; - }), - }, - }, - }, - }, - }) - .then(async (res) => { - if (res.data) { - res.data.forEach((acl) => { - const activeNotif = notifications.value.find( - (notif) => - notif.notificationFk === acl.notificationFk - ); - if (!activeNotif) { - notifications.value.push({ - id: null, - notificationFk: acl.notificationFk, - name: acl.notification.name, - description: acl.notification.description, - active: false, - }); - } - }); - } - }); + notifications.value = res.data; } }); } catch (e) { @@ -112,14 +38,19 @@ async function fetch() { } async function disableNotification(notification) { - await axios.delete(`NotificationSubscriptions/${notification.id}`).then(() => { - notification.id = null; - notification.active = false; - quasar.notify({ - type: 'positive', - message: t('worker.notificationsManager.unsubscribed'), + await axios + .delete(`NotificationSubscriptions/${notification.id}`) + .catch(() => (notification.active = true)) + .then((res) => { + if (res.data) { + notification.id = null; + notification.active = false; + quasar.notify({ + type: 'positive', + message: t('worker.notificationsManager.unsubscribed'), + }); + } }); - }); } async function toggleNotification(notification) { diff --git a/test/vitest/__tests__/pages/Worker/WorkerNotificationsManager.spec.js b/test/vitest/__tests__/pages/Worker/WorkerNotificationsManager.spec.js index 24140cd1b..744346078 100644 --- a/test/vitest/__tests__/pages/Worker/WorkerNotificationsManager.spec.js +++ b/test/vitest/__tests__/pages/Worker/WorkerNotificationsManager.spec.js @@ -5,7 +5,6 @@ import WorkerNotificationsManager from 'src/pages/Worker/Card/WorkerNotification describe('WorkerNotificationsManager', () => { let vm; const entityId = 1110; - const roleFk = 1; beforeAll(() => { vm = createWrapper(WorkerNotificationsManager, { @@ -26,81 +25,16 @@ describe('WorkerNotificationsManager', () => { data: [ { id: 1, - notification: { - name: 'Name 1', - description: 'Description 1', - }, + name: 'Name 1', + description: 'Description 1', notificationFk: 1, - }, - ], - }) - .mockResolvedValueOnce({ - data: [ - { - roleId: roleFk, - }, - ], - }) - .mockResolvedValueOnce({ - data: [ - { - notification: { - name: 'Name 1', - description: 'Description 1', - }, - notificationFk: 1, - }, - { - notification: { - name: 'Name 2', - description: 'Description 2', - }, - notificationFk: 2, + active: true }, ], }); await vm.fetch(); - expect(axios.get).toHaveBeenCalledWith(`NotificationSubscriptions`, { - params: { - filter: { - include: [ - { - relation: 'notification', - }, - ], - where: { - userFk: entityId, - }, - }, - }, - }); - expect(axios.get).toHaveBeenCalledWith(`RoleMappings`, { - params: { - filter: { - fields: ['roleId'], - where: { - principalId: entityId, - }, - }, - }, - }); - expect(axios.get).toHaveBeenCalledWith(`NotificationAcls`, { - params: { - filter: { - include: [ - { - relation: 'notification', - }, - ], - where: { - roleFk: { - inq: [roleFk], - }, - }, - }, - }, - }); + expect(axios.get).toHaveBeenCalledWith(`NotificationSubscriptions/${entityId}/getList`); expect(vm.notifications).toEqual([ { id: 1, @@ -109,13 +43,6 @@ describe('WorkerNotificationsManager', () => { description: 'Description 1', active: true, }, - { - id: null, - notificationFk: 2, - name: 'Name 2', - description: 'Description 2', - active: false, - }, ]); }); });