const app = require('vn-loopback/server/server'); describe('sale moveToTicket()', () => { let createdTicketId; afterAll(async done => { await app.models.Ticket.destroyById(createdTicketId); done(); }); it('should throw an error if the ticket is not editable', async() => { const ctx = {req: {accessToken: {userId: 101}}}; let error; const params = {currentTicket: {currentTicketId: 10}}; await app.models.Sale.moveToTicket(ctx, params) .catch(response => { expect(response.message).toEqual(`The sales of the current ticket can't be modified`); error = response; }); expect(error).toBeDefined(); }); it('should throw an error if the receiving ticket is not editable', async() => { const ctx = {req: {accessToken: {userId: 101}}}; let error; const params = {currentTicket: {currentTicketId: 16}, receiverTicket: {id: 1}}; await app.models.Sale.moveToTicket(ctx, params) .catch(response => { expect(response.message).toEqual(`The sales of the receiver ticket can't be modified`); error = response; }); expect(error).toBeDefined(); }); it('should transfer the sales from one ticket to a new one', async() => { const ctx = {req: {accessToken: {userId: 101}}}; let currentTicket = await app.models.Ticket.findById(11); currentTicket.currentTicketId = currentTicket.id; currentTicket.id = undefined; let currentTicketSales = await app.models.Ticket.getSales(currentTicket.currentTicketId); expect(currentTicketSales.length).toEqual(2); let params = { currentTicket: currentTicket, receiverTicket: {id: undefined}, sales: [ {id: currentTicketSales[0].id}, {id: currentTicketSales[1].id} ] }; let createdTicket = await app.models.Sale.moveToTicket(ctx, params); createdTicketId = createdTicket.id; currentTicketSales = await app.models.Ticket.getSales(currentTicket.currentTicketId); receiverTicketSales = await app.models.Ticket.getSales(createdTicket.id); expect(currentTicketSales.length).toEqual(0); expect(receiverTicketSales.length).toEqual(2); }); it('should transfer back the sales and set the created ticket as deleted', async() => { const ctx = {req: {accessToken: {userId: 101}}}; let receiverTicketId = 11; let currentTicket = await app.models.Ticket.findById(createdTicketId); currentTicket.currentTicketId = createdTicketId; currentTicket.id = undefined; let createdTicket = await app.models.Ticket.findById(createdTicketId); let createdTicketSales = await app.models.Ticket.getSales(createdTicketId); let receiverTicketSales = await app.models.Ticket.getSales(receiverTicketId); expect(createdTicket.isDeleted).toBeFalsy(); expect(createdTicketSales.length).toEqual(2); expect(receiverTicketSales.length).toEqual(0); let params = { removeEmptyTicket: true, currentTicket: currentTicket, receiverTicket: {id: receiverTicketId}, sales: [ {id: createdTicketSales[0].id}, {id: createdTicketSales[1].id} ] }; await app.models.Sale.moveToTicket(ctx, params); createdTicket = await app.models.Ticket.findById(createdTicketId); createdTicketSales = await app.models.Ticket.getSales(createdTicketId); receiverTicketSales = await app.models.Ticket.getSales(receiverTicketId); expect(createdTicket.isDeleted).toBeTruthy(); expect(createdTicketSales.length).toEqual(0); expect(receiverTicketSales.length).toEqual(2); }); });