salix/modules/ticket/back/methods/sale/specs/updatePrice.spec.js

84 lines
3.2 KiB
JavaScript
Raw Normal View History

2019-01-24 08:08:28 +00:00
const app = require('vn-loopback/server/server');
2018-08-14 10:48:12 +00:00
describe('sale updatePrice()', () => {
let originalSale;
let originalSalesPersonMana;
let createdSaleComponent;
2019-06-14 10:27:41 +00:00
let saleId = 7;
2019-06-19 12:40:47 +00:00
let manaComponentId;
2019-06-19 12:40:47 +00:00
beforeAll(async done => {
let component = await app.models.Component.findOne({where: {code: 'mana'}});
manaComponentId = component.id;
2019-06-19 12:40:47 +00:00
originalSale = await app.models.Sale.findById(saleId);
originalSalesPersonMana = await app.models.WorkerMana.findById(18);
done();
2019-05-29 12:59:26 +00:00
});
2018-08-14 10:48:12 +00:00
it('should throw an error if the ticket is not editable', async() => {
2020-05-15 10:17:34 +00:00
let ctx = {req: {accessToken: {userId: 18}}};
let immutableSaleId = 1;
let price = 5;
2018-08-14 10:48:12 +00:00
2019-07-22 11:14:00 +00:00
await app.models.Sale.updatePrice(ctx, immutableSaleId, price)
.catch(response => {
expect(response).toEqual(new Error(`The sales of this ticket can't be modified`));
error = response;
});
expect(error).toBeDefined();
2019-05-29 12:59:26 +00:00
});
2018-08-14 10:48:12 +00:00
2019-05-29 12:59:26 +00:00
it('should return 0 if the price is an empty string', async() => {
2020-05-15 10:17:34 +00:00
let ctx = {req: {accessToken: {userId: 18}}};
let price = '';
2019-07-22 11:14:00 +00:00
await app.models.Sale.updatePrice(ctx, saleId, price);
2020-10-08 14:00:19 +00:00
let updatedSale = await app.models.Sale.findById(saleId);
expect(updatedSale.price).toEqual(0);
2018-08-14 10:48:12 +00:00
2020-10-08 14:00:19 +00:00
// restores
await originalSale.updateAttributes(originalSale);
await app.models.SaleComponent.updateAll({componentFk: manaComponentId, saleFk: saleId}, {value: 0});
await originalSalesPersonMana.updateAttributes(originalSalesPersonMana);
2018-08-14 10:48:12 +00:00
});
2019-05-29 12:59:26 +00:00
2020-10-08 14:00:19 +00:00
it('should now set price as a number in a string', async() => {
2020-05-15 10:17:34 +00:00
let ctx = {req: {accessToken: {userId: 18}}};
let price = '8';
2019-05-29 12:59:26 +00:00
2019-07-22 11:14:00 +00:00
await app.models.Sale.updatePrice(ctx, saleId, price);
2020-10-08 14:00:19 +00:00
let updatedSale = await app.models.Sale.findById(saleId);
expect(updatedSale.price).toEqual(8);
2019-05-29 12:59:26 +00:00
2020-10-08 14:00:19 +00:00
// restores
await originalSale.updateAttributes(originalSale);
await app.models.SaleComponent.updateAll({componentFk: manaComponentId, saleFk: saleId}, {value: 0});
await originalSalesPersonMana.updateAttributes(originalSalesPersonMana);
2019-05-29 12:59:26 +00:00
});
2020-10-08 14:00:19 +00:00
it('should set price as a decimal number and check the sale has the mana component changing the salesPersonMana', async() => {
2020-05-15 10:17:34 +00:00
let ctx = {req: {accessToken: {userId: 18}}};
let price = 5.4;
2019-05-29 12:59:26 +00:00
2019-07-22 11:14:00 +00:00
await app.models.Sale.updatePrice(ctx, saleId, price);
2020-10-08 14:00:19 +00:00
let updatedSale = await app.models.Sale.findById(saleId);
createdSaleComponent = await app.models.SaleComponent.findOne({where: {saleFk: saleId, componentFk: manaComponentId}});
2019-05-29 12:59:26 +00:00
2020-10-08 14:00:19 +00:00
expect(updatedSale.price).toBe(price);
expect(createdSaleComponent.value).toEqual(-2.04);
let updatedSalesPersonMana = await app.models.WorkerMana.findById(18);
2019-05-29 12:59:26 +00:00
expect(updatedSalesPersonMana.amount).not.toEqual(originalSalesPersonMana.amount);
2020-10-08 14:00:19 +00:00
// restores
await originalSale.updateAttributes(originalSale);
await app.models.SaleComponent.updateAll({componentFk: manaComponentId, saleFk: saleId}, {value: 0});
await originalSalesPersonMana.updateAttributes(originalSalesPersonMana);
2019-05-29 12:59:26 +00:00
});
2018-08-14 10:48:12 +00:00
});