salix/back/methods/starred-module/specs/setPosition.spec.js

224 lines
8.0 KiB
JavaScript

const {models} = 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 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;
}
});
});