const {models} = require('vn-loopback/server/server'); describe('setPosition()', () => { const ctx = beforeAll.getCtx(); beforeAll.mockLoopBackContext(); it('should increase the orders module position by replacing it with clients and vice versa', async() => { const tx = await models.StarredModule.beginTransaction({}); const filter = { where: { workerFk: ctx.req.accessToken.userId, moduleFk: 'order' } }; try { const options = {transaction: tx}; await models.StarredModule.toggleStarredModule(ctx, 'order', options); await models.StarredModule.toggleStarredModule(ctx, 'customer', options); let orders = await models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'customer'; let clients = await models.StarredModule.findOne(filter, options); expect(orders.position).toEqual(1); expect(clients.position).toEqual(2); await models.StarredModule.setPosition(ctx, 'customer', 'left', options); filter.where.moduleFk = 'customer'; clients = await models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'order'; orders = await models.StarredModule.findOne(filter, options); expect(clients.position).toEqual(1); expect(orders.position).toEqual(2); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); it('should decrease the orders module position by replacing it with clients and vice versa', async() => { const tx = await models.StarredModule.beginTransaction({}); const filter = { where: { workerFk: ctx.req.accessToken.userId, moduleFk: 'order' } }; try { const options = {transaction: tx}; await models.StarredModule.toggleStarredModule(ctx, 'order', options); await models.StarredModule.toggleStarredModule(ctx, 'customer', options); let orders = await models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'customer'; let clients = await models.StarredModule.findOne(filter, options); expect(orders.position).toEqual(1); expect(clients.position).toEqual(2); await models.StarredModule.setPosition(ctx, 'order', 'right', options); filter.where.moduleFk = 'order'; orders = await models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'customer'; clients = await models.StarredModule.findOne(filter, options); expect(orders.position).toEqual(2); expect(clients.position).toEqual(1); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); it('should switch two modules after adding and deleting several modules', async() => { const tx = await models.StarredModule.beginTransaction({}); const filter = { where: { workerFk: ctx.req.accessToken.userId, moduleFk: 'item' } }; try { const options = {transaction: tx}; await models.StarredModule.toggleStarredModule(ctx, 'customer', options); await models.StarredModule.toggleStarredModule(ctx, 'order', options); await models.StarredModule.toggleStarredModule(ctx, 'customer', options); await models.StarredModule.toggleStarredModule(ctx, 'order', options); await models.StarredModule.toggleStarredModule(ctx, 'item', options); await models.StarredModule.toggleStarredModule(ctx, 'claim', options); await models.StarredModule.toggleStarredModule(ctx, 'customer', options); await models.StarredModule.toggleStarredModule(ctx, 'order', options); await models.StarredModule.toggleStarredModule(ctx, 'zone', options); const items = await models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'claim'; const claims = await models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'customer'; let clients = await models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'order'; let orders = await models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'zone'; const zones = await models.StarredModule.findOne(filter, options); expect(items.position).toEqual(1); expect(claims.position).toEqual(2); expect(clients.position).toEqual(3); expect(orders.position).toEqual(4); expect(zones.position).toEqual(5); await models.StarredModule.setPosition(ctx, 'customer', 'right', options); filter.where.moduleFk = 'order'; orders = await models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'customer'; clients = await models.StarredModule.findOne(filter, options); expect(orders.position).toEqual(3); expect(clients.position).toEqual(4); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); it('should switch two modules after adding and deleting a module between them', async() => { const tx = await models.StarredModule.beginTransaction({}); const filter = { where: { workerFk: ctx.req.accessToken.userId, moduleFk: 'item' } }; try { const options = {transaction: tx}; await models.StarredModule.toggleStarredModule(ctx, 'item', options); await models.StarredModule.toggleStarredModule(ctx, 'customer', options); await models.StarredModule.toggleStarredModule(ctx, 'claim', options); await models.StarredModule.toggleStarredModule(ctx, 'order', options); await models.StarredModule.toggleStarredModule(ctx, 'zone', options); const items = await models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'customer'; let clients = await models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'claim'; const claims = await models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'order'; let orders = await models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'zone'; const zones = await models.StarredModule.findOne(filter, options); expect(items.position).toEqual(1); expect(clients.position).toEqual(2); expect(claims.position).toEqual(3); expect(orders.position).toEqual(4); expect(zones.position).toEqual(5); await models.StarredModule.toggleStarredModule(ctx, 'claim', options); await models.StarredModule.setPosition(ctx, 'customer', 'right', options); filter.where.moduleFk = 'customer'; clients = await models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'order'; orders = await models.StarredModule.findOne(filter, options); expect(orders.position).toEqual(2); expect(clients.position).toEqual(4); await tx.rollback(); } catch (e) { await tx.rollback(); throw e; } }); });