Merge pull request #16298 from strapi/fix/out-of-range-value-when-reordering-relation

This commit is contained in:
Marc 2023-04-13 17:47:10 +02:00 committed by GitHub
commit 9542f27484
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -121,6 +121,28 @@ describe('Given I have some relations in the database', () => {
]);
});
});
describe('When you connect a relation before one with null order', () => {
test('Then it replaces null order values to 1 and properly reorders relations', () => {
const orderer = relationsOrderer(
[
{ id: 2, order: null },
{ id: 3, order: null },
],
'id',
'order'
);
orderer.connect([{ id: 4, position: { before: 3 } }, { id: 5 }]);
expect(orderer.get()).toMatchObject([
{ id: 2, order: 1 },
{ id: 4, order: 0.5 },
{ id: 3, order: 1 },
{ id: 5, order: 1.5 },
]);
});
});
});
describe('Given there are no relations in the database', () => {

View File

@ -135,7 +135,7 @@ const relationsOrderer = (initArr, idColumn, orderColumn, strict) => {
const computedRelations = _.castArray(initArr || []).map((r) => ({
init: true,
id: r[idColumn],
order: r[orderColumn],
order: r[orderColumn] || 1,
}));
const maxOrder = _.maxBy('order', computedRelations)?.order || 0;