From e5e05c292c7aec349f3a13ba8b9a59070c66a8f6 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Wed, 6 Jul 2022 15:50:11 +0200 Subject: [PATCH] Added basic test fixture structure for comments refs https://github.com/TryGhost/Team/issues/1664 - includes a comment and a reply for now --- .../__snapshots__/comments.test.js.snap | 30 ++++++++++++++-- .../e2e-api/members-comments/comments.test.js | 31 +++++++++-------- test/utils/fixture-utils.js | 9 +++++ test/utils/fixtures/data-generator.js | 34 +++++++++++++++++++ 4 files changed, 88 insertions(+), 16 deletions(-) diff --git a/test/e2e-api/members-comments/__snapshots__/comments.test.js.snap b/test/e2e-api/members-comments/__snapshots__/comments.test.js.snap index 6a32780179..c931c36fbb 100644 --- a/test/e2e-api/members-comments/__snapshots__/comments.test.js.snap +++ b/test/e2e-api/members-comments/__snapshots__/comments.test.js.snap @@ -3,6 +3,32 @@ exports[`Comments API when authenticated Can browse all comments of a post 1: [body] 1`] = ` Object { "comments": Array [ + Object { + "created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, + "edited_at": null, + "html": "

First.

", + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "member": Object { + "avatar_image": null, + "bio": null, + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "name": "Mr Egg", + }, + "status": "published", + }, + Object { + "created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, + "edited_at": null, + "html": "

Really original

", + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "member": Object { + "avatar_image": null, + "bio": null, + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "name": null, + }, + "status": "published", + }, Object { "created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, "edited_at": null, @@ -24,7 +50,7 @@ Object { "page": 1, "pages": 1, "prev": null, - "total": 1, + "total": 3, }, }, } @@ -34,7 +60,7 @@ exports[`Comments API when authenticated Can browse all comments of a post 2: [h Object { "access-control-allow-origin": "*", "cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0", - "content-length": "327", + "content-length": "782", "content-type": "application/json; charset=utf-8", "etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/, "vary": "Accept-Encoding", diff --git a/test/e2e-api/members-comments/comments.test.js b/test/e2e-api/members-comments/comments.test.js index a6cfdca605..45477f951c 100644 --- a/test/e2e-api/members-comments/comments.test.js +++ b/test/e2e-api/members-comments/comments.test.js @@ -3,12 +3,24 @@ const {anyEtag, anyObjectId, anyLocationFor, anyISODateTime, anyUuid} = matchers let membersAgent, membersService, postId, commentId; +const commentMatcher = { + id: anyObjectId, + created_at: anyISODateTime +}; + +const commentMatcherWithMember = { + id: anyObjectId, + created_at: anyISODateTime, + member: { + id: anyObjectId + } +}; + describe('Comments API', function () { before(async function () { membersAgent = await agentProvider.getMembersAPIAgent(); - await fixtureManager.init('posts', 'members'); - //await fixtureManager.init('comments'); + await fixtureManager.init('posts', 'members', 'comments'); postId = fixtureManager.get('posts', 0).id; }); @@ -27,7 +39,7 @@ describe('Comments API', function () { }); it('Can comment on a post', async function () { - await membersAgent + const {body} = await membersAgent .post(`/api/comments/`) .body({comments: [{ post_id: postId, @@ -39,10 +51,7 @@ describe('Comments API', function () { location: anyLocationFor('comments') }) .matchBodySnapshot({ - comments: [{ - id: anyObjectId, - created_at: anyISODateTime - }] + comments: [commentMatcher] }); // Save for other tests commentId = body.comments[0].id; @@ -56,13 +65,7 @@ describe('Comments API', function () { etag: anyEtag }) .matchBodySnapshot({ - comments: [{ - id: anyObjectId, - created_at: anyISODateTime, - member: { - id: anyObjectId - } - }] + comments: new Array(3).fill(commentMatcherWithMember) }); }); diff --git a/test/utils/fixture-utils.js b/test/utils/fixture-utils.js index 828be22ceb..45f0091020 100644 --- a/test/utils/fixture-utils.js +++ b/test/utils/fixture-utils.js @@ -609,6 +609,12 @@ const fixtures = { }); }, + insertComments: async function insertComments() { + return Promise.map(DataGenerator.forKnex.comments, function (comment) { + return models.Comment.add(comment, context.internal); + }); + }, + insertSnippets: function insertSnippets() { return Promise.map(DataGenerator.forKnex.snippets, function (snippet) { return models.Snippet.add(snippet, context.internal); @@ -742,6 +748,9 @@ const toDoList = { }, 'tiers:archived': function insertArchivedTiers() { return fixtures.insertArchivedTiers(); + }, + comments: function insertComments() { + return fixtures.insertComments(); } }; diff --git a/test/utils/fixtures/data-generator.js b/test/utils/fixtures/data-generator.js index 7faeb7195e..025d9b8b0b 100644 --- a/test/utils/fixtures/data-generator.js +++ b/test/utils/fixtures/data-generator.js @@ -780,6 +780,20 @@ DataGenerator.Content = { type: 'select', value: 'Full' } + ], + + comments: [ + { + id: '6195c6a1e792de832cd08144', + html: '

First.

', + member_index: 0 + }, + { + id: '6195c6a1e792de832cd08145', + html: '

Really original

', + parent_id: '6195c6a1e792de832cd08144', + member_index: 1 + } ] }; @@ -1192,6 +1206,19 @@ DataGenerator.forKnex = (function () { }); } + function createComment(overrides) { + const memberIndex = overrides.member_index || 0; + delete overrides.memberIndex; + + const newObj = _.cloneDeep(overrides); + + return _.defaults(newObj, { + id: ObjectId().toHexString(), + member_id: DataGenerator.Content.members[memberIndex].id, + post_id: DataGenerator.Content.posts[0].id + }); + } + const posts = [ createPost(DataGenerator.Content.posts[0]), createPost(DataGenerator.Content.posts[1]), @@ -1548,6 +1575,11 @@ DataGenerator.forKnex = (function () { createBasic(DataGenerator.Content.custom_theme_settings[1]) ]; + const comments = [ + createComment(DataGenerator.Content.comments[0]), + createComment(DataGenerator.Content.comments[1]) + ]; + return { createPost, createGenericPost, @@ -1578,6 +1610,7 @@ DataGenerator.forKnex = (function () { createProduct, createNewsletter, createOffer, + createComment, invites, posts, @@ -1606,6 +1639,7 @@ DataGenerator.forKnex = (function () { stripe_products, snippets, custom_theme_settings, + comments, members_paid_subscription_events };