salix/modules/ticket/back/methods/ticket-request/specs/confirm.spec.js

100 lines
3.0 KiB
JavaScript
Raw Normal View History

2019-07-25 07:55:09 +00:00
const app = require('vn-loopback/server/server');
describe('ticket-request confirm()', () => {
2019-10-22 11:44:36 +00:00
let originalRequest;
let originalSale;
2019-07-25 07:55:09 +00:00
let createdSaleId;
afterAll(async done => {
2019-10-22 11:44:36 +00:00
await originalRequest.updateAttributes(originalRequest);
await originalSale.updateAttributes(originalSale);
await app.models.Sale.destroyById(createdSaleId);
2019-07-25 07:55:09 +00:00
done();
});
it(`should throw an error if the item doesn't exist`, async() => {
let ctx = {req: {accessToken: {userId: 9}}, args: {itemFk: 999}};
let error;
try {
await app.models.TicketRequest.confirm(ctx);
} catch (err) {
error = err;
}
expect(error.message).toEqual(`That item doesn't exists`);
});
it(`should throw an error if the item is not available`, async() => {
const requestId = 5;
const itemId = 4;
2019-07-25 07:55:09 +00:00
const quantity = 99999;
let ctx = {req: {accessToken: {userId: 9}}, args: {
itemFk: itemId,
id: requestId,
quantity: quantity
}};
let error;
try {
await app.models.TicketRequest.confirm(ctx);
} catch (err) {
error = err;
}
expect(error.message).toEqual(`This item is not available`);
});
it(`should update the sale details if the request already contains a sale id`, async() => {
const requestId = 4;
const saleId = 11;
const itemId = 1;
const quantity = 10;
2019-10-22 11:44:36 +00:00
originalRequest = await app.models.TicketRequest.findById(requestId);
originalSale = await app.models.Sale.findById(saleId);
2019-07-25 07:55:09 +00:00
2019-10-22 11:44:36 +00:00
const request = await app.models.TicketRequest.findById(requestId);
await request.updateAttributes({saleFk: saleId});
2019-07-25 07:55:09 +00:00
let ctx = {req: {accessToken: {userId: 9}}, args: {
itemFk: itemId,
id: requestId,
quantity: quantity
}};
await app.models.TicketRequest.confirm(ctx);
let updatedSale = await app.models.Sale.findById(saleId);
expect(updatedSale.itemFk).toEqual(itemId);
expect(updatedSale.quantity).toEqual(quantity);
});
it(`should create a new sale for the the request if there's no sale id`, async() => {
const requestId = 4;
const itemId = 1;
const quantity = 10;
2019-10-22 11:44:36 +00:00
const request = await app.models.TicketRequest.findById(requestId);
await request.updateAttributes({saleFk: null});
2019-07-25 07:55:09 +00:00
let ctx = {req: {accessToken: {userId: 9}}, args: {
itemFk: itemId,
id: requestId,
quantity: quantity,
ticketFk: 1
}};
await app.models.TicketRequest.confirm(ctx);
let updatedRequest = await app.models.TicketRequest.findById(requestId);
createdSaleId = updatedRequest.saleFk;
expect(updatedRequest.saleFk).toEqual(createdSaleId);
expect(updatedRequest.isOk).toEqual(true);
expect(updatedRequest.itemFk).toEqual(itemId);
});
});