const models = require('vn-loopback/server/server').models; const UserError = require('vn-loopback/util/user-error'); describe('Order updateBasicData', () => { const orderId = 21; it('should return an error if the order is confirmed', async() => { const tx = await models.Order.beginTransaction({}); let error; try { const options = {transaction: tx}; const params = []; const orderConfirmed = 1; await models.Order.updateBasicData(orderConfirmed, params, options); await tx.rollback(); } catch (e) { await tx.rollback(); error = e; } const expectedError = `You can't make changes on the basic data of an confirmed order or with rows`; expect(error).toEqual(new UserError(expectedError)); }); it('should return an error if the order has rows', async() => { const tx = await models.Order.beginTransaction({}); let error; try { const options = {transaction: tx}; const params = []; const orderWithRows = 16; await models.Order.updateBasicData(orderWithRows, params, options); await tx.rollback(); } catch (e) { await tx.rollback(); error = e; } const expectedError = `You can't make changes on the basic data of an confirmed order or with rows`; expect(error).toEqual(new UserError(expectedError)); }); it('should skip invalid params', async() => { const tx = await models.Order.beginTransaction({}); try { const options = {transaction: tx}; let success; const invalidparams = {invalid: 'param for update'}; await models.Order.updateBasicData(orderId, invalidparams, options) .then(() => success = true); expect(success).toBeTruthy(); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); it('should update the client fiscal data and return the count if changes made', async() => { const tx = await models.Order.beginTransaction({}); try { const options = {transaction: tx}; const validparams = {note: 'test note'}; const order = await models.Order.findById(orderId, null, options); expect(order.note).toEqual(null); const result = await models.Order.updateBasicData(orderId, validparams, options); expect(result.note).toEqual('test note'); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); });