salix/modules/invoiceIn/back/methods/invoice-in/specs/invoiceInTax.spec.js

75 lines
2.6 KiB
JavaScript

const models = require('vn-loopback/server/server').models;
describe('invoiceInTax', () => {
let options;
let tx;
const invoiceInId = 1;
const invoiceInTaxId = 1;
beforeEach(async() => {
tx = await models.InvoiceInTax.beginTransaction({});
options = {transaction: tx};
});
afterEach(async() => {
await tx.rollback();
});
it('should throw an error if trying to save a tax from a booked invoice', async() => {
const invoiceIn = await models.InvoiceIn.findById(invoiceInId, null, options);
await invoiceIn.updateAttributes({isBooked: true}, options);
const invoiceInTax = await models.InvoiceInTax.findById(invoiceInTaxId, null, options);
let error;
try {
await invoiceInTax.updateAttribute('taxableBase', 100, options);
} catch (err) {
error = err;
}
expect(error.message).toBe('InvoiceIn is already booked');
});
it('should allow save if the invoice is not booked', async() => {
const invoiceIn = await models.InvoiceIn.findById(invoiceInId, null, options);
await invoiceIn.updateAttribute('isBooked', false, options);
const invoiceInTax = await models.InvoiceInTax.findById(invoiceInTaxId, null, options);
await invoiceInTax.updateAttribute('taxableBase', 100, options);
const updatedInvoiceInTax = await models.InvoiceInTax.findById(invoiceInTaxId, null, options);
expect(updatedInvoiceInTax.taxableBase).toBe(100);
});
it('should throw an error if trying to delete a tax from a booked invoice', async() => {
const invoiceIn = await models.InvoiceIn.findById(invoiceInId, null, options);
await invoiceIn.updateAttribute('isBooked', true, options);
let error;
try {
await models.InvoiceInTax.destroyById(invoiceInTaxId, options);
} catch (err) {
error = err;
}
expect(error).toBeDefined();
expect(error.message).toBe('InvoiceIn is already booked');
});
it('should allow delete if the invoice is not booked', async() => {
const invoiceIn = await models.InvoiceIn.findById(invoiceInId, null, options);
await invoiceIn.updateAttribute('isBooked', false, options);
let error;
try {
await models.InvoiceInTax.destroyById(invoiceInTaxId, options);
} catch (err) {
error = err;
}
const deletedInvoiceInTax = await models.InvoiceInTax.findById(invoiceInTaxId, null, options);
expect(error).toBeUndefined();
expect(deletedInvoiceInTax).toBeNull();
});
});