salix/modules/ticket/back/methods/sale/specs/refund.spec.js

81 lines
2.5 KiB
JavaScript
Raw Normal View History

const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context');
describe('Sale refund()', () => {
const userId = 5;
2023-09-25 08:49:47 +00:00
const ctx = {req: {accessToken: userId}, args: {}};
const activeCtx = {
2023-06-01 06:32:06 +00:00
accessToken: {userId},
};
const servicesIds = [3];
2023-05-26 10:03:36 +00:00
const withWarehouse = true;
beforeEach(() => {
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx
});
});
it('should create ticket with the selected lines', async() => {
const tx = await models.Sale.beginTransaction({});
const salesIds = [7, 8];
try {
const options = {transaction: tx};
const refundedTickets = await models.Sale.refund(ctx, salesIds, servicesIds, withWarehouse, options);
expect(refundedTickets).toBeDefined();
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
2023-04-24 06:57:10 +00:00
it('should create one ticket for each unique ticketFk in the sales', async() => {
const tx = await models.Sale.beginTransaction({});
const salesIds = [6, 7];
try {
const options = {transaction: tx};
2023-09-25 08:49:47 +00:00
const ticketsBefore = await models.Ticket.find({}, options);
const tickets = await models.Sale.refund(ctx, salesIds, servicesIds, withWarehouse, options);
2023-04-24 06:57:10 +00:00
const refundedTicket = await models.Ticket.findOne({
where: {
id: tickets[0].id
},
include: [
{
relation: 'ticketSales',
scope: {
include: {
relation: 'components'
}
}
},
{
relation: 'ticketServices',
}
]
}, options);
2023-09-25 08:49:47 +00:00
const ticketsAfter = await models.Ticket.find({}, options);
2023-04-24 06:57:10 +00:00
const salesLength = refundedTicket.ticketSales().length;
const componentsLength = refundedTicket.ticketSales()[0].components().length;
2023-04-24 06:57:10 +00:00
expect(refundedTicket).toBeDefined();
2023-09-25 08:49:47 +00:00
expect(salesLength).toEqual(1);
expect(ticketsBefore.length).toEqual(ticketsAfter.length - 2);
expect(componentsLength).toEqual(4);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});