0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-24 12:02:48 +00:00

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.
This commit is contained in:
Trevor Buckner
2024-10-01 17:15:36 -04:00
parent fabc0bea83
commit 8949248bc4
2 changed files with 31 additions and 0 deletions

View File

@@ -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",

30
server/admin.api.spec.js Normal file
View File

@@ -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);
});
});
});