Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix notifications hook #86

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions src/test/notificationsController.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import request from 'supertest';
import { app } from '../server';
import Notification from '../database/models/notification';
import { sendInternalErrorResponse } from '../validations';

jest.mock('../database/models/notification');
jest.mock('../validations');
jest.mock('../logs/config');

describe('Notification Controller', () => {
afterEach(() => {
jest.clearAllMocks();
});

describe('getNotifications', () => {
it('should return 200 and notifications for a valid userId', async () => {
const userId = 'test-user-id';
const notifications = [
{ id: '1', userId, message: 'Test message 1', isRead: false },
{ id: '2', userId, message: 'Test message 2', isRead: true },
];

(Notification.findAll as jest.Mock).mockResolvedValue(notifications);

const response = await request(app).get(`/api/notifications/${userId}`);

expect(response.status).toBe(200);
expect(response.body).toEqual({ ok: true, data: notifications });
});

it('should return 404 if no notifications are found', async () => {
const userId = 'test-user-id';

(Notification.findAll as jest.Mock).mockResolvedValue([]);

const response = await request(app).get(`/api/notifications/${userId}`);

expect(response.status).toBe(404);
expect(response.body).toEqual({
ok: false,
message: 'No notifications that were found',
});
});

it('should return 500 on internal server error', async () => {
const userId = 'test-user-id';

(Notification.findAll as jest.Mock).mockRejectedValue(new Error('Internal Error'));

const response = await request(app).get(`/api/notifications/${userId}`);

expect(response.status).toBe(500);
expect(sendInternalErrorResponse).toHaveBeenCalled();
}, 10000);
});

describe('markNotificationAsRead', () => {
it('should return 200 and update the notification', async () => {
const notification = {
id: '1',
userId: 'test-user-id',
message: 'Test message',
isRead: false,
save: jest.fn().mockResolvedValue(true),
};

(Notification.findByPk as jest.Mock).mockResolvedValue(notification);

const response = await request(app).patch(`/api/notifications/${notification.id}`).send({ isRead: true });

expect(response.status).toBe(200);
expect(response.body).toEqual({
ok: true,
message: 'Notification were updated successfully',
});
expect(notification.isRead).toBe(true);
expect(notification.save).toHaveBeenCalled();
});

it('should return 401 if notification is not found', async () => {
const id = 'non-existent-id';

(Notification.findByPk as jest.Mock).mockResolvedValue(null);

const response = await request(app).patch(`/api/notifications/${id}`).send({ isRead: true });

expect(response.status).toBe(401);
expect(response.body).toEqual({
ok: false,
errorMessage: 'No such notification that were found! Try again',
});
});

it('should return 500 on internal server error', async () => {
const id = '1';

(Notification.findByPk as jest.Mock).mockRejectedValue(new Error('Internal Error'));

const response = await request(app).patch(`/api/notifications/${id}`).send({ isRead: true });

expect(response.status).toBe(500);
expect(sendInternalErrorResponse).toHaveBeenCalled();
}, 10000); // Extend timeout to 10 seconds
});
});
Loading