From a0a7c3a61ba9897bf63c4ec2b3c0ad9a02b218f4 Mon Sep 17 00:00:00 2001 From: Naz Date: Thu, 1 Jun 2023 12:37:51 +0700 Subject: [PATCH] Added coverage to all editable collection properties refs https://github.com/TryGhost/Team/issues/3260 - Made sure we have sufficient coverage for all editable properties of the collection to avoid sneaky bugs --- ghost/collections/src/CollectionsService.ts | 28 +++++++++++++-------- ghost/collections/test/collections.test.ts | 10 ++++++-- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/ghost/collections/src/CollectionsService.ts b/ghost/collections/src/CollectionsService.ts index 6bcb69302d..31f5028b66 100644 --- a/ghost/collections/src/CollectionsService.ts +++ b/ghost/collections/src/CollectionsService.ts @@ -15,7 +15,7 @@ type ManualCollection = { type: 'manual'; slug?: string; description?: string; - featureImage?: string; + feature_image?: string; filter?: null; }; @@ -25,7 +25,7 @@ type AutomaticCollection = { filter: string; slug?: string; description?: string; - featureImage?: string; + feature_image?: string; }; type CollectionInputDTO = ManualCollection | AutomaticCollection; @@ -73,6 +73,16 @@ export class CollectionsService { }; } + fromDTO(data: any): any { + return { + title: data.title, + slug: data.slug, + description: data.description, + featureImage: data.feature_image, + filter: data.filter + }; + } + async createCollection(data: CollectionInputDTO): Promise { const collection = await Collection.create({ title: data.title, @@ -80,7 +90,7 @@ export class CollectionsService { description: data.description, type: data.type, filter: data.filter, - featureImage: data.featureImage + featureImage: data.feature_image }); await this.collectionsRepository.save(collection); @@ -102,7 +112,7 @@ export class CollectionsService { return this.toDTO(collection); } - async edit(data: any): Promise { + async edit(data: any): Promise { const collection = await this.collectionsRepository.getById(data.id); if (!collection) { @@ -115,17 +125,13 @@ export class CollectionsService { } } - if (data.title) { - collection.title = data.title; - } + const collectionData = this.fromDTO(data); - if (data.description) { - collection.description = data.description; - } + Object.assign(collection, collectionData); await this.collectionsRepository.save(collection); - return collection; + return this.toDTO(collection); } async getById(id: string): Promise { diff --git a/ghost/collections/test/collections.test.ts b/ghost/collections/test/collections.test.ts index 449384a1d5..ad3e3b7f9f 100644 --- a/ghost/collections/test/collections.test.ts +++ b/ghost/collections/test/collections.test.ts @@ -70,10 +70,15 @@ describe('CollectionsService', function () { const editedCollection = await collectionsService.edit({ id: savedCollection.id, - description: 'Edited description' + title: 'Edited title', + description: 'Edited description', + feature_image: '/assets/images/edited.jpg' }); + assert.equal(editedCollection?.title, 'Edited title', 'Collection title should be edited'); assert.equal(editedCollection?.description, 'Edited description', 'Collection description should be edited'); + assert.equal(editedCollection?.feature_image, '/assets/images/edited.jpg', 'Collection feature_image should be edited'); + assert.equal(editedCollection?.type, 'manual', 'Collection type should not be edited'); }); it('Resolves to null when editing unexistend collection', async function () { @@ -99,7 +104,8 @@ describe('CollectionsService', function () { }); assert.equal(editedCollection?.posts.length, 1, 'Collection should have one post'); - assert.equal(editedCollection?.posts[0], posts[0].id, 'Collection should have the correct post'); + assert.equal(editedCollection?.posts[0].id, posts[0].id, 'Collection should have the correct post'); + assert.equal(editedCollection?.posts[0].sort_order, 0, 'Collection should have the correct post sort order'); }); }); });