2021-06-01 20:18:13 +00:00
|
|
|
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,
|
2022-12-14 13:20:41 +00:00
|
|
|
moduleFk: 'order'
|
2021-06-01 20:18:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
2022-12-14 13:20:41 +00:00
|
|
|
await app.models.StarredModule.toggleStarredModule(ctx, 'order', options);
|
|
|
|
await app.models.StarredModule.toggleStarredModule(ctx, 'customer', options);
|
2021-06-01 20:18:13 +00:00
|
|
|
|
|
|
|
let orders = await app.models.StarredModule.findOne(filter, options);
|
|
|
|
|
2022-12-14 13:20:41 +00:00
|
|
|
filter.where.moduleFk = 'customer';
|
2021-06-01 20:18:13 +00:00
|
|
|
let clients = await app.models.StarredModule.findOne(filter, options);
|
|
|
|
|
|
|
|
expect(orders.position).toEqual(1);
|
|
|
|
expect(clients.position).toEqual(2);
|
|
|
|
|
2022-12-14 13:20:41 +00:00
|
|
|
await app.models.StarredModule.setPosition(ctx, 'customer', 'left', options);
|
2021-06-01 20:18:13 +00:00
|
|
|
|
2022-12-14 13:20:41 +00:00
|
|
|
filter.where.moduleFk = 'customer';
|
2021-06-01 20:18:13 +00:00
|
|
|
clients = await app.models.StarredModule.findOne(filter, options);
|
|
|
|
|
2022-12-14 13:20:41 +00:00
|
|
|
filter.where.moduleFk = 'order';
|
2021-06-01 20:18:13 +00:00
|
|
|
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,
|
2022-12-14 13:20:41 +00:00
|
|
|
moduleFk: 'order'
|
2021-06-01 20:18:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
2022-12-14 13:20:41 +00:00
|
|
|
await app.models.StarredModule.toggleStarredModule(ctx, 'order', options);
|
|
|
|
await app.models.StarredModule.toggleStarredModule(ctx, 'customer', options);
|
2021-06-01 20:18:13 +00:00
|
|
|
|
|
|
|
let orders = await app.models.StarredModule.findOne(filter, options);
|
|
|
|
|
2022-12-14 13:20:41 +00:00
|
|
|
filter.where.moduleFk = 'customer';
|
2021-06-01 20:18:13 +00:00
|
|
|
let clients = await app.models.StarredModule.findOne(filter, options);
|
|
|
|
|
|
|
|
expect(orders.position).toEqual(1);
|
|
|
|
expect(clients.position).toEqual(2);
|
|
|
|
|
2022-12-14 13:20:41 +00:00
|
|
|
await app.models.StarredModule.setPosition(ctx, 'order', 'right', options);
|
2021-06-01 20:18:13 +00:00
|
|
|
|
2022-12-14 13:20:41 +00:00
|
|
|
filter.where.moduleFk = 'order';
|
2021-06-01 20:18:13 +00:00
|
|
|
orders = await app.models.StarredModule.findOne(filter, options);
|
|
|
|
|
2022-12-14 13:20:41 +00:00
|
|
|
filter.where.moduleFk = 'customer';
|
2021-06-01 20:18:13 +00:00
|
|
|
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,
|
2022-12-14 13:20:41 +00:00
|
|
|
moduleFk: 'item'
|
2021-06-01 20:18:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
2022-12-14 13:20:41 +00:00
|
|
|
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);
|
2021-06-01 20:18:13 +00:00
|
|
|
|
|
|
|
const items = await app.models.StarredModule.findOne(filter, options);
|
|
|
|
|
2022-12-14 13:20:41 +00:00
|
|
|
filter.where.moduleFk = 'claim';
|
2021-06-01 20:18:13 +00:00
|
|
|
const claims = await app.models.StarredModule.findOne(filter, options);
|
|
|
|
|
2022-12-14 13:20:41 +00:00
|
|
|
filter.where.moduleFk = 'customer';
|
2021-06-01 20:18:13 +00:00
|
|
|
let clients = await app.models.StarredModule.findOne(filter, options);
|
|
|
|
|
2022-12-14 13:20:41 +00:00
|
|
|
filter.where.moduleFk = 'order';
|
2021-06-01 20:18:13 +00:00
|
|
|
let orders = await app.models.StarredModule.findOne(filter, options);
|
|
|
|
|
2022-12-14 13:20:41 +00:00
|
|
|
filter.where.moduleFk = 'zone';
|
2021-06-01 20:18:13 +00:00
|
|
|
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);
|
|
|
|
|
2022-12-14 13:20:41 +00:00
|
|
|
await app.models.StarredModule.setPosition(ctx, 'customer', 'right', options);
|
2021-06-01 20:18:13 +00:00
|
|
|
|
2022-12-14 13:20:41 +00:00
|
|
|
filter.where.moduleFk = 'order';
|
2021-06-01 20:18:13 +00:00
|
|
|
orders = await app.models.StarredModule.findOne(filter, options);
|
|
|
|
|
2022-12-14 13:20:41 +00:00
|
|
|
filter.where.moduleFk = 'customer';
|
2021-06-01 20:18:13 +00:00
|
|
|
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,
|
2022-12-14 13:20:41 +00:00
|
|
|
moduleFk: 'item'
|
2021-06-01 20:18:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
2022-12-14 13:20:41 +00:00
|
|
|
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);
|
2021-06-01 20:18:13 +00:00
|
|
|
|
|
|
|
const items = await app.models.StarredModule.findOne(filter, options);
|
|
|
|
|
2022-12-14 13:20:41 +00:00
|
|
|
filter.where.moduleFk = 'customer';
|
2021-06-01 20:18:13 +00:00
|
|
|
let clients = await app.models.StarredModule.findOne(filter, options);
|
|
|
|
|
2022-12-14 13:20:41 +00:00
|
|
|
filter.where.moduleFk = 'claim';
|
2021-06-01 20:18:13 +00:00
|
|
|
const claims = await app.models.StarredModule.findOne(filter, options);
|
|
|
|
|
2022-12-14 13:20:41 +00:00
|
|
|
filter.where.moduleFk = 'order';
|
2021-06-01 20:18:13 +00:00
|
|
|
let orders = await app.models.StarredModule.findOne(filter, options);
|
|
|
|
|
2022-12-14 13:20:41 +00:00
|
|
|
filter.where.moduleFk = 'zone';
|
2021-06-01 20:18:13 +00:00
|
|
|
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);
|
|
|
|
|
2022-12-14 13:20:41 +00:00
|
|
|
await app.models.StarredModule.toggleStarredModule(ctx, 'claim', options);
|
|
|
|
await app.models.StarredModule.setPosition(ctx, 'customer', 'right', options);
|
2021-06-01 20:18:13 +00:00
|
|
|
|
2022-12-14 13:20:41 +00:00
|
|
|
filter.where.moduleFk = 'customer';
|
2021-06-01 20:18:13 +00:00
|
|
|
clients = await app.models.StarredModule.findOne(filter, options);
|
|
|
|
|
2022-12-14 13:20:41 +00:00
|
|
|
filter.where.moduleFk = 'order';
|
2021-06-01 20:18:13 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|