const models = require('vn-loopback/server/server').models; describe('sale updatePrice()', () => { const ctx = { req: { accessToken: {userId: 18}, headers: {origin: 'localhost:5000'}, __: () => {} } }; const saleId = 7; it('should throw an error if the ticket is not editable', async() => { const tx = await models.Sale.beginTransaction({}); try { const options = {transaction: tx}; const immutableSaleId = 1; const price = 5; await models.Sale.updatePrice(ctx, immutableSaleId, price, options); await tx.rollback(); } catch (e) { await tx.rollback(); error = e; } expect(error).toEqual(new Error(`The sales of this ticket can't be modified`)); }); it('should return 0 if the price is an empty string', async() => { const tx = await models.Sale.beginTransaction({}); try { const options = {transaction: tx}; const price = ''; await models.Sale.updatePrice(ctx, saleId, price, options); const updatedSale = await models.Sale.findById(saleId, null, options); expect(updatedSale.price).toEqual(0); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); it('should now set price as a number in a string', async() => { const tx = await models.Sale.beginTransaction({}); try { const options = {transaction: tx}; const price = '8'; await models.Sale.updatePrice(ctx, saleId, price, options); const updatedSale = await models.Sale.findById(saleId, null, options); expect(updatedSale.price).toEqual(8); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); it('should set price as a decimal number and check the sale has the mana component changing the salesPersonMana', async() => { const tx = await models.Sale.beginTransaction({}); try { const options = {transaction: tx}; const price = 5.4; const originalSalesPersonMana = await models.WorkerMana.findById(18, null, options); const manaComponent = await models.Component.findOne({where: {code: 'mana'}}, options); const teamOne = 96; const userId = ctx.req.accessToken.userId; const business = await models.Business.findOne({where: {workerFk: userId}}, options); await business.updateAttribute('departmentFk', teamOne, options); await models.Sale.updatePrice(ctx, saleId, price, options); const updatedSale = await models.Sale.findById(saleId, null, options); const createdSaleComponent = await models.SaleComponent.findOne({ where: { saleFk: saleId, componentFk: manaComponent.id }}, options); expect(updatedSale.price).toBe(price); expect(createdSaleComponent.value).toEqual(-2.04); const updatedSalesPersonMana = await models.WorkerMana.findById(18, null, options); expect(updatedSalesPersonMana.amount).not.toEqual(originalSalesPersonMana.amount); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); });