feat: refs # test corrective & clone
gitea/salix/pipeline/pr-dev There was a failure building this commit Details

This commit is contained in:
Jorge Penadés 2024-02-05 11:59:51 +01:00
parent cd73e5adfe
commit a0c5b1fff6
2 changed files with 109 additions and 44 deletions

View File

@ -2,59 +2,75 @@ const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context'); const LoopBackContext = require('loopback-context');
describe('invoiceIn clone()', () => { describe('invoiceIn clone()', () => {
beforeAll(async() => { let ctx;
const activeCtx = { let options;
accessToken: {userId: 9}, let tx;
http: {
req: { beforeEach(async() => {
headers: {origin: 'http://localhost'} ctx = {
} req: {
} accessToken: {userId: 1},
headers: {origin: 'http://localhost'}
},
args: {}
}; };
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx active: ctx.req
}); });
options = {transaction: tx};
tx = await models.Sale.beginTransaction({});
options.transaction = tx;
});
afterEach(async() => {
await tx.rollback();
}); });
it('should return the cloned invoiceIn and also clone invoiceInDueDays and invoiceInTaxes if there are any referencing the invoiceIn', async() => { it('should return the cloned invoiceIn and also clone invoiceInDueDays and invoiceInTaxes if there are any referencing the invoiceIn', async() => {
const userId = 1; const clone = await models.InvoiceIn.clone(ctx, 1, false, options);
const ctx = {
req: {
accessToken: {userId: userId}, expect(clone.supplierRef).toEqual('1234(2)');
headers: {origin: 'http://localhost:5000'}, const invoiceIn = await models.InvoiceIn.findOne({
} include: [
}; {
relation: 'invoiceInTax',
const tx = await models.InvoiceIn.beginTransaction({}); },
const options = {transaction: tx}; {
relation: 'invoiceInDueDay',
try {
const clone = await models.InvoiceIn.clone(ctx, 1, false, options);
expect(clone.supplierRef).toEqual('1234(2)');
const invoiceIn = await models.InvoiceIn.findOne({
include: [
{
relation: 'invoiceInTax',
},
{
relation: 'invoiceInDueDay',
}
], where: {
id: clone.id
} }
}, options); ], where: {
const invoiceInTax = invoiceIn.invoiceInTax(); id: clone.id
const invoiceInDueDay = invoiceIn.invoiceInDueDay(); }
}, options);
const invoiceInTax = invoiceIn.invoiceInTax();
const invoiceInDueDay = invoiceIn.invoiceInDueDay();
expect(invoiceInTax.length).toEqual(2); expect(invoiceInTax.length).toEqual(2);
expect(invoiceInDueDay.length).toEqual(2); expect(invoiceInDueDay.length).toEqual(2);
});
await tx.rollback(); it('should return the cloned invoiceIn and also clone invoiceInIntrastat and invoiceInTaxes if it is rectificative', async() => {
} catch (e) { const clone = await models.InvoiceIn.clone(ctx, 1, true, options);
await tx.rollback();
throw e; expect(clone.supplierRef).toEqual('1234(2)');
} const invoiceIn = await models.InvoiceIn.findOne({
include: [
{
relation: 'invoiceInTax',
},
{
relation: 'invoiceInIntrastat',
}
], where: {
id: clone.id
}
}, options);
const invoiceInTax = invoiceIn.invoiceInTax();
const invoiceInIntrastat = invoiceIn.invoiceInIntrastat();
expect(invoiceInTax.length).toEqual(2);
expect(invoiceInIntrastat.length).toEqual(2);
}); });
}); });

View File

@ -0,0 +1,49 @@
const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context');
describe('invoiceIn corrective()', () => {
let ctx;
let options;
let tx;
beforeEach(async() => {
ctx = {
req: {
accessToken: {userId: 9},
headers: {origin: 'http://localhost'}
},
args: {}
};
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: ctx.req
});
options = {transaction: tx};
tx = await models.Sale.beginTransaction({});
options.transaction = tx;
});
afterEach(async() => {
await tx.rollback();
});
it('La función corrective debería devolver un id cuando se ejecuta correctamente', async() => {
const originalId = 1;
const invoiceReason = 3;
const invoiceType = 2;
const invoiceClass = 1;
const cloneId = await models.InvoiceIn.corrective(ctx,
originalId, invoiceReason, invoiceType, invoiceClass, options);
expect(cloneId).toBeDefined();
const correction = await models.InvoiceInCorrection.findOne({
where: {correctedFk: originalId, correctingFk: cloneId}
}, options);
expect(correction.cplusRectificationTypeFk).toEqual(invoiceType);
expect(correction.siiTypeInvoiceOutFk).toEqual(invoiceClass);
expect(correction.invoiceCorrectionTypeFk).toEqual(invoiceReason);
});
});