const models = require('vn-loopback/server/server').models; 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() => { const tx = await models.Sale.beginTransaction({}); let error; try { const options = {transaction: tx}; await models.Sale.updateQuantity(ctx, 1, 'wrong quantity!', options); await tx.rollback(); } catch (e) { await tx.rollback(); error = e; } expect(error).toEqual(new Error('The value should be a number')); }); it('should throw an error if the quantity is greater than it should be', async() => { const tx = await models.Sale.beginTransaction({}); let error; try { const options = {transaction: tx}; await models.Sale.updateQuantity(ctx, 1, 99, options); await tx.rollback(); } catch (e) { await tx.rollback(); error = e; } expect(error).toEqual(new Error('The new quantity should be smaller than the old one')); }); it('should update the quantity of a given sale current line', async() => { const tx = await models.Sale.beginTransaction({}); try { const options = {transaction: tx}; const originalLine = await models.Sale.findOne({where: {id: 1}, fields: ['quantity']}, options); expect(originalLine.quantity).toEqual(5); await models.Sale.updateQuantity(ctx, 1, 4, options); const modifiedLine = await models.Sale.findOne({where: {id: 1}, fields: ['quantity']}, options); expect(modifiedLine.quantity).toEqual(4); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); });