|
| 1 | +import '../../src/env-test'; |
| 2 | + |
| 3 | +jest.mock('../../src/utils/personalNotifications', () => ({ |
| 4 | + __esModule: true, |
| 5 | + default: jest.fn().mockResolvedValue(undefined), |
| 6 | +})); |
| 7 | + |
| 8 | +import sendPersonalNotification from '../../src/utils/personalNotifications'; |
| 9 | +// eslint-disable-next-line @typescript-eslint/no-var-requires |
| 10 | +const { fireAndForgetAssigneeNotifications } = require('../../src/resolvers/helpers/bulkEvents') as { |
| 11 | + fireAndForgetAssigneeNotifications: (args: { |
| 12 | + assigneeData: Record<string, unknown> | null; |
| 13 | + eventIds: string[]; |
| 14 | + projectId: string; |
| 15 | + assigneeId: string; |
| 16 | + whoAssignedId: string; |
| 17 | + }) => void; |
| 18 | +}; |
| 19 | + |
| 20 | +describe('fireAndForgetAssigneeNotifications', () => { |
| 21 | + let consoleErrorSpy: jest.SpyInstance; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + jest.clearAllMocks(); |
| 25 | + consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); |
| 26 | + }); |
| 27 | + |
| 28 | + afterEach(() => { |
| 29 | + consoleErrorSpy.mockRestore(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should enqueue personal notification for each event id', async () => { |
| 33 | + fireAndForgetAssigneeNotifications({ |
| 34 | + assigneeData: { id: 'assignee-1', email: 'assignee@hawk.so' }, |
| 35 | + eventIds: [ 'e-1', 'e-2' ], |
| 36 | + projectId: 'p-1', |
| 37 | + assigneeId: 'assignee-1', |
| 38 | + whoAssignedId: 'u-1', |
| 39 | + }); |
| 40 | + |
| 41 | + await Promise.resolve(); |
| 42 | + |
| 43 | + expect(sendPersonalNotification).toHaveBeenCalledTimes(2); |
| 44 | + expect(sendPersonalNotification).toHaveBeenNthCalledWith( |
| 45 | + 1, |
| 46 | + expect.objectContaining({ id: 'assignee-1' }), |
| 47 | + { |
| 48 | + type: 'assignee', |
| 49 | + payload: { |
| 50 | + assigneeId: 'assignee-1', |
| 51 | + projectId: 'p-1', |
| 52 | + whoAssignedId: 'u-1', |
| 53 | + eventId: 'e-1', |
| 54 | + }, |
| 55 | + } |
| 56 | + ); |
| 57 | + expect(sendPersonalNotification).toHaveBeenNthCalledWith( |
| 58 | + 2, |
| 59 | + expect.objectContaining({ id: 'assignee-1' }), |
| 60 | + { |
| 61 | + type: 'assignee', |
| 62 | + payload: { |
| 63 | + assigneeId: 'assignee-1', |
| 64 | + projectId: 'p-1', |
| 65 | + whoAssignedId: 'u-1', |
| 66 | + eventId: 'e-2', |
| 67 | + }, |
| 68 | + } |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should not call personal notifications when assignee data is empty', () => { |
| 73 | + fireAndForgetAssigneeNotifications({ |
| 74 | + assigneeData: null, |
| 75 | + eventIds: [ 'e-1' ], |
| 76 | + projectId: 'p-1', |
| 77 | + assigneeId: 'assignee-1', |
| 78 | + whoAssignedId: 'u-1', |
| 79 | + }); |
| 80 | + |
| 81 | + expect(sendPersonalNotification).not.toHaveBeenCalled(); |
| 82 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 83 | + 'Failed to enqueue assignee notifications: assignee data is empty' |
| 84 | + ); |
| 85 | + }); |
| 86 | +}); |
0 commit comments