const { models } = require('vn-loopback/server/server'); const LoopBackContext = require('loopback-context'); // #6276 describe('ItemShelving upsertItem()', () => { const warehouseFk = 1; let ctx; let options; let tx; beforeEach(async () => { ctx = { req: { accessToken: { userId: 9 }, headers: { origin: 'http://localhost' } }, args: {} }; spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ active: ctx.req }); options = { transaction: tx }; tx = await models.ItemShelving.beginTransaction({}); options.transaction = tx; }); afterEach(async () => { await tx.rollback(); }); it('should add two new records', async () => { const shelvingFk = 'ZPP'; const items = [1, 1, 1, 2]; await models.ItemShelving.upsertItem(ctx, shelvingFk, items, warehouseFk, options); const itemShelvings = await models.ItemShelving.find({ where: { shelvingFk } }, options); expect(itemShelvings.length).toEqual(2); }); it('should update the visible items', async () => { const shelvingFk = 'GVC'; const items = [2, 2]; const { visible: visibleItemsBefore } = await models.ItemShelving.findOne({ where: { shelvingFk, itemFk: items[0] } }, options); await models.ItemShelving.upsertItem(ctx, shelvingFk, items, warehouseFk, options); const { visible: visibleItemsAfter } = await models.ItemShelving.findOne({ where: { shelvingFk, itemFk: items[0] } }, options); expect(visibleItemsAfter).toEqual(visibleItemsBefore + 2); }); });