salix/modules/invoiceOut/back/methods/invoiceOut/specs/refundAndInvoice.spec.js

82 lines
2.7 KiB
JavaScript

const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context');
describe('InvoiceOut refundAndInvoice()', () => {
const userId = 5;
const ctx = {req: {accessToken: {userId}}};
const activeCtx = {accessToken: {userId}};
const id = 4;
const cplusRectificationTypeFk = 1;
const siiTypeInvoiceOutFk = 1;
const invoiceCorrectionTypeFk = 1;
beforeEach(() => {
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({active: activeCtx});
});
it('should refund an invoice and create a new invoice with warehouse', async() => {
const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx};
const withWarehouse = true;
try {
const result = await models.InvoiceOut.refundAndInvoice(
ctx,
id,
withWarehouse,
cplusRectificationTypeFk,
siiTypeInvoiceOutFk,
invoiceCorrectionTypeFk,
options
);
expect(result).toBeDefined();
expect(result.refundId).toBeDefined();
const invoicesAfter = await models.InvoiceOut.find({where: {id: result.refundId}}, options);
const ticketsAfter = await models.Ticket.find(
{where: {refFk: 'R10100001', warehouse: {neq: null}}}, options);
expect(invoicesAfter.length).toBeGreaterThan(0);
expect(ticketsAfter.length).toBeGreaterThan(0);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should refund an invoice and create a new invoice with warehouse null', async() => {
const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx};
const withWarehouse = false;
try {
const result = await models.InvoiceOut.refundAndInvoice(
ctx,
id,
withWarehouse,
cplusRectificationTypeFk,
siiTypeInvoiceOutFk,
invoiceCorrectionTypeFk,
options
);
expect(result).toBeDefined();
expect(result.refundId).toBeDefined();
const invoicesAfter = await models.InvoiceOut.find({where: {id: result.refundId}}, options);
const ticketsAfter = await models.Ticket.find({where: {refFk: 'R10100001', warehouse: null}}, options);
expect(invoicesAfter.length).toBeGreaterThan(0);
expect(ticketsAfter.length).toBeGreaterThan(0);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});