From f4add0942c2dc2f0e11ab379dda90270f649b4f2 Mon Sep 17 00:00:00 2001 From: Carlos Jimenez <=> Date: Wed, 24 Oct 2018 13:45:59 +0200 Subject: [PATCH 1/2] #751 removes.spec.js repairs --- .../common/methods/client/specs/getMana.spec.js | 1 - .../common/methods/sale/specs/removes.spec.js | 15 +++++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/services/loopback/common/methods/client/specs/getMana.spec.js b/services/loopback/common/methods/client/specs/getMana.spec.js index 1b621faab..fcaeb3730 100644 --- a/services/loopback/common/methods/client/specs/getMana.spec.js +++ b/services/loopback/common/methods/client/specs/getMana.spec.js @@ -7,4 +7,3 @@ describe('client getMana()', () => { expect(result.mana).toEqual(151.33); }); }); - diff --git a/services/loopback/common/methods/sale/specs/removes.spec.js b/services/loopback/common/methods/sale/specs/removes.spec.js index 1032129f9..fd74f3641 100644 --- a/services/loopback/common/methods/sale/specs/removes.spec.js +++ b/services/loopback/common/methods/sale/specs/removes.spec.js @@ -1,14 +1,13 @@ const app = require(`${servicesDir}/ticket/server/server`); -xdescribe('sale removes()', () => { +describe('sale removes()', () => { let sale; + let newsale; beforeAll(async() => { - sale = await app.models.Sale.findOne({where: {id: 1}}); - }); - - afterAll(async() => { - await app.models.Sale.create(sale); + sale = await app.models.Sale.findOne({where: {id: 9}}); + sale.id = null; + newsale = await app.models.Sale.create(sale); }); it('should throw an error if the ticket of the given sales is not editable', async() => { @@ -30,8 +29,8 @@ xdescribe('sale removes()', () => { it('should delete the sales', async() => { let params = { - sales: [{id: 1, instance: 0}], - actualTicketFk: 1 + sales: [{id: newsale.id, instance: 0}], + actualTicketFk: 16 }; let res = await app.models.Sale.removes(params); From 7a6b2f19d3f2e54c1822a246d6940db9febf6574 Mon Sep 17 00:00:00 2001 From: Carlos Jimenez <=> Date: Wed, 24 Oct 2018 14:10:08 +0200 Subject: [PATCH 2/2] #753 order - removes.spec.js repairs + refactors --- .../order/common/methods/order-row/removes.js | 7 ++++-- .../order-row/specs/addToOrder.spec.js | 8 ++++++- .../methods/order-row/specs/removes.spec.js | 24 +++++++++++++++++-- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/services/order/common/methods/order-row/removes.js b/services/order/common/methods/order-row/removes.js index c08ac9a8f..0f8aaa86e 100644 --- a/services/order/common/methods/order-row/removes.js +++ b/services/order/common/methods/order-row/removes.js @@ -23,9 +23,12 @@ module.exports = Self => { Self.removes = async params => { if (!params.rows || !params.rows.length) - throw new UserError('There is nothing delete'); + throw new UserError('There is nothing to delete'); - await Self.app.models.Order.isEditable(params.actualOrderId); + let isEditable = await Self.app.models.Order.isEditable(params.actualOrderId); + + if (!isEditable) + throw new UserError('This order is not editable'); let promises = []; for (let i = 0; i < params.rows.length; i++) { diff --git a/services/order/common/methods/order-row/specs/addToOrder.spec.js b/services/order/common/methods/order-row/specs/addToOrder.spec.js index c9c1a53cd..9cef506d6 100644 --- a/services/order/common/methods/order-row/specs/addToOrder.spec.js +++ b/services/order/common/methods/order-row/specs/addToOrder.spec.js @@ -1,6 +1,11 @@ const app = require(`../../../../server/server`); describe('order addToOrder()', () => { + let rowToDelete; + afterAll(async() => { + await app.models.OrderRow.removes({rows: [rowToDelete], actualOrderId: 16}); + }); + it('should add a row to a given order', async() => { let unmodifiedRows = await app.models.OrderRow.find({where: {orderFk: 16}}); @@ -19,7 +24,8 @@ describe('order addToOrder()', () => { let modifiedRows = await app.models.OrderRow.find({where: {orderFk: 16}}); + rowToDelete = modifiedRows[modifiedRows.length - 1].id; + expect(modifiedRows.length).toBe(5); - await app.models.OrderRow.removes({rows: [modifiedRows[modifiedRows.length - 1].id]}); }); }); diff --git a/services/order/common/methods/order-row/specs/removes.spec.js b/services/order/common/methods/order-row/specs/removes.spec.js index b6dafae24..f8a90e2c0 100644 --- a/services/order/common/methods/order-row/specs/removes.spec.js +++ b/services/order/common/methods/order-row/specs/removes.spec.js @@ -1,6 +1,15 @@ const app = require(`../../../../server/server`); -xdescribe('order removes()', () => { +describe('order removes()', () => { + let row; + let newRow; + + beforeAll(async() => { + row = await app.models.OrderRow.findOne({where: {id: 12}}); + row.id = null; + newRow = await app.models.OrderRow.create(row); + }); + it('should throw an error if rows property is empty', async() => { let error; try { @@ -9,7 +18,7 @@ xdescribe('order removes()', () => { error = e; } - expect(error).toEqual(new Error('There is nothing delete')); + expect(error).toEqual(new Error('There is nothing to delete')); }); it('should throw an error if the row selected is not editable', async() => { @@ -22,4 +31,15 @@ xdescribe('order removes()', () => { expect(error).toEqual(new Error('This order is not editable')); }); + + it('should delete the row', async() => { + let params = { + rows: [newRow.id], + actualOrderId: 16 + }; + + let res = await app.models.OrderRow.removes(params); + + expect(res).toEqual([{count: 1}]); + }); });