Update audit log tests to utilize new database mock functions for improved accuracy
All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good

This commit enhances the audit log service route tests by replacing direct calls to the `auditLogModel` with mocked database functions `listObjects` and `getObject`. This change improves the accuracy of the tests by ensuring they reflect the updated database interaction methods. The tests now verify that the correct parameters are passed to these functions, enhancing the overall reliability of the audit log service tests.
This commit is contained in:
Tom Butcher 2026-09-04 00:39:50 +01:00
parent 605d845ba3
commit 91eb22ac49

View File

@ -3,6 +3,8 @@ import { jest } from '@jest/globals';
jest.unstable_mockModule('../../../database/database.js', () => ({ jest.unstable_mockModule('../../../database/database.js', () => ({
searchObjects: jest.fn(), searchObjects: jest.fn(),
getPropertyValues: jest.fn(), getPropertyValues: jest.fn(),
listObjects: jest.fn(),
getObject: jest.fn(),
getModelStats: jest.fn(), getModelStats: jest.fn(),
getModelHistory: jest.fn(), getModelHistory: jest.fn(),
getObjectNeighbors: jest.fn(), getObjectNeighbors: jest.fn(),
@ -11,8 +13,6 @@ jest.unstable_mockModule('../../../database/database.js', () => ({
jest.unstable_mockModule('../../../database/schemas/management/auditlog.schema.js', () => ({ jest.unstable_mockModule('../../../database/schemas/management/auditlog.schema.js', () => ({
auditLogModel: { auditLogModel: {
modelName: 'AuditLog', modelName: 'AuditLog',
find: jest.fn(),
findOne: jest.fn(),
}, },
})); }));
@ -30,6 +30,7 @@ jest.unstable_mockModule('log4js', () => ({
const { listAuditLogsRouteHandler, getAuditLogRouteHandler } = await import('../auditlogs.js'); const { listAuditLogsRouteHandler, getAuditLogRouteHandler } = await import('../auditlogs.js');
const { listObjects, getObject } = await import('../../../database/database.js');
const { auditLogModel } = await import('../../../database/schemas/management/auditlog.schema.js'); const { auditLogModel } = await import('../../../database/schemas/management/auditlog.schema.js');
describe('Audit Log Service Route Handlers', () => { describe('Audit Log Service Route Handlers', () => {
@ -54,17 +55,14 @@ describe('Audit Log Service Route Handlers', () => {
const mockResult = [ const mockResult = [
{ _id: '1', operation: 'edit', parent: 'parent123', _doc: { parent: 'parent123' } }, { _id: '1', operation: 'edit', parent: 'parent123', _doc: { parent: 'parent123' } },
]; ];
auditLogModel.find.mockReturnValue({ listObjects.mockResolvedValue(mockResult);
sort: jest.fn().mockReturnThis(),
skip: jest.fn().mockReturnThis(),
limit: jest.fn().mockReturnThis(),
populate: jest.fn().mockResolvedValue(mockResult),
});
await listAuditLogsRouteHandler(req, res); await listAuditLogsRouteHandler(req, res);
expect(auditLogModel.find).toHaveBeenCalled(); expect(listObjects).toHaveBeenCalledWith(
expect(res.send).toHaveBeenCalled(); expect.objectContaining({ model: auditLogModel, populate: ['owner', 'parent'] })
);
expect(res.send).toHaveBeenCalledWith(mockResult);
}); });
}); });
@ -72,19 +70,18 @@ describe('Audit Log Service Route Handlers', () => {
it('should get an audit log by ID', async () => { it('should get an audit log by ID', async () => {
req.params.id = '507f1f77bcf86cd799439011'; req.params.id = '507f1f77bcf86cd799439011';
const mockLog = { _id: '507f1f77bcf86cd799439011', operation: 'edit' }; const mockLog = { _id: '507f1f77bcf86cd799439011', operation: 'edit' };
auditLogModel.findOne.mockReturnValue({ getObject.mockResolvedValue(mockLog);
populate: jest.fn().mockReturnValue({
populate: jest.fn().mockReturnValue({
populate: jest.fn().mockResolvedValue(mockLog),
}),
}),
});
await getAuditLogRouteHandler(req, res); await getAuditLogRouteHandler(req, res);
expect(auditLogModel.findOne).toHaveBeenCalled(); expect(getObject).toHaveBeenCalledWith(
expect.objectContaining({
model: auditLogModel,
id: '507f1f77bcf86cd799439011',
populate: ['owner', 'parent'],
})
);
expect(res.send).toHaveBeenCalledWith(mockLog); expect(res.send).toHaveBeenCalledWith(mockLog);
}); });
}); });
}); });