const app = require('vn-loopback/server/server'); describe('updateCollectionSale()', () => { it('should return a new collection', async() => { const sectorOneWarehouseID = 1; let ctx = {req: {accessToken: {userId: 1106}}}; ctx.args = { sale: 1, originalQuantity: 5, quantity: 5, quantityPicked: 5, ticketFk: 1, stateFk: 4, isNicho: false, shelvingFk: 'UXN', itemFk: 1, sectorFk: 1 }; let originalSaleTracking = await app.models.SaleTracking.findOne({ where: { saleFk: ctx.args.sale, stateFk: ctx.args.stateFk } }); let itemPlacement = await app.models.ItemPlacement.findOne({ where: { itemFk: ctx.args.itemFk, warehouseFk: sectorOneWarehouseID } }); const originalSale = await app.models.Sale.findById(ctx.args.sale); const originalItemShelving = await app.models.ItemShelving.findOne({where: {shelvingFk: ctx.args.shelvingFk, itemFk: ctx.args.itemFk}}); const originalTicketLastState = await app.models.TicketLastState.findById(ctx.args.ticketFk); let response = await app.models.Collection.updateCollectionSale(ctx); expect(response.length).toBeGreaterThan(0); expect(response[0][0].id).toEqual(1); expect(response[0][0].quantity).toEqual(5); // restores if (originalSaleTracking) await originalSaleTracking.save(); else { originalSaleTracking = await app.models.SaleTracking.findOne({ where: { saleFk: ctx.args.sale, stateFk: ctx.args.stateFk } }); await originalSaleTracking.destroy(); } await originalSale.save(); const itemShelvingsToDestroy = await app.models.ItemShelving.find({where: {shelvingFk: ctx.args.shelvingFk, itemFk: ctx.args.itemFk}}); for (itemShelving of itemShelvingsToDestroy) await itemShelving.destroy(); await originalItemShelving.save(); await originalTicketLastState.save(); await itemPlacement.save(); }); });