From 75f8858c3a2d3cda786ab40b773f7adcd981c4af Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Tue, 23 Apr 2019 13:41:26 +0200 Subject: [PATCH] fix(core): Fix reordering of collections Fixes #75 --- packages/core/e2e/collection.e2e-spec.ts | 19 ++++++++++++++++--- .../service/services/collection.service.ts | 14 ++++++++++---- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/packages/core/e2e/collection.e2e-spec.ts b/packages/core/e2e/collection.e2e-spec.ts index ca9bbd0eb6..892210ccc3 100644 --- a/packages/core/e2e/collection.e2e-spec.ts +++ b/packages/core/e2e/collection.e2e-spec.ts @@ -237,12 +237,12 @@ describe('Collection resolver', () => { ]); }); - it('alters the position in the current parent', async () => { + it('alters the position in the current parent 1', async () => { await client.query(MOVE_COLLECTION, { input: { - collectionId: pearCollection.id, + collectionId: computersCollection.id, parentId: electronicsCollection.id, - index: 1, + index: 0, }, }); @@ -250,6 +250,19 @@ describe('Collection resolver', () => { expect(afterResult.map((i: any) => i.id)).toEqual([computersCollection.id, pearCollection.id]); }); + it('alters the position in the current parent 2', async () => { + await client.query(MOVE_COLLECTION, { + input: { + collectionId: pearCollection.id, + parentId: electronicsCollection.id, + index: 0, + }, + }); + + const afterResult = await getChildrenOf(electronicsCollection.id); + expect(afterResult.map((i: any) => i.id)).toEqual([pearCollection.id, computersCollection.id]); + }); + it('corrects an out-of-bounds negative index value', async () => { await client.query(MOVE_COLLECTION, { input: { diff --git a/packages/core/src/service/services/collection.service.ts b/packages/core/src/service/services/collection.service.ts index 3b38c8af2d..ee4b9fee0d 100644 --- a/packages/core/src/service/services/collection.service.ts +++ b/packages/core/src/service/services/collection.service.ts @@ -275,15 +275,21 @@ export class CollectionService implements OnModuleInit { const currentIndex = siblings.findIndex(cat => idsAreEqual(cat.id, input.collectionId)); if (currentIndex !== normalizedIndex) { siblings.splice(normalizedIndex, 0, siblings.splice(currentIndex, 1)[0]); - siblings.forEach((cat, index) => { - cat.position = index; + siblings.forEach((collection, index) => { + collection.position = index; + if (target.id === collection.id) { + target.position = index; + } }); } } else { target.parent = new Collection({ id: input.parentId }); siblings.splice(normalizedIndex, 0, target); - siblings.forEach((cat, index) => { - cat.position = index; + siblings.forEach((collection, index) => { + collection.position = index; + if (target.id === collection.id) { + target.position = index; + } }); }