forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotificationRepository.test.js
More file actions
49 lines (41 loc) · 2.15 KB
/
Copy pathnotificationRepository.test.js
File metadata and controls
49 lines (41 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
jest.mock('../db/index', () => ({ query: jest.fn() }));
const { query } = require('../db/index');
const notificationRepo = require('../db/notificationRepository');
const { NotificationFactory, UserFactory } = require('./fixtures');
describe('notificationRepository', () => {
beforeEach(() => {
jest.clearAllMocks();
});
test('createNotification stores the record and returns it', async () => {
const user = UserFactory.build();
const notification = NotificationFactory.build({ user_id: user.id, type: 'alert', message: 'Hello' });
query.mockResolvedValue({ rows: [notification] });
const row = await notificationRepo.createNotification({
userId: user.id,
type: notification.type,
title: notification.title,
message: notification.message,
payload: notification.payload,
});
expect(row).toEqual(notification);
expect(query).toHaveBeenCalledWith(
expect.stringContaining('INSERT INTO notifications'),
[user.id, notification.type, notification.title, notification.message, JSON.stringify(notification.payload)]
);
});
test('getNotificationsForUser returns data and pagination metadata', async () => {
const notifications = NotificationFactory.buildList(2);
query
.mockResolvedValueOnce({ rows: [{ total: '2' }] })
.mockResolvedValueOnce({ rows: notifications });
const result = await notificationRepo.getNotificationsForUser(1, { page: 1, limit: 2 });
expect(result).toEqual({ data: notifications, total: 2, page: 1, limit: 2 });
});
test('markNotificationAsRead updates the row', async () => {
const notification = NotificationFactory.build({ is_read: true });
query.mockResolvedValue({ rows: [notification] });
const updated = await notificationRepo.markNotificationAsRead(notification.id);
expect(updated).toEqual(notification);
expect(query).toHaveBeenCalledWith(expect.stringContaining('UPDATE notifications'), [notification.id]);
});
});