From d4cc8830d8daa4fafa56d5fe71cc5a2206f7873b Mon Sep 17 00:00:00 2001 From: alexandre Date: Tue, 2 May 2023 11:27:23 +0200 Subject: [PATCH] refs #4797 added unit test --- .../Worker/WorkerNotificationsManager.spec.js | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 test/vitest/__tests__/pages/Worker/WorkerNotificationsManager.spec.js diff --git a/test/vitest/__tests__/pages/Worker/WorkerNotificationsManager.spec.js b/test/vitest/__tests__/pages/Worker/WorkerNotificationsManager.spec.js new file mode 100644 index 000000000..a917b25e1 --- /dev/null +++ b/test/vitest/__tests__/pages/Worker/WorkerNotificationsManager.spec.js @@ -0,0 +1,94 @@ +import { vi, describe, expect, it, beforeAll, afterEach } from 'vitest'; +import { createWrapper, axios } from 'app/test/vitest/helper'; +import WorkerNotificationsManager from 'src/pages/Worker/Card/WorkerNotificationsManager.vue'; + +describe('WorkerNotificationsManager', () => { + let vm; + const entityId = 1110; + + beforeAll(() => { + vm = createWrapper(WorkerNotificationsManager, { + propsData: { + id: entityId, + }, + }).vm; + }); + + afterEach(() => { + vi.clearAllMocks(); + }); + + describe('fetch()', () => { + it('should fetch the data', async () => { + const subscriptions = [ + { + id: 1, + notification: { + name: 'Mock name', + description: 'Mock description', + }, + notificationFk: 1, + }, + ]; + 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(); + }); + }); + + describe('disableNotification()', () => { + it('should disable the notification', async () => { + vi.spyOn(axios, 'delete').mockResolvedValue({ data: { count: 1 } }); + vi.spyOn(vm.quasar, 'notify'); + const subscriptionId = 1; + vm.notifications = [{ id: 1, active: true }]; + + await vm.disableNotification(vm.notifications[0]); + + expect(axios.delete).toHaveBeenCalledWith( + `NotificationSubscriptions/${subscriptionId}` + ); + expect(vm.notifications[0].id).toBeNull(); + expect(vm.notifications[0].id).toBeFalsy(); + + expect(vm.quasar.notify).toHaveBeenCalledWith( + expect.objectContaining({ type: 'positive' }) + ); + }); + }); + + describe('toggleNotification()', () => { + it('should activate the notification', async () => { + vi.spyOn(axios, 'post').mockResolvedValue({ + data: { id: 1, notificationFk: 1 }, + }); + vm.notifications = [{ id: null, active: true, notificationFk: 1 }]; + + await vm.toggleNotification(vm.notifications[0]); + + expect(axios.post).toHaveBeenCalledWith('NotificationSubscriptions', { + 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' }) + ); + }); + + it('should disable the notification', async () => { + vi.spyOn(vm, 'disableNotification'); + vm.notifications = [{ id: 1, active: false, notificationFk: 1 }]; + + await vm.toggleNotification(vm.notifications[0]); + + expect(vm.notifications[0].id).toBe(null); + expect(vm.notifications[0].active).toBeFalsy(); + }); + }); +});