diff --git a/modules/ticket/back/methods/sale/specs/updateDiscount.spec.js b/modules/ticket/back/methods/sale/specs/updateDiscount.spec.js new file mode 100644 index 000000000..69f1a11ff --- /dev/null +++ b/modules/ticket/back/methods/sale/specs/updateDiscount.spec.js @@ -0,0 +1,82 @@ +const app = require('vn-loopback/server/server'); + +// #1486 back test updateDiscount +xdescribe('sale updateDiscount()', () => { + let originalSale; + let createdSaleComponent; + + afterAll(async done => { + originalSale.save(); + createdSaleComponent.destroy(); + done(); + }); + + + it('should throw an error if any sale discount is not a number', async() => { + let error; + const ticketId = 1; + const sales = [ + {discount: 'pepinillos'} + ]; + + try { + await app.models.Sale.updateDiscount(ticketId, sales); + } catch (err) { + error = err; + } + + expect(error.message).toEqual('The value should be a number'); + }); + + it('should throw an error if the ticket is invoiced already', async() => { + let error; + const ticketId = 7; + const sales = [ + {discount: 100} + ]; + + try { + await app.models.Sale.updateDiscount(ticketId, sales); + } catch (err) { + error = err; + } + + expect(error.message).toEqual(`The sales of this ticket can't be modified`); + }); + + it('should update the discount if the salesPerson has mana', async() => { + const saleId = 1; + originalSale = await app.models.Sale.findById(saleId); + + let manaDiscount = await app.models.ComponentRate.findOne({where: {code: 'mana'}}); + let componentId = manaDiscount.id; + createdSaleComponent = await app.models.SaleComponent.findOne({ + where: { + componentFk: componentId, + saleFk: saleId + } + }); + + expect(createdSaleComponent).toBeNull(); + + const ticketId = 8; + const sales = [ + { + id: saleId, + discount: 100} + ]; + + await app.models.Sale.updateDiscount(ticketId, sales); + + let updatedSale = await app.models.Sale.findById(saleId); + createdSaleComponent = await app.models.SaleComponent.findOne({ + where: { + componentFk: componentId, + saleFk: saleId + } + }); + + expect(createdSaleComponent.componentFk).toEqual(componentId); + expect(updatedSale.discount).toEqual(100); + }); +});