From 82e805eebf64ab2fce8a93ea8c6bbd618da4fde7 Mon Sep 17 00:00:00 2001 From: alexandre Date: Tue, 2 May 2023 14:01:41 +0200 Subject: [PATCH] refs #4797 unit test refactored --- .../Worker/WorkerNotificationsManager.spec.js | 109 +++++++++++++++--- 1 file changed, 94 insertions(+), 15 deletions(-) diff --git a/test/vitest/__tests__/pages/Worker/WorkerNotificationsManager.spec.js b/test/vitest/__tests__/pages/Worker/WorkerNotificationsManager.spec.js index a917b25e1f..24140cd1b0 100644 --- a/test/vitest/__tests__/pages/Worker/WorkerNotificationsManager.spec.js +++ b/test/vitest/__tests__/pages/Worker/WorkerNotificationsManager.spec.js @@ -5,6 +5,7 @@ import WorkerNotificationsManager from 'src/pages/Worker/Card/WorkerNotification describe('WorkerNotificationsManager', () => { let vm; const entityId = 1110; + const roleFk = 1; beforeAll(() => { vm = createWrapper(WorkerNotificationsManager, { @@ -19,22 +20,103 @@ describe('WorkerNotificationsManager', () => { }); describe('fetch()', () => { - it('should fetch the data', async () => { - const subscriptions = [ + it('should fetch notification subscriptions and role mappings', async () => { + vi.spyOn(axios, 'get') + .mockResolvedValueOnce({ + data: [ + { + id: 1, + notification: { + 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, + }, + ], + }); + 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(vm.notifications).toEqual([ { id: 1, - notification: { - name: 'Mock name', - description: 'Mock description', - }, notificationFk: 1, + name: 'Name 1', + description: 'Description 1', + active: true, }, - ]; - vi.spyOn(axios, 'get').mockResolvedValue({ data: subscriptions }); - await vm.fetch(); - expect(axios.get).toHaveBeenCalledTimes(3) - expect(vm.notifications.length).toEqual(1); - expect(vm.notifications[0].active).toBeTruthy(); + { + id: null, + notificationFk: 2, + name: 'Name 2', + description: 'Description 2', + active: false, + }, + ]); }); }); @@ -52,7 +134,6 @@ describe('WorkerNotificationsManager', () => { ); expect(vm.notifications[0].id).toBeNull(); expect(vm.notifications[0].id).toBeFalsy(); - expect(vm.quasar.notify).toHaveBeenCalledWith( expect.objectContaining({ type: 'positive' }) ); @@ -72,10 +153,8 @@ describe('WorkerNotificationsManager', () => { notificationFk: 1, userFk: entityId, }); - expect(vm.notifications[0].id).toBe(1); expect(vm.notifications[0].active).toBeTruthy(); - expect(vm.quasar.notify).toHaveBeenCalledWith( expect.objectContaining({ type: 'positive' }) );