salix/modules/ticket/back/methods/ticket/specs/transferSales.spec.js

186 lines
6.0 KiB
JavaScript

const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context');
describe('sale transferSales()', () => {
const userId = 1101;
const activeCtx = {
accessToken: {userId: userId},
headers: {origin: ''},
__: value => value
};
const ctx = {req: activeCtx};
beforeAll(() => {
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx
});
});
it('should throw an error as the ticket is not editable', async() => {
const tx = await models.Ticket.beginTransaction({});
let error;
try {
const options = {transaction: tx};
const currentTicketId = 1;
const receiverTicketId = undefined;
const sales = [];
await models.Ticket.transferSales(ctx, currentTicketId, receiverTicketId, sales, options);
await tx.rollback();
} catch (e) {
await tx.rollback();
error = e;
}
expect(error.message).toEqual(`This ticket is not editable.`);
});
it('should throw an error if the receiving ticket is not editable', async() => {
const tx = await models.Ticket.beginTransaction({});
let error;
try {
const options = {transaction: tx};
const currentTicketId = 16;
const receiverTicketId = 1;
const sales = [];
await models.Ticket.transferSales(ctx, currentTicketId, receiverTicketId, sales, options);
await tx.rollback();
} catch (e) {
await tx.rollback();
error = e;
}
expect(error.message).toEqual(`The sales of the receiver ticket can't be modified`);
});
it('should throw an error if any of the sales has a claim', async() => {
const tx = await models.Ticket.beginTransaction({});
let error;
try {
const options = {transaction: tx};
const ticketId = 11;
const receiverTicketId = null;
const sales = await models.Ticket.getSales(ctx, ticketId, options);
await models.Ticket.transferSales(ctx, ticketId, receiverTicketId, sales, options);
await tx.rollback();
} catch (e) {
await tx.rollback();
error = e;
}
expect(error.message).toEqual(`Can't transfer claimed sales`);
});
it('should transfer the sales from a ticket to a new one', async() => {
const tx = await models.Ticket.beginTransaction({});
try {
const options = {transaction: tx};
const formerTicketId = 22;
let createdTicketId = undefined;
let formerTicketSales = await models.Ticket.getSales(ctx, formerTicketId, options);
expect(formerTicketSales.length).toEqual(2);
let createdTicket = await models.Ticket.transferSales(
ctx, formerTicketId, createdTicketId, formerTicketSales, options);
createdTicketId = createdTicket.id;
formerTicketSales = await models.Ticket.getSales(ctx, formerTicketId, options);
let createdTicketSales = await models.Ticket.getSales(ctx, createdTicketId, options);
expect(formerTicketSales.length).toEqual(0);
expect(createdTicketSales.length).toEqual(2);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
describe('sale transferPartialSales()', () => {
it('should throw an error in the quantity to transfer exceeds the amount from the original sale', async() => {
const tx = await models.Ticket.beginTransaction({});
let error;
try {
const options = {transaction: tx};
const currentTicketId = 22;
const currentTicketSales = await models.Ticket.getSales(ctx, currentTicketId, options);
const receiverTicketId = undefined;
currentTicketSales[0].quantity = 99;
await models.Ticket.transferSales(
ctx, currentTicketId, receiverTicketId, currentTicketSales, options);
await tx.rollback();
} catch (e) {
await tx.rollback();
error = e;
}
expect(error.message).toEqual(`Invalid quantity`);
});
it('should transfer two sales to a new ticket but one shall be partial', async() => {
const tx = await models.Ticket.beginTransaction({});
try {
const options = {transaction: tx};
const formerTicketId = 22;
let createdTicketId = undefined;
let formerTicketSales = await models.Ticket.getSales(ctx, formerTicketId, options);
const completeSaleId = formerTicketSales[1].id;
let partialSaleTotalQuantity = formerTicketSales[0].quantity;
expect(partialSaleTotalQuantity).toEqual(30);
formerTicketSales[0].quantity = 1;
let createdTicket = await models.Ticket.transferSales(
ctx, formerTicketId, createdTicketId, formerTicketSales, options);
createdTicketId = createdTicket.id;
formerTicketSales = await models.Ticket.getSales(ctx, formerTicketId, options);
createdTicketSales = await models.Ticket.getSales(ctx, createdTicketId, options);
const [createdPartialSale] = createdTicketSales.filter(sale => {
return sale.id != completeSaleId;
});
expect(formerTicketSales.length).toEqual(1);
expect(formerTicketSales[0].quantity).toEqual(partialSaleTotalQuantity - 1);
expect(createdTicketSales.length).toEqual(2);
expect(createdPartialSale.quantity).toEqual(1);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});
});