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

115 lines
3.0 KiB
JavaScript
Raw Normal View History

const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context');
2019-07-25 07:55:09 +00:00
2020-10-30 09:45:55 +00:00
describe('ticket-request confirm()', () => {
beforeAll(async() => {
const activeCtx = {
accessToken: {userId: 9},
http: {
req: {
headers: {origin: 'http://localhost'}
}
}
};
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx
});
});
2020-01-23 12:31:07 +00:00
let ctx = {
req: {
accessToken: {userId: 9},
headers: {origin: 'http://localhost'}
}
};
ctx.req.__ = value => {
return value;
};
2019-07-25 07:55:09 +00:00
it(`should throw an error if the item doesn't exist`, async() => {
const tx = await models.TicketRequest.beginTransaction({});
2019-07-25 07:55:09 +00:00
2020-01-23 12:31:07 +00:00
let error;
2019-07-25 07:55:09 +00:00
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;
2019-07-25 07:55:09 +00:00
}
expect(error.message).toEqual(`That item doesn't exists`);
});
2020-02-27 11:31:38 +00:00
it('should throw an error if the item is not available', async() => {
const tx = await models.TicketRequest.beginTransaction({});
2020-01-23 12:31:07 +00:00
2019-07-25 07:55:09 +00:00
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;
2019-07-25 07:55:09 +00:00
}
expect(error.message).toEqual(`This item is not available`);
});
2020-10-02 07:39:58 +00:00
it(`should throw if there's a sale id`, async() => {
const tx = await models.TicketRequest.beginTransaction({});
2019-07-25 07:55:09 +00:00
let error;
try {
const options = {transaction: tx};
2020-01-23 12:31:07 +00:00
const requestId = 4;
const itemId = 1;
const quantity = 10;
2020-10-06 08:15:27 +00:00
ctx.args = {
itemFk: itemId,
id: requestId,
quantity: quantity
};
2020-10-06 08:15:27 +00:00
const request = await models.TicketRequest.findById(requestId, null, options);
2020-10-06 08:15:27 +00:00
expect(request.saleFk).toBeNull();
2020-10-06 08:15:27 +00:00
await request.updateAttributes({saleFk: 2}, options);
2019-07-25 07:55:09 +00:00
ctx.args = {
itemFk: itemId,
id: requestId,
quantity: quantity
};
await models.TicketRequest.confirm(ctx, options);
await tx.rollback();
} catch (e) {
await tx.rollback();
error = e;
2020-10-02 07:39:58 +00:00
}
2019-07-25 07:55:09 +00:00
2020-10-02 07:39:58 +00:00
expect(error.message).toEqual(`This request already contains a sale`);
2019-07-25 07:55:09 +00:00
});
});