salix/modules/ticket/back/methods/sale/specs/updateQuantity.spec.js

54 lines
1.7 KiB
JavaScript

const app = require('vn-loopback/server/server');
describe('sale updateQuantity()', () => {
const ctx = {
req: {
accessToken: {userId: 9},
headers: {origin: 'localhost:5000'},
__: () => {}
}
};
it('should throw an error if the quantity is not a number', async() => {
let error;
await app.models.Sale.updateQuantity(ctx, 1, 'wrong quantity!')
.catch(response => {
expect(response).toEqual(new Error('The value should be a number'));
error = response;
});
expect(error).toBeDefined();
});
it('should throw an error if the quantity is greater than it should be', async() => {
let error;
await app.models.Sale.updateQuantity(ctx, 1, 99)
.catch(response => {
expect(response).toEqual(new Error('The new quantity should be smaller than the old one'));
error = response;
});
expect(error).toBeDefined();
});
it('should update the quantity of a given sale current line', async() => {
let originalLineData = await app.models.Sale.findOne({where: {id: 1}, fields: ['quantity']});
expect(originalLineData.quantity).toEqual(5);
await app.models.Sale.updateQuantity(ctx, 1, 4);
let modifiedLineData = await app.models.Sale.findOne({where: {id: 1}, fields: ['quantity']});
expect(modifiedLineData.quantity).toEqual(4);
await app.models.Sale.update({id: 1}, {quantity: 5});
let resetLineDataValues = await app.models.Sale.findOne({where: {id: 1}, fields: ['quantity']});
expect(resetLineDataValues.quantity).toEqual(5);
});
});