salix/back/methods/collection/spec/addItem.spec.js

57 lines
1.8 KiB
JavaScript
Raw Normal View History

const {models} = require('vn-loopback/server/server');
const LoopBackContext = require('loopback-context');
describe('collection addItem()', () => {
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');
}
});
});