mirror of
https://github.com/strapi/strapi.git
synced 2025-11-01 18:33:55 +00:00
test relation orderer
This commit is contained in:
parent
0f330d1410
commit
5106ed251e
@ -0,0 +1,68 @@
|
||||
'use strict';
|
||||
|
||||
const RelationsOrderer = require('../relations-orderer');
|
||||
|
||||
describe('relations orderer', () => {
|
||||
test('connect at the end', () => {
|
||||
const orderer = new RelationsOrderer(
|
||||
[
|
||||
{ id: 2, order: 4 },
|
||||
{ id: 3, order: 10 },
|
||||
],
|
||||
'id',
|
||||
'order'
|
||||
);
|
||||
|
||||
orderer.connect([{ id: 4, position: { end: true } }, { id: 5 }]);
|
||||
|
||||
expect(orderer.arr).toMatchObject([
|
||||
{ id: 2, order: 4 },
|
||||
{ id: 3, order: 10 },
|
||||
{ id: 4, order: 10.5 },
|
||||
{ id: 5, order: 10.5 },
|
||||
]);
|
||||
});
|
||||
|
||||
test('connect at the start', () => {
|
||||
const orderer = new RelationsOrderer(
|
||||
[
|
||||
{ id: 2, order: 4 },
|
||||
{ id: 3, order: 10 },
|
||||
],
|
||||
'id',
|
||||
'order'
|
||||
);
|
||||
|
||||
orderer.connect([{ id: 4, position: { start: true } }]);
|
||||
|
||||
expect(orderer.arr).toMatchObject([
|
||||
{ id: 4, order: 0.5 },
|
||||
{ id: 2, order: 4 },
|
||||
{ id: 3, order: 10 },
|
||||
]);
|
||||
});
|
||||
|
||||
test('connect multiple relations', () => {
|
||||
const orderer = new RelationsOrderer(
|
||||
[
|
||||
{ id: 2, order: 4 },
|
||||
{ id: 3, order: 10 },
|
||||
],
|
||||
'id',
|
||||
'order'
|
||||
);
|
||||
|
||||
orderer.connect([
|
||||
{ id: 4, position: { before: 2 } },
|
||||
{ id: 4, position: { before: 3 } },
|
||||
{ id: 5, position: { before: 4 } },
|
||||
]);
|
||||
|
||||
expect(orderer.arr).toMatchObject([
|
||||
{ id: 2, order: 4 },
|
||||
{ id: 5, order: 9.5 },
|
||||
{ id: 4, order: 9.5 },
|
||||
{ id: 3, order: 10 },
|
||||
]);
|
||||
});
|
||||
});
|
||||
@ -38,7 +38,7 @@ const {
|
||||
deleteRelations,
|
||||
cleanOrderColumns,
|
||||
} = require('./regular-relations');
|
||||
const FractionalOrderer = require('./relationsOrderer');
|
||||
const FractionalOrderer = require('./relations-orderer');
|
||||
|
||||
const toId = (value) => value.id || value;
|
||||
const toIds = (value) => castArray(value || []).map(toId);
|
||||
|
||||
@ -29,11 +29,11 @@ const _ = require('lodash/fp');
|
||||
* in the positional attributes & the last relation in the database.
|
||||
*
|
||||
*/
|
||||
class FractionalOrderer {
|
||||
class RelationsOrderer {
|
||||
/**
|
||||
* @param {*} initArr - array of relations to initialize the class with
|
||||
* @param {*} idColumn - the column name of the id
|
||||
* @param {*} orderColumn - the column name of the order
|
||||
* @param {Array<*>} initArr - array of relations to initialize the class with
|
||||
* @param {string} idColumn - the column name of the id
|
||||
* @param {string} orderColumn - the column name of the order
|
||||
*/
|
||||
constructor(initArr, idColumn, orderColumn) {
|
||||
this.arr = _.castArray(initArr || []).map((r) => ({
|
||||
@ -45,17 +45,17 @@ class FractionalOrderer {
|
||||
|
||||
_updateRelationOrder(r) {
|
||||
let idx;
|
||||
if (r.position.before) {
|
||||
if (r.position?.before) {
|
||||
const { idx: _idx, relation } = this.findRelation(r.position.before);
|
||||
if (relation.init) r.order = relation.order - 0.5;
|
||||
else r.order = relation.order;
|
||||
idx = _idx;
|
||||
} else if (r.position.after) {
|
||||
} else if (r.position?.after) {
|
||||
const { idx: _idx, relation } = this.findRelation(r.position.after);
|
||||
if (relation.init) r.order = relation.order + 0.5;
|
||||
else r.order = relation.order;
|
||||
idx = _idx + 1;
|
||||
} else if (r.position.start) {
|
||||
} else if (r.position?.start) {
|
||||
r.order = 0.5;
|
||||
idx = 0;
|
||||
} else {
|
||||
@ -120,4 +120,4 @@ class FractionalOrderer {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = FractionalOrderer;
|
||||
module.exports = RelationsOrderer;
|
||||
Loading…
x
Reference in New Issue
Block a user