Skip to content

Commit

Permalink
created tests for sending notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
caiodasilva2005 committed Nov 27, 2024
1 parent 45d0574 commit 598d8fe
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 29 deletions.
7 changes: 4 additions & 3 deletions src/backend/src/utils/homepage-notifications.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ export const sendNotificationToUsers = async (userIds: string[], text: string, i
data: {
text,
iconName
}
},
...getNotificationQueryArgs(organizationId)
});

const notificationPromises = userIds.map(async (userId) => {
return sendNotificationToUser(userId, createdNotification.notificationId, organizationId);
});

const resolvedNotifications = await Promise.all(notificationPromises);
return resolvedNotifications.flat();
await Promise.all(notificationPromises);
return notificationTransformer(createdNotification);
};
49 changes: 49 additions & 0 deletions src/backend/tests/unmocked/notifications.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Organization } from '@prisma/client';
import { createTestOrganization, createTestUser, resetUsers } from '../test-utils';
import { batmanAppAdmin, wonderwomanGuest } from '../test-data/users.test-data';
import { NotFoundException } from '../../src/utils/errors.utils';
import { sendNotificationToUsers } from '../../src/utils/homepage-notifications.utils';
import prisma from '../../src/prisma/prisma';

describe('Notification Tests', () => {
let orgId: string;
let organization: Organization;
beforeEach(async () => {
organization = await createTestOrganization();
orgId = organization.organizationId;
});

afterEach(async () => {
await resetUsers();
});

describe('Send Notification', () => {
it('fails on invalid user id', async () => {
await expect(async () => await sendNotificationToUsers(['1'], 'test', 'test', orgId)).rejects.toThrow(
new NotFoundException('User', '1')
);
});

it('Succeeds and sends notification to user', async () => {
const testBatman = await createTestUser(batmanAppAdmin, orgId);
const testWonderWoman = await createTestUser(wonderwomanGuest, orgId);

const notification = await sendNotificationToUsers([testBatman.userId, testWonderWoman.userId], 'test', 'icon', orgId);

const batmanWithNotifications = await prisma.user.findUnique({
where: { userId: testBatman.userId },
include: { unreadNotifications: true }
});
const wonderWomanWithNotifications = await prisma.user.findUnique({
where: { userId: testWonderWoman.userId },
include: { unreadNotifications: true }
});

expect(batmanWithNotifications?.unreadNotifications).toHaveLength(1);
expect(batmanWithNotifications?.unreadNotifications[0]).toStrictEqual(notification);

expect(wonderWomanWithNotifications?.unreadNotifications).toHaveLength(1);
expect(wonderWomanWithNotifications?.unreadNotifications[0]).toStrictEqual(notification);
});
});
});
26 changes: 0 additions & 26 deletions src/backend/tests/unmocked/users.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { createTestOrganization, createTestTask, createTestUser, resetUsers } fr
import { batmanAppAdmin } from '../test-data/users.test-data';
import UsersService from '../../src/services/users.services';
import { NotFoundException } from '../../src/utils/errors.utils';
import prisma from '../../src/prisma/prisma';

describe('User Tests', () => {
let orgId: string;
Expand Down Expand Up @@ -49,29 +48,4 @@ describe('User Tests', () => {
expect(userTasks).toStrictEqual([batmanTask, batmanTask]);
});
});

/*
describe('Send Notification', () => {
it('fails on invalid user id', async () => {
await expect(async () => await UsersService.sendNotification('1', 'test', 'test')).rejects.toThrow(
new NotFoundException('User', '1')
);
});
it('Succeeds and sends notification to user', async () => {
const testBatman = await createTestUser(batmanAppAdmin, orgId);
await UsersService.sendNotification(testBatman.userId, 'test1', 'test1');
await UsersService.sendNotification(testBatman.userId, 'test2', 'test2');
const batmanWithNotifications = await prisma.user.findUnique({
where: { userId: testBatman.userId },
include: { unreadNotifications: true }
});
expect(batmanWithNotifications?.unreadNotifications).toHaveLength(2);
expect(batmanWithNotifications?.unreadNotifications[0].text).toBe('test1');
expect(batmanWithNotifications?.unreadNotifications[1].text).toBe('test2');
});
});
*/
});

0 comments on commit 598d8fe

Please sign in to comment.