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

107 lines
3.8 KiB
JavaScript
Raw Normal View History

2019-01-24 08:08:28 +00:00
const app = require('vn-loopback/server/server');
describe('sale moveToTicket()', () => {
let createdTicketId;
afterAll(async done => {
await app.models.Ticket.destroyById(createdTicketId);
done();
});
2019-02-06 15:37:59 +00:00
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();
});
2019-02-06 15:37:59 +00:00
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(8);
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: 13},
{id: 14}
]
};
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 transfers back the sales and set the created ticket as deleted', async() => {
const ctx = {req: {accessToken: {userId: 101}}};
let receiverTicketId = 8;
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: 13},
{id: 14}
]
};
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);
});
});