0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

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
This commit is contained in:
Naz 2023-06-01 12:37:51 +07:00 committed by naz
parent 5d16425428
commit a0a7c3a61b
2 changed files with 25 additions and 13 deletions

View file

@ -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<CollectionDTO> {
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<Collection | null> {
async edit(data: any): Promise<CollectionDTO | null> {
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<Collection | null> {

View file

@ -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');
});
});
});