Tom Butcher 28c94159b4
Some checks failed
farmcontrol/farmcontrol-api/pipeline/head There was a failure building this commit
Added unit tests.
2025-12-29 00:39:13 +00:00

92 lines
2.4 KiB
JavaScript

import { jest } from '@jest/globals';
jest.unstable_mockModule('../../../utils.js', () => ({
getAuditLogs: jest.fn(),
}));
jest.unstable_mockModule('../../../database/database.js', () => ({
getModelStats: jest.fn(),
getModelHistory: jest.fn(),
}));
jest.unstable_mockModule('../../../database/schemas/inventory/stockaudit.schema.js', () => ({
stockAuditModel: {
modelName: 'StockAudit',
aggregate: jest.fn(),
findOne: jest.fn(),
create: jest.fn(),
},
}));
jest.unstable_mockModule('log4js', () => ({
default: {
getLogger: () => ({
level: 'info',
debug: jest.fn(),
error: jest.fn(),
warn: jest.fn(),
trace: jest.fn(),
}),
},
}));
const {
listStockAuditsRouteHandler,
getStockAuditRouteHandler,
newStockAuditRouteHandler,
} = await import('../stockaudits.js');
const { getAuditLogs } = await import('../../../utils.js');
const { stockAuditModel } = await import('../../../database/schemas/inventory/stockaudit.schema.js');
describe('Stock Audit Service Route Handlers', () => {
let req, res;
beforeEach(() => {
req = {
params: {},
query: {},
body: {},
user: { id: 'test-user-id' },
};
res = {
send: jest.fn(),
status: jest.fn().mockReturnThis(),
};
jest.clearAllMocks();
});
describe('listStockAuditsRouteHandler', () => {
it('should list stock audits', async () => {
const mockResult = [{ _id: '1', type: 'full' }];
stockAuditModel.aggregate.mockResolvedValue(mockResult);
await listStockAuditsRouteHandler(req, res);
expect(stockAuditModel.aggregate).toHaveBeenCalled();
expect(res.send).toHaveBeenCalledWith(mockResult);
});
});
describe('getStockAuditRouteHandler', () => {
it('should get a stock audit by ID with audit logs', async () => {
req.params.id = '507f1f77bcf86cd799439011';
const mockAudit = { _id: '507f1f77bcf86cd799439011', type: 'full', _doc: {} };
stockAuditModel.findOne.mockReturnValue({
populate: jest.fn().mockReturnValue({
populate: jest.fn().mockReturnValue({
populate: jest.fn().mockResolvedValue(mockAudit),
}),
}),
});
getAuditLogs.mockResolvedValue([]);
await getStockAuditRouteHandler(req, res);
expect(getAuditLogs).toHaveBeenCalled();
expect(res.send).toHaveBeenCalled();
});
});
});