Added tests.
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 is contained in:
parent
4458a1d828
commit
6645898471
@ -10,6 +10,8 @@ jest.unstable_mockModule('../../../database/database.js', () => ({
|
|||||||
listObjectsByProperties: jest.fn(),
|
listObjectsByProperties: jest.fn(),
|
||||||
getModelStats: jest.fn(),
|
getModelStats: jest.fn(),
|
||||||
getModelHistory: jest.fn(),
|
getModelHistory: jest.fn(),
|
||||||
|
aggregateRollups: jest.fn(),
|
||||||
|
aggregateRollupsHistory: jest.fn(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
jest.unstable_mockModule('../../../database/schemas/inventory/orderitem.schema.js', () => ({
|
jest.unstable_mockModule('../../../database/schemas/inventory/orderitem.schema.js', () => ({
|
||||||
|
|||||||
@ -1,13 +1,17 @@
|
|||||||
import { jest } from '@jest/globals';
|
import { jest } from '@jest/globals';
|
||||||
|
|
||||||
|
jest.unstable_mockModule('../../../database/database.js', () => ({
|
||||||
|
listObjects: jest.fn(),
|
||||||
|
listObjectsByProperties: jest.fn(),
|
||||||
|
getObject: jest.fn(),
|
||||||
|
editObject: jest.fn(),
|
||||||
|
newObject: jest.fn(),
|
||||||
|
getModelStats: jest.fn(),
|
||||||
|
getModelHistory: jest.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
jest.unstable_mockModule('../../../database/schemas/management/material.schema.js', () => ({
|
jest.unstable_mockModule('../../../database/schemas/management/material.schema.js', () => ({
|
||||||
materialModel: {
|
materialModel: { modelName: 'material' },
|
||||||
modelName: 'Material',
|
|
||||||
aggregate: jest.fn(),
|
|
||||||
findOne: jest.fn(),
|
|
||||||
updateOne: jest.fn(),
|
|
||||||
create: jest.fn(),
|
|
||||||
},
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
jest.unstable_mockModule('log4js', () => ({
|
jest.unstable_mockModule('log4js', () => ({
|
||||||
@ -28,6 +32,7 @@ const {
|
|||||||
newMaterialRouteHandler,
|
newMaterialRouteHandler,
|
||||||
} = await import('../materials.js');
|
} = await import('../materials.js');
|
||||||
|
|
||||||
|
const { listObjects, getObject, newObject } = await import('../../../database/database.js');
|
||||||
const { materialModel } = await import('../../../database/schemas/management/material.schema.js');
|
const { materialModel } = await import('../../../database/schemas/management/material.schema.js');
|
||||||
|
|
||||||
describe('Material Service Route Handlers', () => {
|
describe('Material Service Route Handlers', () => {
|
||||||
@ -50,11 +55,11 @@ describe('Material Service Route Handlers', () => {
|
|||||||
describe('listMaterialsRouteHandler', () => {
|
describe('listMaterialsRouteHandler', () => {
|
||||||
it('should list materials', async () => {
|
it('should list materials', async () => {
|
||||||
const mockResult = [{ name: 'PLA' }];
|
const mockResult = [{ name: 'PLA' }];
|
||||||
materialModel.aggregate.mockResolvedValue(mockResult);
|
listObjects.mockResolvedValue(mockResult);
|
||||||
|
|
||||||
await listMaterialsRouteHandler(req, res);
|
await listMaterialsRouteHandler(req, res);
|
||||||
|
|
||||||
expect(materialModel.aggregate).toHaveBeenCalled();
|
expect(listObjects).toHaveBeenCalledWith(expect.objectContaining({ model: materialModel }));
|
||||||
expect(res.send).toHaveBeenCalledWith(mockResult);
|
expect(res.send).toHaveBeenCalledWith(mockResult);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -63,11 +68,11 @@ describe('Material Service Route Handlers', () => {
|
|||||||
it('should get a material by ID', async () => {
|
it('should get a material by ID', async () => {
|
||||||
req.params.id = '507f1f77bcf86cd799439011';
|
req.params.id = '507f1f77bcf86cd799439011';
|
||||||
const mockMaterial = { _id: '507f1f77bcf86cd799439011', name: 'PLA' };
|
const mockMaterial = { _id: '507f1f77bcf86cd799439011', name: 'PLA' };
|
||||||
materialModel.findOne.mockResolvedValue(mockMaterial);
|
getObject.mockResolvedValue(mockMaterial);
|
||||||
|
|
||||||
await getMaterialRouteHandler(req, res);
|
await getMaterialRouteHandler(req, res);
|
||||||
|
|
||||||
expect(materialModel.findOne).toHaveBeenCalled();
|
expect(getObject).toHaveBeenCalledWith(expect.objectContaining({ model: materialModel, id: '507f1f77bcf86cd799439011' }));
|
||||||
expect(res.send).toHaveBeenCalledWith(mockMaterial);
|
expect(res.send).toHaveBeenCalledWith(mockMaterial);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user