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
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:
parent
605d845ba3
commit
91eb22ac49
@ -3,6 +3,8 @@ import { jest } from '@jest/globals';
|
||||
jest.unstable_mockModule('../../../database/database.js', () => ({
|
||||
searchObjects: jest.fn(),
|
||||
getPropertyValues: jest.fn(),
|
||||
listObjects: jest.fn(),
|
||||
getObject: jest.fn(),
|
||||
getModelStats: jest.fn(),
|
||||
getModelHistory: jest.fn(),
|
||||
getObjectNeighbors: jest.fn(),
|
||||
@ -11,8 +13,6 @@ jest.unstable_mockModule('../../../database/database.js', () => ({
|
||||
jest.unstable_mockModule('../../../database/schemas/management/auditlog.schema.js', () => ({
|
||||
auditLogModel: {
|
||||
modelName: 'AuditLog',
|
||||
find: jest.fn(),
|
||||
findOne: jest.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
@ -30,6 +30,7 @@ jest.unstable_mockModule('log4js', () => ({
|
||||
|
||||
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');
|
||||
|
||||
describe('Audit Log Service Route Handlers', () => {
|
||||
@ -54,17 +55,14 @@ describe('Audit Log Service Route Handlers', () => {
|
||||
const mockResult = [
|
||||
{ _id: '1', operation: 'edit', parent: 'parent123', _doc: { parent: 'parent123' } },
|
||||
];
|
||||
auditLogModel.find.mockReturnValue({
|
||||
sort: jest.fn().mockReturnThis(),
|
||||
skip: jest.fn().mockReturnThis(),
|
||||
limit: jest.fn().mockReturnThis(),
|
||||
populate: jest.fn().mockResolvedValue(mockResult),
|
||||
});
|
||||
listObjects.mockResolvedValue(mockResult);
|
||||
|
||||
await listAuditLogsRouteHandler(req, res);
|
||||
|
||||
expect(auditLogModel.find).toHaveBeenCalled();
|
||||
expect(res.send).toHaveBeenCalled();
|
||||
expect(listObjects).toHaveBeenCalledWith(
|
||||
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 () => {
|
||||
req.params.id = '507f1f77bcf86cd799439011';
|
||||
const mockLog = { _id: '507f1f77bcf86cd799439011', operation: 'edit' };
|
||||
auditLogModel.findOne.mockReturnValue({
|
||||
populate: jest.fn().mockReturnValue({
|
||||
populate: jest.fn().mockReturnValue({
|
||||
populate: jest.fn().mockResolvedValue(mockLog),
|
||||
}),
|
||||
}),
|
||||
});
|
||||
getObject.mockResolvedValue(mockLog);
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user