#753 order - removes.spec.js repairs + refactors

This commit is contained in:
Carlos Jimenez 2018-10-24 14:10:08 +02:00
parent 443d6ec08a
commit 7a6b2f19d3
3 changed files with 34 additions and 5 deletions

View File

@ -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++) {

View File

@ -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]});
});
});

View File

@ -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}]);
});
});