From 91eb22ac4932d755c1c6e60cd6606fd9098d4c45 Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Fri, 4 Sep 2026 00:39:50 +0100 Subject: [PATCH] Update audit log tests to utilize new database mock functions for improved accuracy 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. --- .../management/__tests__/auditlogs.test.js | 35 +++++++++---------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/src/services/management/__tests__/auditlogs.test.js b/src/services/management/__tests__/auditlogs.test.js index e62a2f3..72e2781 100644 --- a/src/services/management/__tests__/auditlogs.test.js +++ b/src/services/management/__tests__/auditlogs.test.js @@ -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); }); }); }); -