From 8949248bc48370a7f75ab16a2192fd04f6f55c01 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Tue, 1 Oct 2024 17:15:36 -0400 Subject: [PATCH] Example test Added an example test that queries /admin/notification/all and checks if the response returns a list of notifications. Since we don't have a real database, we overwrite (mock) NotificationModel to just return some fake data, otherwise the test would crash. --- package.json | 1 + server/admin.api.spec.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 server/admin.api.spec.js diff --git a/package.json b/package.json index ff702fda7..50e95f712 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "test:api-unit": "jest \"server/.*.spec.js\" --verbose", "test:api-unit:themes": "jest \"server/.*.spec.js\" -t \"theme bundle\" --verbose", "test:api-unit:css": "jest \"server/.*.spec.js\" -t \"Get CSS\" --verbose", + "test:api-unit:notifications": "jest \"server/.*.spec.js\" -t \"Notifications\" --verbose", "test:coverage": "jest --coverage --silent --runInBand", "test:dev": "jest --verbose --watch", "test:basic": "jest tests/markdown/basic.test.js --verbose", diff --git a/server/admin.api.spec.js b/server/admin.api.spec.js new file mode 100644 index 000000000..a0ad74a5e --- /dev/null +++ b/server/admin.api.spec.js @@ -0,0 +1,30 @@ +const supertest = require('supertest'); + +// Mimic https responses to avoid being redirected all the time +const app = supertest.agent(require('app.js').app) + .set('X-Forwarded-Proto', 'https'); + +const NotificationModel = require('./notifications.model.js').model; + +// Mock the NotificationModel +jest.mock('./notifications.model.js'); + +describe('Tests for admin api', () => { + afterEach(() => { + jest.resetAllMocks(); + }); + + describe('Notifications', () => { + it('should return list of all notifications', async () => { + const fakeNotifications = ["a", "b"]; + NotificationModel.getAll.mockResolvedValue(fakeNotifications); + + const response = await app + .get('/admin/notification/all') + .set('Authorization', 'Basic ' + Buffer.from('admin:password3').toString('base64')); + + expect(response.status).toBe(200); + expect(response.body).toEqual(fakeNotifications); + }); + }); +});