From df3aaed3c838bfa4b06d65038a56ab3dd110c88a Mon Sep 17 00:00:00 2001 From: Naz Date: Tue, 22 Aug 2023 15:16:54 +0800 Subject: [PATCH] Added test coverage for collection updates refs https://github.com/TryGhost/Arch/issues/77 - The test covers collection updates on post resource add/edit/delete operations --- .../__snapshots__/collections.test.js.snap | 136 ++++++++++++++++++ .../test/e2e-api/admin/collections.test.js | 96 +++++++++++++ 2 files changed, 232 insertions(+) diff --git a/ghost/core/test/e2e-api/admin/__snapshots__/collections.test.js.snap b/ghost/core/test/e2e-api/admin/__snapshots__/collections.test.js.snap index d1940f21cf..591a3a9804 100644 --- a/ghost/core/test/e2e-api/admin/__snapshots__/collections.test.js.snap +++ b/ghost/core/test/e2e-api/admin/__snapshots__/collections.test.js.snap @@ -1365,6 +1365,142 @@ Object { } `; +exports[`Collections API Collection Posts updates automatically Updates collections when a Post is added/edited/deleted 1: [body] 1`] = ` +Object { + "collections": Array [ + Object { + "count": Object { + "posts": 2, + }, + "created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, + "description": "Featured posts", + "feature_image": null, + "filter": "featured:true", + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "slug": "featured", + "title": "Featured", + "type": "automatic", + "updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, + }, + ], +} +`; + +exports[`Collections API Collection Posts updates automatically Updates collections when a Post is added/edited/deleted 2: [headers] 1`] = ` +Object { + "access-control-allow-origin": "http://127.0.0.1:2369", + "cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0", + "content-length": "284", + "content-type": "application/json; charset=utf-8", + "content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/, + "etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/, + "vary": "Accept-Version, Origin, Accept-Encoding", + "x-powered-by": "Express", +} +`; + +exports[`Collections API Collection Posts updates automatically Updates collections when a Post is added/edited/deleted 3: [body] 1`] = ` +Object { + "collections": Array [ + Object { + "count": Object { + "posts": 2, + }, + "created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, + "description": "Featured posts", + "feature_image": null, + "filter": "featured:true", + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "slug": "featured", + "title": "Featured", + "type": "automatic", + "updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, + }, + ], +} +`; + +exports[`Collections API Collection Posts updates automatically Updates collections when a Post is added/edited/deleted 4: [headers] 1`] = ` +Object { + "access-control-allow-origin": "http://127.0.0.1:2369", + "cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0", + "content-length": "284", + "content-type": "application/json; charset=utf-8", + "content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/, + "etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/, + "vary": "Accept-Version, Origin, Accept-Encoding", + "x-powered-by": "Express", +} +`; + +exports[`Collections API Collection Posts updates automatically Updates collections when a Post is added/edited/deleted 5: [body] 1`] = ` +Object { + "collections": Array [ + Object { + "count": Object { + "posts": 3, + }, + "created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, + "description": "Featured posts", + "feature_image": null, + "filter": "featured:true", + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "slug": "featured", + "title": "Featured", + "type": "automatic", + "updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, + }, + ], +} +`; + +exports[`Collections API Collection Posts updates automatically Updates collections when a Post is added/edited/deleted 6: [headers] 1`] = ` +Object { + "access-control-allow-origin": "http://127.0.0.1:2369", + "cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0", + "content-length": "284", + "content-type": "application/json; charset=utf-8", + "content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/, + "etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/, + "vary": "Accept-Version, Origin, Accept-Encoding", + "x-powered-by": "Express", +} +`; + +exports[`Collections API Collection Posts updates automatically Updates collections when a Post is added/edited/deleted 7: [body] 1`] = ` +Object { + "collections": Array [ + Object { + "count": Object { + "posts": 2, + }, + "created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, + "description": "Featured posts", + "feature_image": null, + "filter": "featured:true", + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "slug": "featured", + "title": "Featured", + "type": "automatic", + "updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, + }, + ], +} +`; + +exports[`Collections API Collection Posts updates automatically Updates collections when a Post is added/edited/deleted 8: [headers] 1`] = ` +Object { + "access-control-allow-origin": "http://127.0.0.1:2369", + "cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0", + "content-length": "284", + "content-type": "application/json; charset=utf-8", + "content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/, + "etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/, + "vary": "Accept-Version, Origin, Accept-Encoding", + "x-powered-by": "Express", +} +`; + exports[`Collections API Delete Can delete a Collection 1: [body] 1`] = ` Object { "collections": Array [ diff --git a/ghost/core/test/e2e-api/admin/collections.test.js b/ghost/core/test/e2e-api/admin/collections.test.js index 16f046190e..17e6e668f4 100644 --- a/ghost/core/test/e2e-api/admin/collections.test.js +++ b/ghost/core/test/e2e-api/admin/collections.test.js @@ -5,6 +5,7 @@ const { mockManager, matchers } = require('../../utils/e2e-framework'); +const DomainEvents = require('@tryghost/domain-events/lib/DomainEvents'); const { anyContentVersion, anyEtag, @@ -488,4 +489,99 @@ describe('Collections API', function () { }); }); }); + + describe('Collection Posts updates automatically', function () { + it('Updates collections when a Post is added/edited/deleted', async function () { + await agent + .get(`/collections/slug/featured/?include=count.posts`) + .expectStatus(200) + .matchHeaderSnapshot({ + 'content-version': anyContentVersion, + etag: anyEtag + }) + .matchBodySnapshot({ + collections: [{ + ...matchCollection, + count: { + posts: 2 + } + }] + }); + + const postToAdd = { + title: 'Collection update test', + featured: false + }; + + const {body: {posts: [post]}} = await agent + .post('/posts/') + .body({ + posts: [postToAdd] + }) + .expectStatus(201); + + await agent + .get(`/collections/slug/featured/?include=count.posts`) + .expectStatus(200) + .matchHeaderSnapshot({ + 'content-version': anyContentVersion, + etag: anyEtag + }) + .matchBodySnapshot({ + collections: [{ + ...matchCollection, + count: { + posts: 2 + } + }] + }); + + await agent + .put(`/posts/${post.id}/`) + .body({ + posts: [Object.assign({}, post, {featured: true})] + }) + .expectStatus(200); + + await DomainEvents.allSettled(); + + await agent + .get(`/collections/slug/featured/?include=count.posts`) + .expectStatus(200) + .matchHeaderSnapshot({ + 'content-version': anyContentVersion, + etag: anyEtag + }) + .matchBodySnapshot({ + collections: [{ + ...matchCollection, + count: { + posts: 3 + } + }] + }); + + await agent + .delete(`/posts/${post.id}/`) + .expectStatus(204); + + await DomainEvents.allSettled(); + + await agent + .get(`/collections/slug/featured/?include=count.posts`) + .expectStatus(200) + .matchHeaderSnapshot({ + 'content-version': anyContentVersion, + etag: anyEtag + }) + .matchBodySnapshot({ + collections: [{ + ...matchCollection, + count: { + posts: 2 + } + }] + }); + }); + }); });