const app = require('vn-loopback/server/server'); describe('ticket-request confirm()', () => { 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() => { ctx.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; const quantity = 99999; ctx.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 throw if there's a sale id`, async() => { const requestId = 4; const itemId = 1; const quantity = 10; ctx.args = { itemFk: itemId, id: requestId, quantity: quantity }; const request = await app.models.TicketRequest.findById(requestId); expect(request.saleFk).toBeNull(); await request.updateAttributes({saleFk: 2}); ctx.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 request already contains a sale`); // restores await request.updateAttributes({saleFk: null}); }); 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; const originalRequest = await app.models.TicketRequest.findById(requestId); ctx.args = { itemFk: itemId, id: requestId, quantity: quantity }; const request = await app.models.TicketRequest.findById(requestId); await request.updateAttributes({saleFk: null}); await app.models.TicketRequest.confirm(ctx); const updatedRequest = await app.models.TicketRequest.findById(requestId); const createdSaleId = updatedRequest.saleFk; expect(updatedRequest.saleFk).toEqual(createdSaleId); expect(updatedRequest.isOk).toEqual(true); expect(updatedRequest.itemFk).toEqual(itemId); // restores await originalRequest.updateAttributes(originalRequest); await app.models.Sale.destroyById(createdSaleId); await app.models.Sale.destroyById(createdSaleId); await app.models.Item.rawSql(` TRUNCATE TABLE cache.last_buy `); }); });