All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good
101 lines
2.7 KiB
JavaScript
101 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/taxrate.schema.js', () => ({
|
|
taxRateModel: { modelName: 'TaxRate' },
|
|
}));
|
|
|
|
jest.unstable_mockModule('log4js', () => ({
|
|
default: {
|
|
getLogger: () => ({
|
|
level: 'info',
|
|
debug: jest.fn(),
|
|
error: jest.fn(),
|
|
warn: jest.fn(),
|
|
trace: jest.fn(),
|
|
}),
|
|
},
|
|
}));
|
|
|
|
const {
|
|
listTaxRatesRouteHandler,
|
|
getTaxRateRouteHandler,
|
|
newTaxRateRouteHandler,
|
|
editTaxRateRouteHandler,
|
|
} = await import('../taxrates.js');
|
|
|
|
const { listObjects, getObject, editObject, newObject } = await import(
|
|
'../../../database/database.js'
|
|
);
|
|
const { taxRateModel } = await import('../../../database/schemas/management/taxrate.schema.js');
|
|
|
|
describe('Tax Rate 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('listTaxRatesRouteHandler', () => {
|
|
it('should list tax rates', async () => {
|
|
const mockResult = [{ _id: '1', name: 'GST', rate: 10 }];
|
|
listObjects.mockResolvedValue(mockResult);
|
|
|
|
await listTaxRatesRouteHandler(req, res);
|
|
|
|
expect(listObjects).toHaveBeenCalledWith(expect.objectContaining({ model: taxRateModel }));
|
|
expect(res.send).toHaveBeenCalledWith(mockResult);
|
|
});
|
|
});
|
|
|
|
describe('newTaxRateRouteHandler', () => {
|
|
it('should create a new tax rate', async () => {
|
|
req.body = { name: 'VAT', rate: 20, rateType: 'percentage' };
|
|
const mockTaxRate = { _id: '456', ...req.body };
|
|
newObject.mockResolvedValue(mockTaxRate);
|
|
|
|
await newTaxRateRouteHandler(req, res);
|
|
|
|
expect(newObject).toHaveBeenCalled();
|
|
expect(res.send).toHaveBeenCalledWith(mockTaxRate);
|
|
});
|
|
});
|
|
|
|
describe('editTaxRateRouteHandler', () => {
|
|
it('should update a tax rate', async () => {
|
|
req.params.id = '507f1f77bcf86cd799439011';
|
|
req.body = { rate: 15 };
|
|
const mockResult = { _id: '507f1f77bcf86cd799439011', rate: 15 };
|
|
editObject.mockResolvedValue(mockResult);
|
|
|
|
await editTaxRateRouteHandler(req, res);
|
|
|
|
expect(editObject).toHaveBeenCalled();
|
|
expect(res.send).toHaveBeenCalledWith(mockResult);
|
|
});
|
|
});
|
|
});
|
|
|