mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-03 14:52:38 +00:00
Add test for "add notification"
This commit is contained in:
@@ -6,18 +6,18 @@ const app = supertest.agent(require('app.js').app)
|
|||||||
|
|
||||||
const NotificationModel = require('./notifications.model.js').model;
|
const NotificationModel = require('./notifications.model.js').model;
|
||||||
|
|
||||||
// Mock the NotificationModel
|
|
||||||
jest.mock('./notifications.model.js');
|
|
||||||
|
|
||||||
describe('Tests for admin api', () => {
|
describe('Tests for admin api', () => {
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
jest.resetAllMocks();
|
jest.resetAllMocks();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Notifications', () => {
|
describe('Notifications', () => {
|
||||||
it('should return list of all notifications', async () => {
|
it('should return list of all notifications', async () => {
|
||||||
const fakeNotifications = ["a", "b"];
|
const fakeNotifications = ["a", "b"];
|
||||||
NotificationModel.getAll.mockResolvedValue(fakeNotifications);
|
|
||||||
|
// Change 'getAll' function to just return the above array instead of actually using the database
|
||||||
|
jest.spyOn(NotificationModel, 'getAll')
|
||||||
|
.mockImplementationOnce(() => fakeNotifications);
|
||||||
|
|
||||||
const response = await app
|
const response = await app
|
||||||
.get('/admin/notification/all')
|
.get('/admin/notification/all')
|
||||||
@@ -26,5 +26,37 @@ describe('Tests for admin api', () => {
|
|||||||
expect(response.status).toBe(200);
|
expect(response.status).toBe(200);
|
||||||
expect(response.body).toEqual(fakeNotifications);
|
expect(response.body).toEqual(fakeNotifications);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should add a new notification', async () => {
|
||||||
|
const inputNotification = {
|
||||||
|
title : 'Test Notification',
|
||||||
|
text : 'This is a test notification',
|
||||||
|
startAt : new Date(),
|
||||||
|
stopAt : new Date(),
|
||||||
|
dismissKey : 'testKey'
|
||||||
|
};
|
||||||
|
|
||||||
|
const savedNotification = {
|
||||||
|
...inputNotification,
|
||||||
|
_id : expect.any(String), // Don't care what _id is, just that it added one
|
||||||
|
createdAt : expect.any(String), // Don't care what date is, just that it added it
|
||||||
|
startAt : inputNotification.startAt.toJSON(), // Convert to json to match the format coming from mongo
|
||||||
|
stopAt : inputNotification.stopAt.toJSON()
|
||||||
|
};
|
||||||
|
|
||||||
|
//Change 'save' function to just return itself instead of actually interacting with the database
|
||||||
|
jest.spyOn(NotificationModel.prototype, 'save')
|
||||||
|
.mockImplementationOnce(function() {
|
||||||
|
return Promise.resolve(this);
|
||||||
|
});
|
||||||
|
|
||||||
|
const response = await app
|
||||||
|
.post('/admin/notification/add')
|
||||||
|
.set('Authorization', 'Basic ' + Buffer.from('admin:password3').toString('base64'))
|
||||||
|
.send(inputNotification);
|
||||||
|
|
||||||
|
expect(response.status).toBe(201);
|
||||||
|
expect(response.body).toEqual(savedNotification);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user