From 80d5c923ccac46cce7f074b54a2af69d60d978f7 Mon Sep 17 00:00:00 2001 From: Carlos Jimenez Ruiz Date: Thu, 30 May 2019 08:57:25 +0200 Subject: [PATCH] #1486 back test updateDiscount --- .../methods/sale/specs/updateDiscount.spec.js | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 modules/ticket/back/methods/sale/specs/updateDiscount.spec.js 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); + }); +});