const app = require('vn-loopback/server/server'); const LoopBackContext = require('loopback-context'); describe('setPosition()', () => { const activeCtx = { accessToken: {userId: 9}, http: { req: { headers: {origin: 'http://localhost'} } } }; const ctx = { req: activeCtx }; beforeEach(() => { spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ active: activeCtx }); }); it('should increase the orders module position by replacing it with clients and vice versa', async() => { const tx = await app.models.StarredModule.beginTransaction({}); const filter = { where: { workerFk: ctx.req.accessToken.userId, moduleFk: 'order' } }; try { const options = {transaction: tx}; await app.models.StarredModule.toggleStarredModule(ctx, 'order', options); await app.models.StarredModule.toggleStarredModule(ctx, 'customer', options); let orders = await app.models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'customer'; let clients = await app.models.StarredModule.findOne(filter, options); expect(orders.position).toEqual(1); expect(clients.position).toEqual(2); await app.models.StarredModule.setPosition(ctx, 'customer', 'left', options); filter.where.moduleFk = 'customer'; clients = await app.models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'order'; orders = await app.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 app.models.StarredModule.beginTransaction({}); const filter = { where: { workerFk: ctx.req.accessToken.userId, moduleFk: 'order' } }; try { const options = {transaction: tx}; await app.models.StarredModule.toggleStarredModule(ctx, 'order', options); await app.models.StarredModule.toggleStarredModule(ctx, 'customer', options); let orders = await app.models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'customer'; let clients = await app.models.StarredModule.findOne(filter, options); expect(orders.position).toEqual(1); expect(clients.position).toEqual(2); await app.models.StarredModule.setPosition(ctx, 'order', 'right', options); filter.where.moduleFk = 'order'; orders = await app.models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'customer'; clients = await app.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 app.models.StarredModule.beginTransaction({}); const filter = { where: { workerFk: ctx.req.accessToken.userId, moduleFk: 'item' } }; try { const options = {transaction: tx}; await app.models.StarredModule.toggleStarredModule(ctx, 'customer', options); await app.models.StarredModule.toggleStarredModule(ctx, 'order', options); await app.models.StarredModule.toggleStarredModule(ctx, 'customer', options); await app.models.StarredModule.toggleStarredModule(ctx, 'order', options); await app.models.StarredModule.toggleStarredModule(ctx, 'item', options); await app.models.StarredModule.toggleStarredModule(ctx, 'claim', options); await app.models.StarredModule.toggleStarredModule(ctx, 'customer', options); await app.models.StarredModule.toggleStarredModule(ctx, 'order', options); await app.models.StarredModule.toggleStarredModule(ctx, 'zone', options); const items = await app.models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'claim'; const claims = await app.models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'customer'; let clients = await app.models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'order'; let orders = await app.models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'zone'; const zones = await app.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 app.models.StarredModule.setPosition(ctx, 'customer', 'right', options); filter.where.moduleFk = 'order'; orders = await app.models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'customer'; clients = await app.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 app.models.StarredModule.beginTransaction({}); const filter = { where: { workerFk: ctx.req.accessToken.userId, moduleFk: 'item' } }; try { const options = {transaction: tx}; await app.models.StarredModule.toggleStarredModule(ctx, 'item', options); await app.models.StarredModule.toggleStarredModule(ctx, 'customer', options); await app.models.StarredModule.toggleStarredModule(ctx, 'claim', options); await app.models.StarredModule.toggleStarredModule(ctx, 'order', options); await app.models.StarredModule.toggleStarredModule(ctx, 'zone', options); const items = await app.models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'customer'; let clients = await app.models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'claim'; const claims = await app.models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'order'; let orders = await app.models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'zone'; const zones = await app.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 app.models.StarredModule.toggleStarredModule(ctx, 'claim', options); await app.models.StarredModule.setPosition(ctx, 'customer', 'right', options); filter.where.moduleFk = 'customer'; clients = await app.models.StarredModule.findOne(filter, options); filter.where.moduleFk = 'order'; orders = await app.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; } }); });