From c86859d0a76bbbee4818a4d15e448060feb18bce Mon Sep 17 00:00:00 2001 From: Naz Date: Mon, 5 Jun 2023 16:37:17 +0700 Subject: [PATCH] Added test coverage to automatic collections refs https://github.com/TryGhost/Team/issues/3170 - The test confirms relational filters like `tag:kitchen-sink` filtering works for automatic collections --- .../__snapshots__/collections.test.js.snap | 42 +++++++++++++++++++ .../test/e2e-api/admin/collections.test.js | 35 ++++++++++++++++ 2 files changed, 77 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 2c808acf3a..db0ede694a 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 @@ -104,6 +104,48 @@ Object { } `; +exports[`Collections API Automatic Collection Filtering Creates an automatic Collection with a relational tag filter 1: [body] 1`] = ` +Object { + "collections": Array [ + Object { + "created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, + "description": "Test Collection Description with relational tag filter", + "feature_image": null, + "filter": "tag:kitchen-sink", + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "posts": Array [ + Object { + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "sort_order": 0, + }, + Object { + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "sort_order": 1, + }, + ], + "title": "Test Collection with relational tag filter", + "type": "automatic", + "updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, + }, + ], +} +`; + +exports[`Collections API Automatic Collection Filtering Creates an automatic Collection with a relational tag filter 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": "431", + "content-type": "application/json; charset=utf-8", + "content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/, + "etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/, + "location": StringMatching /https\\?:\\\\/\\\\/\\.\\*\\?\\\\/collections\\\\/\\[a-f0-9\\]\\{24\\}\\\\//, + "vary": "Accept-Version, Origin, Accept-Encoding", + "x-cache-invalidate": "/*", + "x-powered-by": "Express", +} +`; + exports[`Collections API Can add 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 740a251433..10ce20dd78 100644 --- a/ghost/core/test/e2e-api/admin/collections.test.js +++ b/ghost/core/test/e2e-api/admin/collections.test.js @@ -379,5 +379,40 @@ describe('Collections API', function () { collections: [buildMatcher(7)] }); }); + + it('Creates an automatic Collection with a relational tag filter', async function () { + const collection = { + title: 'Test Collection with relational tag filter', + description: 'Test Collection Description with relational tag filter', + type: 'automatic', + filter: 'tag:kitchen-sink' + }; + + const automaticTagCollection = await agent + .post('/collections/') + .body({ + collections: [collection] + }) + .expectStatus(201) + .matchHeaderSnapshot({ + 'content-version': anyContentVersion, + etag: anyEtag, + location: anyLocationFor('collections') + }) + .matchBodySnapshot({ + collections: [buildMatcher(2)] + }); + + const kitchenSinkTagPosts = await agent + .get('/posts/?filter=tag:kitchen-sink'); + + assert.equal(automaticTagCollection.body.collections[0].posts.length, 2); + assert.equal(kitchenSinkTagPosts.body.posts.length, 2); + + const collectionPostIds = automaticTagCollection.body.collections[0].posts.map(p => p.id); + const tagFilteredPosts = kitchenSinkTagPosts.body.posts.map(p => p.id); + + assert.deepEqual(collectionPostIds, tagFilteredPosts); + }); }); });