From ab341b9b5e2e2b8b8584c755bdb74a97153f24ce Mon Sep 17 00:00:00 2001 From: jgallego Date: Wed, 3 Jan 2024 15:36:39 +0100 Subject: [PATCH] test: refs #3979 test de clone --- modules/ticket/back/methods/sale/clone.js | 2 +- .../back/methods/sale/specs/clone.spec.js | 92 +++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 modules/ticket/back/methods/sale/specs/clone.spec.js diff --git a/modules/ticket/back/methods/sale/clone.js b/modules/ticket/back/methods/sale/clone.js index 53a501844..233d114cf 100644 --- a/modules/ticket/back/methods/sale/clone.js +++ b/modules/ticket/back/methods/sale/clone.js @@ -43,7 +43,7 @@ module.exports = Self => { } for (const sale of sales) { - const newTicketId = mappedTickets.get(sale.ticketFk); + const newTicketId = group ? ticketsIds : mappedTickets.get(sale.ticketFk); const createdSale = await models.Sale.create({ ticketFk: newTicketId, diff --git a/modules/ticket/back/methods/sale/specs/clone.spec.js b/modules/ticket/back/methods/sale/specs/clone.spec.js new file mode 100644 index 000000000..b12a7e982 --- /dev/null +++ b/modules/ticket/back/methods/sale/specs/clone.spec.js @@ -0,0 +1,92 @@ +const models = require('vn-loopback/server/server').models; +const LoopBackContext = require('loopback-context'); + +describe('Ticket cloning - clone function', () => { + 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.commit(); + }); + + it('should create new tickets with cloned sales with warehouse', async() => { + const salesIds = [1, 2, 3]; + const servicesIds = []; + const withWarehouse = true; + const group = false; + const negative = false; + const newTickets = await models.Sale.clone(ctx, salesIds, servicesIds, withWarehouse, group, negative, options); + + expect(newTickets).toBeDefined(); + expect(newTickets.length).toBeGreaterThan(0); + }); + + it('should handle negative quantities correctly', async() => { + const negative = true; + const salesIds = [7, 8]; + const newTickets = await models.Sale.clone(ctx, salesIds, [], false, false, negative, options); + + // Verify that the cloned sales have negative quantities + for (const ticket of newTickets) { + const sales = await models.Sale.find({where: {ticketFk: ticket.id}}, options); + sales.forEach(sale => { + expect(sale.quantity).toBeLessThan(0); + }); + } + }); + + it('should group sales into a single ticket if group is true', async() => { + const salesIds = [1, 2, 3, 4, 5]; + const group = true; + const newTickets = await models.Sale.clone(ctx, salesIds, [], false, group, false, options); + + expect(newTickets.length).toEqual(1); + // Additional assertions for ticket grouping + }); + + it('should create new components and services for cloned tickets', async() => { + const servicesIds = [2]; + const salesIds = [5]; + const newTickets = await models.Sale.clone(ctx, salesIds, servicesIds, false, false, false, options); + + // Verify that components and services are created for each new ticket + for (const ticket of newTickets) { + const sale = await models.Sale.findOne({where: {ticketFk: ticket.id}}, options); + const components = await models.SaleComponent.find({where: {saleFk: sale.id}}, options); + const services = await models.TicketService.find({where: {ticketFk: ticket.id}}, options); + + expect(components.length).toBeGreaterThan(0); + expect(services.length).toBeGreaterThan(0); + } + }); + + it('should handle transaction rollback on error', async() => { + const negative = true; + const salesIds = [1, 2, 3, 4, 5]; + try { + await models.Sale.clone(ctx, salesIds, [], false, false, negative, options); + fail('Expected error was not thrown'); + } catch (error) { + expect(error).toBeDefined(); + } + }); +});