From 7464dbc1aff53a9eed649fc2800c0dfd271bff66 Mon Sep 17 00:00:00 2001 From: Naz Date: Tue, 1 Aug 2023 14:48:56 +0800 Subject: [PATCH] Added e2e test coverage for bulk collection edits refs https://github.com/TryGhost/Arch/issues/16 - Makes sure post bulk actions also update collection posts --- .../test/e2e-api/admin/posts-bulk.test.js | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/ghost/core/test/e2e-api/admin/posts-bulk.test.js b/ghost/core/test/e2e-api/admin/posts-bulk.test.js index be840129bd..c6c6ae959f 100644 --- a/ghost/core/test/e2e-api/admin/posts-bulk.test.js +++ b/ghost/core/test/e2e-api/admin/posts-bulk.test.js @@ -1,3 +1,4 @@ +const DomainEvents = require('@tryghost/domain-events'); const {agentProvider, fixtureManager, mockManager} = require('../../utils/e2e-framework'); const models = require('../../../core/server/models'); const assert = require('assert/strict'); @@ -28,6 +29,10 @@ describe('Posts Bulk API', function () { assert(amount > 0, 'Expect at least one post to be affected for this test to work'); + let featuredCollection = await models.Collection.findPage({filter: 'slug:featured', limit: 1, withRelated: ['posts']}); + let featuredCollectionPostsAmount = featuredCollection.data[0].toJSON().posts.length; + assert(featuredCollectionPostsAmount > 0, 'Expect to have multiple featured collection posts'); + const response = await agent .put('/posts/bulk/?filter=' + encodeURIComponent(filter)) .body({ @@ -38,12 +43,18 @@ describe('Posts Bulk API', function () { .expectStatus(200) .matchBodySnapshot(); + await DomainEvents.allSettled(); + assert.equal(response.body.bulk.meta.stats.successful, amount, `Expect all matching posts (${amount}) to be changed`); // Fetch all posts and check if they are featured const posts = await models.Post.findAll({filter, status: 'all'}); assert.equal(posts.length, amount, `Expect all matching posts (${amount}) to be changed`); + featuredCollection = await models.Collection.findPage({filter: 'slug:featured', limit: 1, withRelated: ['posts']}); + featuredCollectionPostsAmount = featuredCollection.data[0].toJSON().posts.length; + assert.equal(featuredCollectionPostsAmount, amount, 'Expect to have same amount featured collection posts as changed'); + for (const post of posts) { assert(post.get('featured') === true, `Expect post ${post.id} to be featured`); } @@ -58,6 +69,10 @@ describe('Posts Bulk API', function () { assert(amount > 0, 'Expect at least one post to be affected for this test to work'); + let featuredCollection = await models.Collection.findPage({filter: 'slug:featured', limit: 1, withRelated: ['posts']}); + let featuredCollectionPostsAmount = featuredCollection.data[0].toJSON().posts.length; + assert(featuredCollectionPostsAmount > 0, 'Expect to have multiple featured collection posts'); + const response = await agent .put('/posts/bulk/?filter=' + encodeURIComponent(filter)) .body({ @@ -68,12 +83,18 @@ describe('Posts Bulk API', function () { .expectStatus(200) .matchBodySnapshot(); + await DomainEvents.allSettled(); + assert.equal(response.body.bulk.meta.stats.successful, amount, `Expect all matching posts (${amount}) to be changed`); // Fetch all posts and check if they are featured const posts = await models.Post.findAll({filter, status: 'all'}); assert.equal(posts.length, amount, `Expect all matching posts (${amount}) to be changed`); + featuredCollection = await models.Collection.findPage({filter: 'slug:featured', limit: 1, withRelated: ['posts']}); + featuredCollectionPostsAmount = featuredCollection.data[0].toJSON().posts.length; + assert.equal(featuredCollectionPostsAmount, 0, 'Expect to have no featured collection posts'); + for (const post of posts) { assert(post.get('featured') === false, `Expect post ${post.id} to be unfeatured`); } @@ -293,6 +314,13 @@ describe('Posts Bulk API', function () { assert(amount > 0, 'Expect at least one post to be affected for this test to work'); + await agent + .get('posts/?collection=latest') + .expectStatus(200) + .expect((res) => { + assert(res.body.posts.length > 0, 'Expect latest collection to have some posts'); + }); + const response = await agent .delete('/posts/?filter=' + encodeURIComponent(filter)) .expectStatus(200)