Added basic test fixture structure for comments

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

- includes a comment and a reply for now
This commit is contained in:
Hannah Wolfe 2022-07-06 15:50:11 +02:00 committed by Simon Backx
parent caef9d74e0
commit e5e05c292c
4 changed files with 88 additions and 16 deletions

View File

@ -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": "<p>First.</p>",
"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": "<p>Really original</p>",
"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",

View File

@ -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)
});
});

View File

@ -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();
}
};

View File

@ -780,6 +780,20 @@ DataGenerator.Content = {
type: 'select',
value: 'Full'
}
],
comments: [
{
id: '6195c6a1e792de832cd08144',
html: '<p>First.</p>',
member_index: 0
},
{
id: '6195c6a1e792de832cd08145',
html: '<p>Really original</p>',
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
};