From a9aab5bb0c4ba3e4f46f9ea8f826c7103f13bb80 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Wed, 9 Oct 2024 14:52:56 -0400 Subject: [PATCH] Add test for error handling deleting notifications --- server/admin.api.spec.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/server/admin.api.spec.js b/server/admin.api.spec.js index 20b09b699..b0dbd5d84 100644 --- a/server/admin.api.spec.js +++ b/server/admin.api.spec.js @@ -58,7 +58,7 @@ describe('Tests for admin api', ()=>{ expect(response.body).toEqual(savedNotification); }); - it('should fail adding a new notification without dismissKey', async () => { + it('should handle error adding a notification without dismissKey', async () => { const inputNotification = { title : 'Test Notification', text : 'This is a test notification', @@ -96,5 +96,21 @@ describe('Tests for admin api', ()=>{ expect(response.status).toBe(200); expect(response.body).toEqual({ dismissKey: 'testKey' }); }); + + it('should handle error deleting a notification that doesnt exist', async ()=>{ + const dismissKey = 'testKey'; + + jest.spyOn(NotificationModel, 'findOneAndDelete') + .mockImplementationOnce(() => { + return { exec: jest.fn().mockResolvedValue() }; + }); + const response = await app + .delete(`/admin/notification/delete/${dismissKey}`) + .set('Authorization', `Basic ${Buffer.from('admin:password3').toString('base64')}`); + + expect(NotificationModel.findOneAndDelete).toHaveBeenCalledWith({'dismissKey': 'testKey'}); + expect(response.status).toBe(500); + expect(response.body).toEqual({ message: 'Notification not found' }); + }); }); });