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

112 lines
2.9 KiB
JavaScript

const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context');
describe('ticket-request confirm()', () => {
beforeAll(async() => {
const activeCtx = {
accessToken: {userId: 9},
http: {
req: {
headers: {origin: 'http://localhost'}
}
}
};
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx
});
});
let ctx = {
req: {
accessToken: {userId: 9},
headers: {origin: 'http://localhost'}
}
};
ctx.req.__ = value => {
return value;
};
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`);
});
});