All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good
102 lines
2.7 KiB
JavaScript
102 lines
2.7 KiB
JavaScript
import { jest } from '@jest/globals';
|
|
|
|
jest.unstable_mockModule('../../../database/database.js', () => ({
|
|
searchObjects: jest.fn(),
|
|
getPropertyValues: jest.fn(),
|
|
listObjects: jest.fn(),
|
|
getObject: jest.fn(),
|
|
editObject: jest.fn(),
|
|
newObject: jest.fn(),
|
|
deleteObject: jest.fn(),
|
|
listObjectsByProperties: jest.fn(),
|
|
getModelStats: jest.fn(),
|
|
getModelHistory: jest.fn(),
|
|
}));
|
|
|
|
jest.unstable_mockModule('../../../database/schemas/management/vendor.schema.js', () => ({
|
|
vendorModel: { modelName: 'Vendor' },
|
|
}));
|
|
|
|
jest.unstable_mockModule('log4js', () => ({
|
|
default: {
|
|
getLogger: () => ({
|
|
level: 'info',
|
|
debug: jest.fn(),
|
|
error: jest.fn(),
|
|
warn: jest.fn(),
|
|
trace: jest.fn(),
|
|
}),
|
|
},
|
|
}));
|
|
|
|
const {
|
|
listVendorsRouteHandler,
|
|
getVendorRouteHandler,
|
|
newVendorRouteHandler,
|
|
editVendorRouteHandler,
|
|
deleteVendorRouteHandler,
|
|
} = await import('../vendors.js');
|
|
|
|
const { listObjects, getObject, editObject, newObject, deleteObject } = await import(
|
|
'../../../database/database.js'
|
|
);
|
|
const { vendorModel } = await import('../../../database/schemas/management/vendor.schema.js');
|
|
|
|
describe('Vendor 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('listVendorsRouteHandler', () => {
|
|
it('should list vendors', async () => {
|
|
const mockResult = [{ _id: '1', name: 'Vendor 1' }];
|
|
listObjects.mockResolvedValue(mockResult);
|
|
|
|
await listVendorsRouteHandler(req, res);
|
|
|
|
expect(listObjects).toHaveBeenCalledWith(expect.objectContaining({ model: vendorModel }));
|
|
expect(res.send).toHaveBeenCalledWith(mockResult);
|
|
});
|
|
});
|
|
|
|
describe('newVendorRouteHandler', () => {
|
|
it('should create a new vendor', async () => {
|
|
req.body = { name: 'New Vendor', email: 'vendor@example.com' };
|
|
const mockVendor = { _id: '456', ...req.body };
|
|
newObject.mockResolvedValue(mockVendor);
|
|
|
|
await newVendorRouteHandler(req, res);
|
|
|
|
expect(newObject).toHaveBeenCalled();
|
|
expect(res.send).toHaveBeenCalledWith(mockVendor);
|
|
});
|
|
});
|
|
|
|
describe('editVendorRouteHandler', () => {
|
|
it('should update a vendor', async () => {
|
|
req.params.id = '507f1f77bcf86cd799439011';
|
|
req.body = { name: 'Updated Vendor' };
|
|
const mockResult = { _id: '507f1f77bcf86cd799439011', ...req.body };
|
|
editObject.mockResolvedValue(mockResult);
|
|
|
|
await editVendorRouteHandler(req, res);
|
|
|
|
expect(editObject).toHaveBeenCalled();
|
|
expect(res.send).toHaveBeenCalledWith(mockResult);
|
|
});
|
|
});
|
|
});
|
|
|