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

208 lines
7.6 KiB
JavaScript
Raw Permalink Normal View History

2023-01-24 08:04:43 +00:00
const {models} = require('vn-loopback/server/server');
2021-06-01 20:18:13 +00:00
describe('setPosition()', () => {
2024-06-14 06:39:57 +00:00
const ctx = beforeAll.getCtx();
2024-06-14 06:57:11 +00:00
beforeAll.mockLoopBackContext();
2021-06-01 20:18:13 +00:00
it('should increase the orders module position by replacing it with clients and vice versa', async() => {
2023-01-24 08:04:43 +00:00
const tx = await models.StarredModule.beginTransaction({});
2021-06-01 20:18:13 +00:00
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};
2023-01-24 08:04:43 +00:00
await models.StarredModule.toggleStarredModule(ctx, 'order', options);
await models.StarredModule.toggleStarredModule(ctx, 'customer', options);
2021-06-01 20:18:13 +00:00
2023-01-24 08:04:43 +00:00
let orders = await models.StarredModule.findOne(filter, options);
2021-06-01 20:18:13 +00:00
2022-12-14 13:20:41 +00:00
filter.where.moduleFk = 'customer';
2023-01-24 08:04:43 +00:00
let clients = await models.StarredModule.findOne(filter, options);
2021-06-01 20:18:13 +00:00
expect(orders.position).toEqual(1);
expect(clients.position).toEqual(2);
2023-01-24 08:04:43 +00:00
await 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';
2023-01-24 08:04:43 +00:00
clients = await models.StarredModule.findOne(filter, options);
2021-06-01 20:18:13 +00:00
2022-12-14 13:20:41 +00:00
filter.where.moduleFk = 'order';
2023-01-24 08:04:43 +00:00
orders = await models.StarredModule.findOne(filter, options);
2021-06-01 20:18:13 +00:00
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() => {
2023-01-24 08:04:43 +00:00
const tx = await models.StarredModule.beginTransaction({});
2021-06-01 20:18:13 +00:00
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};
2023-01-24 08:04:43 +00:00
await models.StarredModule.toggleStarredModule(ctx, 'order', options);
await models.StarredModule.toggleStarredModule(ctx, 'customer', options);
2021-06-01 20:18:13 +00:00
2023-01-24 08:04:43 +00:00
let orders = await models.StarredModule.findOne(filter, options);
2021-06-01 20:18:13 +00:00
2022-12-14 13:20:41 +00:00
filter.where.moduleFk = 'customer';
2023-01-24 08:04:43 +00:00
let clients = await models.StarredModule.findOne(filter, options);
2021-06-01 20:18:13 +00:00
expect(orders.position).toEqual(1);
expect(clients.position).toEqual(2);
2023-01-24 08:04:43 +00:00
await 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';
2023-01-24 08:04:43 +00:00
orders = await models.StarredModule.findOne(filter, options);
2021-06-01 20:18:13 +00:00
2022-12-14 13:20:41 +00:00
filter.where.moduleFk = 'customer';
2023-01-24 08:04:43 +00:00
clients = await models.StarredModule.findOne(filter, options);
2021-06-01 20:18:13 +00:00
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() => {
2023-01-24 08:04:43 +00:00
const tx = await models.StarredModule.beginTransaction({});
2021-06-01 20:18:13 +00:00
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};
2023-01-24 08:04:43 +00:00
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);
2021-06-01 20:18:13 +00:00
2023-01-24 08:04:43 +00:00
const items = await models.StarredModule.findOne(filter, options);
2021-06-01 20:18:13 +00:00
2022-12-14 13:20:41 +00:00
filter.where.moduleFk = 'claim';
2023-01-24 08:04:43 +00:00
const claims = await models.StarredModule.findOne(filter, options);
2021-06-01 20:18:13 +00:00
2022-12-14 13:20:41 +00:00
filter.where.moduleFk = 'customer';
2023-01-24 08:04:43 +00:00
let clients = await models.StarredModule.findOne(filter, options);
2021-06-01 20:18:13 +00:00
2022-12-14 13:20:41 +00:00
filter.where.moduleFk = 'order';
2023-01-24 08:04:43 +00:00
let orders = await models.StarredModule.findOne(filter, options);
2021-06-01 20:18:13 +00:00
2022-12-14 13:20:41 +00:00
filter.where.moduleFk = 'zone';
2023-01-24 08:04:43 +00:00
const zones = await models.StarredModule.findOne(filter, options);
2021-06-01 20:18:13 +00:00
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);
2023-01-24 08:04:43 +00:00
await 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';
2023-01-24 08:04:43 +00:00
orders = await models.StarredModule.findOne(filter, options);
2021-06-01 20:18:13 +00:00
2022-12-14 13:20:41 +00:00
filter.where.moduleFk = 'customer';
2023-01-24 08:04:43 +00:00
clients = await models.StarredModule.findOne(filter, options);
2021-06-01 20:18:13 +00:00
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() => {
2023-01-24 08:04:43 +00:00
const tx = await models.StarredModule.beginTransaction({});
2021-06-01 20:18:13 +00:00
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};
2023-01-24 08:04:43 +00:00
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);
2021-06-01 20:18:13 +00:00
2023-01-24 08:04:43 +00:00
const items = await models.StarredModule.findOne(filter, options);
2021-06-01 20:18:13 +00:00
2022-12-14 13:20:41 +00:00
filter.where.moduleFk = 'customer';
2023-01-24 08:04:43 +00:00
let clients = await models.StarredModule.findOne(filter, options);
2021-06-01 20:18:13 +00:00
2022-12-14 13:20:41 +00:00
filter.where.moduleFk = 'claim';
2023-01-24 08:04:43 +00:00
const claims = await models.StarredModule.findOne(filter, options);
2021-06-01 20:18:13 +00:00
2022-12-14 13:20:41 +00:00
filter.where.moduleFk = 'order';
2023-01-24 08:04:43 +00:00
let orders = await models.StarredModule.findOne(filter, options);
2021-06-01 20:18:13 +00:00
2022-12-14 13:20:41 +00:00
filter.where.moduleFk = 'zone';
2023-01-24 08:04:43 +00:00
const zones = await models.StarredModule.findOne(filter, options);
2021-06-01 20:18:13 +00:00
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);
2023-01-24 08:04:43 +00:00
await models.StarredModule.toggleStarredModule(ctx, 'claim', options);
await 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';
2023-01-24 08:04:43 +00:00
clients = await models.StarredModule.findOne(filter, options);
2021-06-01 20:18:13 +00:00
2022-12-14 13:20:41 +00:00
filter.where.moduleFk = 'order';
2023-01-24 08:04:43 +00:00
orders = await models.StarredModule.findOne(filter, options);
2021-06-01 20:18:13 +00:00
expect(orders.position).toEqual(2);
expect(clients.position).toEqual(4);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});