2024-01-18 10:51:21 +00:00
|
|
|
const {models} = require('vn-loopback/server/server');
|
|
|
|
const LoopBackContext = require('loopback-context');
|
|
|
|
|
2024-01-18 12:28:22 +00:00
|
|
|
describe('collection addItem()', () => {
|
2024-01-18 10:51:21 +00:00
|
|
|
const quantity = 3;
|
|
|
|
const ticketFk = 24;
|
|
|
|
const warehouseFk = 1;
|
|
|
|
beforeAll(async() => {
|
|
|
|
activeCtx = {
|
|
|
|
req: {
|
|
|
|
accessToken: {userId: 9},
|
|
|
|
headers: {origin: 'http://localhost'},
|
|
|
|
__: value => value
|
|
|
|
}
|
|
|
|
};
|
|
|
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
|
|
|
active: activeCtx
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should add a new sale', async() => {
|
|
|
|
const tx = await models.Sale.beginTransaction({});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
const code = '1111111111';
|
|
|
|
|
|
|
|
const salesBefore = await models.Sale.find(null, options);
|
|
|
|
await models.Collection.addItem(activeCtx, code, quantity, ticketFk, warehouseFk, options);
|
|
|
|
const salesAfter = await models.Sale.find(null, options);
|
|
|
|
|
|
|
|
expect(salesAfter.length).toEqual(salesBefore.length + 1);
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error when the item is not available', async() => {
|
|
|
|
const code = '00000000000';
|
|
|
|
const tx = await models.Sale.beginTransaction({});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
|
|
|
await models.Collection.addItem(activeCtx, code, quantity, ticketFk, warehouseFk, options);
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
const error = e;
|
|
|
|
|
|
|
|
expect(error.message).toEqual('We do not have availability for the selected item');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|