From a0c5b1fff6f27d9be04370c085a3395753138132 Mon Sep 17 00:00:00 2001 From: jorgep Date: Mon, 5 Feb 2024 11:59:51 +0100 Subject: [PATCH] feat: refs # test corrective & clone --- .../methods/invoice-in/specs/clone.spec.js | 104 ++++++++++-------- .../invoice-in/specs/corrective.spec.js | 49 +++++++++ 2 files changed, 109 insertions(+), 44 deletions(-) create mode 100644 modules/invoiceIn/back/methods/invoice-in/specs/corrective.spec.js diff --git a/modules/invoiceIn/back/methods/invoice-in/specs/clone.spec.js b/modules/invoiceIn/back/methods/invoice-in/specs/clone.spec.js index b6c0fb160..436306aab 100644 --- a/modules/invoiceIn/back/methods/invoice-in/specs/clone.spec.js +++ b/modules/invoiceIn/back/methods/invoice-in/specs/clone.spec.js @@ -2,59 +2,75 @@ const models = require('vn-loopback/server/server').models; const LoopBackContext = require('loopback-context'); describe('invoiceIn clone()', () => { - beforeAll(async() => { - const activeCtx = { - accessToken: {userId: 9}, - http: { - req: { - headers: {origin: 'http://localhost'} - } - } + let ctx; + let options; + let tx; + + beforeEach(async() => { + ctx = { + req: { + accessToken: {userId: 1}, + headers: {origin: 'http://localhost'} + }, + args: {} }; + 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() => { - const userId = 1; - const ctx = { - req: { + const clone = await models.InvoiceIn.clone(ctx, 1, false, options); - accessToken: {userId: userId}, - headers: {origin: 'http://localhost:5000'}, - } - }; - - const tx = await models.InvoiceIn.beginTransaction({}); - const options = {transaction: tx}; - - 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 + expect(clone.supplierRef).toEqual('1234(2)'); + const invoiceIn = await models.InvoiceIn.findOne({ + include: [ + { + relation: 'invoiceInTax', + }, + { + relation: 'invoiceInDueDay', } - }, options); - const invoiceInTax = invoiceIn.invoiceInTax(); - const invoiceInDueDay = invoiceIn.invoiceInDueDay(); + ], where: { + id: clone.id + } + }, options); + const invoiceInTax = invoiceIn.invoiceInTax(); + const invoiceInDueDay = invoiceIn.invoiceInDueDay(); - expect(invoiceInTax.length).toEqual(2); - expect(invoiceInDueDay.length).toEqual(2); + expect(invoiceInTax.length).toEqual(2); + expect(invoiceInDueDay.length).toEqual(2); + }); - await tx.rollback(); - } catch (e) { - await tx.rollback(); - throw e; - } + it('should return the cloned invoiceIn and also clone invoiceInIntrastat and invoiceInTaxes if it is rectificative', async() => { + const clone = await models.InvoiceIn.clone(ctx, 1, true, options); + + 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); }); }); diff --git a/modules/invoiceIn/back/methods/invoice-in/specs/corrective.spec.js b/modules/invoiceIn/back/methods/invoice-in/specs/corrective.spec.js new file mode 100644 index 000000000..1047cd028 --- /dev/null +++ b/modules/invoiceIn/back/methods/invoice-in/specs/corrective.spec.js @@ -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); + }); +});