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

91 lines
2.4 KiB
JavaScript

const models = require('vn-loopback/server/server').models;
describe('ticket-request confirm()', () => {
const ctx = beforeAll.getCtx();
beforeAll.mockLoopBackContext();
it(`should throw an error if the item doesn't exist`, async() => {
const tx = await models.TicketRequest.beginTransaction({});
let error;
try {
const options = {transaction: tx};
ctx.args = {itemFk: 999};
await models.TicketRequest.confirm(ctx, options);
await tx.rollback();
} catch (e) {
await tx.rollback();
error = e;
}
expect(error.message).toEqual(`That item doesn't exists`);
});
it('should throw an error if the item is not available', async() => {
const tx = await models.TicketRequest.beginTransaction({});
let error;
try {
const options = {transaction: tx};
const requestId = 5;
const itemId = 4;
const quantity = 99999;
ctx.args = {
itemFk: itemId,
id: requestId,
quantity: quantity
};
await models.TicketRequest.confirm(ctx, options);
await tx.rollback();
} catch (e) {
await tx.rollback();
error = e;
}
expect(error.message).toEqual(`This item is not available`);
});
it(`should throw if there's a sale id`, async() => {
const tx = await models.TicketRequest.beginTransaction({});
let error;
try {
const options = {transaction: tx};
const requestId = 4;
const itemId = 1;
const quantity = 10;
ctx.args = {
itemFk: itemId,
id: requestId,
quantity: quantity
};
const request = await models.TicketRequest.findById(requestId, null, options);
expect(request.saleFk).toBeNull();
await request.updateAttributes({saleFk: 2});
ctx.args = {
itemFk: itemId,
id: requestId,
quantity: quantity
};
await models.TicketRequest.confirm(ctx, options);
await tx.rollback();
} catch (e) {
await tx.rollback();
error = e;
}
expect(error.message).toEqual(`This request already contains a sale`);
});
});