mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-19 22:52:49 +00:00
Add test for adding notification without dismissKey
This commit is contained in:
@@ -12,17 +12,19 @@ describe('Tests for admin api', ()=>{
|
|||||||
|
|
||||||
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 testNotifications = ['a', 'b'];
|
||||||
|
|
||||||
jest.spyOn(NotificationModel, 'getAll')
|
jest.spyOn(NotificationModel, 'find')
|
||||||
.mockImplementationOnce(()=>fakeNotifications);
|
.mockImplementationOnce(() => {
|
||||||
|
return { exec: jest.fn().mockResolvedValue(testNotifications) };
|
||||||
|
});
|
||||||
|
|
||||||
const response = await app
|
const response = await app
|
||||||
.get('/admin/notification/all')
|
.get('/admin/notification/all')
|
||||||
.set('Authorization', `Basic ${Buffer.from('admin:password3').toString('base64')}`);
|
.set('Authorization', `Basic ${Buffer.from('admin:password3').toString('base64')}`);
|
||||||
|
|
||||||
expect(response.status).toBe(200);
|
expect(response.status).toBe(200);
|
||||||
expect(response.body).toEqual(fakeNotifications);
|
expect(response.body).toEqual(testNotifications);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should add a new notification', async ()=>{
|
it('should add a new notification', async ()=>{
|
||||||
@@ -32,7 +34,7 @@ describe('Tests for admin api', ()=>{
|
|||||||
startAt : new Date().toISOString(),
|
startAt : new Date().toISOString(),
|
||||||
stopAt : new Date().toISOString(),
|
stopAt : new Date().toISOString(),
|
||||||
dismissKey : 'testKey'
|
dismissKey : 'testKey'
|
||||||
};
|
};
|
||||||
|
|
||||||
const savedNotification = {
|
const savedNotification = {
|
||||||
...inputNotification,
|
...inputNotification,
|
||||||
@@ -56,19 +58,43 @@ describe('Tests for admin api', ()=>{
|
|||||||
expect(response.body).toEqual(savedNotification);
|
expect(response.body).toEqual(savedNotification);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should fail adding a new notification without dismissKey', async () => {
|
||||||
|
const inputNotification = {
|
||||||
|
title : 'Test Notification',
|
||||||
|
text : 'This is a test notification',
|
||||||
|
startAt : new Date().toISOString(),
|
||||||
|
stopAt : new Date().toISOString()
|
||||||
|
};
|
||||||
|
|
||||||
|
//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(500);
|
||||||
|
expect(response.body).toEqual({ message: 'Dismiss key is required!' });
|
||||||
|
});
|
||||||
|
|
||||||
it('should delete a notification based on its dismiss key', async ()=>{
|
it('should delete a notification based on its dismiss key', async ()=>{
|
||||||
const dismissKey = 'testKey';
|
const dismissKey = 'testKey';
|
||||||
jest.spyOn(NotificationModel, 'deleteNotification')
|
|
||||||
.mockImplementationOnce(async (key)=>{
|
jest.spyOn(NotificationModel, 'findOneAndDelete')
|
||||||
expect(key).toBe(dismissKey);
|
.mockImplementationOnce((key) => {
|
||||||
return { message: 'Notification deleted successfully' };
|
return { exec: jest.fn().mockResolvedValue(key) };
|
||||||
});
|
});
|
||||||
const response = await app
|
const response = await app
|
||||||
.delete(`/admin/notification/delete/${dismissKey}`)
|
.delete(`/admin/notification/delete/${dismissKey}`)
|
||||||
.set('Authorization', `Basic ${Buffer.from('admin:password3').toString('base64')}`);
|
.set('Authorization', `Basic ${Buffer.from('admin:password3').toString('base64')}`);
|
||||||
|
|
||||||
|
expect(NotificationModel.findOneAndDelete).toHaveBeenCalledWith({'dismissKey': 'testKey'});
|
||||||
expect(response.status).toBe(200);
|
expect(response.status).toBe(200);
|
||||||
expect(response.body).toEqual({ message: 'Notification deleted successfully' });
|
expect(response.body).toEqual({ dismissKey: 'testKey' });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user