Added test coverage for collection updates

refs https://github.com/TryGhost/Team/issues/3170

- When the collection filter is updated the collection's posts should be updated automatically.
This commit is contained in:
Naz 2023-06-05 16:22:51 +07:00
parent 67fdff479b
commit 3599cfdd7a
No known key found for this signature in database
3 changed files with 32 additions and 2 deletions

View File

@ -181,7 +181,7 @@ describe('CollectionsService', function () {
assert.equal(collection.type, 'automatic', 'Collection should be automatic'); assert.equal(collection.type, 'automatic', 'Collection should be automatic');
assert.equal(collection.filter, 'featured:true', 'Collection should have the correct filter'); assert.equal(collection.filter, 'featured:true', 'Collection should have the correct filter');
assert.equal(collection.posts.length, 1, 'Collection should have one post'); assert.equal(collection.posts.length, 2, 'Collection should have two posts');
}); });
it('Populates collection when the type is changed to automatic and filter is present', async function () { it('Populates collection when the type is changed to automatic and filter is present', async function () {
@ -200,8 +200,30 @@ describe('CollectionsService', function () {
filter: 'featured:true' filter: 'featured:true'
}); });
assert.equal(automaticCollection?.posts.length, 1, 'Collection should have one post'); assert.equal(automaticCollection?.posts.length, 2, 'Collection should have two featured post');
assert.equal(automaticCollection?.posts[0].id, 'post-3-featured', 'Collection should have the correct post'); assert.equal(automaticCollection?.posts[0].id, 'post-3-featured', 'Collection should have the correct post');
}); });
it('Updates the automatic collection posts when the filter is changed', async function () {
let collection = await collectionsService.createCollection({
title: 'I am automatic',
description: 'testing automatic collection',
type: 'automatic',
filter: 'featured:true'
});
assert.equal(collection?.type, 'automatic', 'Collection should be automatic');
assert.equal(collection?.posts.length, 2, 'Collection should have two featured post');
assert.equal(collection?.posts[0].id, 'post-3-featured', 'Collection should have the correct post');
assert.equal(collection?.posts[1].id, 'post-4-featured', 'Collection should have the correct post');
let updatedCollection = await collectionsService.edit({
id: collection.id,
filter: 'slug:post-2'
});
assert.equal(updatedCollection?.posts.length, 1, 'Collection should have one post');
assert.equal(updatedCollection?.posts[0].id, 'post-2', 'Collection should have the correct post');
});
}); });
}); });

View File

@ -2,6 +2,7 @@ import {InMemoryRepository} from '@tryghost/in-memory-repository';
type CollectionPost = { type CollectionPost = {
id: string; id: string;
slug: string;
featured: boolean; featured: boolean;
published_at: Date; published_at: Date;
deleted: boolean; deleted: boolean;
@ -11,6 +12,7 @@ export class PostsRepositoryInMemory extends InMemoryRepository<string, Collecti
protected toPrimitive(entity: CollectionPost): object { protected toPrimitive(entity: CollectionPost): object {
return { return {
id: entity.id, id: entity.id,
slug: entity.slug,
featured: entity.featured, featured: entity.featured,
published_at: entity.published_at published_at: entity.published_at
}; };

View File

@ -16,4 +16,10 @@ export const posts = [{
slug: 'featured-post-3', slug: 'featured-post-3',
featured: true, featured: true,
published_at: new Date('2023-05-25T07:21:07.447Z') published_at: new Date('2023-05-25T07:21:07.447Z')
}, {
id: 'post-4-featured',
title: 'Featured Post 4',
slug: 'featured-post-4',
featured: true,
published_at: new Date('2023-05-15T07:21:07.447Z')
}]; }];