Skip to content

Commit

Permalink
feat(/api/v1/contents): add tests for return results for comments
Browse files Browse the repository at this point in the history
add tests for with_children and with_root query params to filter content between posts and comments.
  • Loading branch information
mthmcalixto committed Jan 21, 2024
1 parent a706ee2 commit 1f1a8ba
Showing 1 changed file with 137 additions and 0 deletions.
137 changes: 137 additions & 0 deletions tests/integration/api/v1/contents/get.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1246,5 +1246,142 @@ describe('GET /api/v1/contents', () => {
expect(uuidVersion(responseBody.error_id)).toEqual(4);
expect(uuidVersion(responseBody.request_id)).toEqual(4);
});

test.each([
{ content: 'relevant root', params: [] },
{ content: 'relevant root, where first content is draft,', params: ['with_children=false'] },
{ content: 'relevant children', params: ['with_children=true'] },
{
content: 'new root, where first content is draft,',
params: ['with_children=false', 'with_root=true', 'strategy=new'],
},
{ content: 'new root', params: ['with_children=false', 'with_root=true', 'strategy=new'] },
{ content: 'new root', params: ['with_children=false', 'strategy=new'] },
{ content: 'new children', params: ['with_children=true', 'with_root=false', 'strategy=new'] },
{ content: 'new root and children', params: ['with_children=true', 'strategy=new'] },
{ content: 'new root and children', params: ['with_children=true', 'with_root=true', 'strategy=new'] },
{ content: 'new root, where first content is draft,', params: ['with_root=true', 'strategy=new'] },
])('get $content with params: $params', async ({ content, params }) => {
const url = `${orchestrator.webserverUrl}/api/v1/contents?${params.join('&')}`;

const createUser = async () => orchestrator.createUser();
const createContent = async (user, options) => orchestrator.createContent({ owner_id: user.id, ...options });
const createComment = async (user, parent, body) =>
createContent(user, { body, status: 'published', parent_id: parent.id });

const [firstUser, secondUser, thirdUser] = await Promise.all(Array.from({ length: 3 }, () => createUser()));

const firstRootContent = await createContent(firstUser, {
title: content.includes('draft') ? 'Contéudo "Draft"' : 'Primeiro conteúdo criado',
status: content.includes('draft') ? 'draft' : 'published',
});

const secondRootContent = await createContent(secondUser, {
title: 'Segundo conteúdo criado',
status: 'published',
});

const thirdRootContent = await createContent(thirdUser, {
title: 'Terceiro conteúdo criado',
status: 'published',
});

const firstComment = await createComment(firstUser, thirdRootContent, 'Comentário #1');
const secondComment = await createComment(thirdUser, secondRootContent, 'Comentário #2');

const response = await fetch(url);
const responseBody = await response.json();

expect(response.status).toEqual(200);

const rootReturn = [];

if (!params.includes('with_root=false')) {
rootReturn.push(
{
id: thirdRootContent.id,
owner_id: thirdUser.id,
parent_id: null,
slug: 'terceiro-conteudo-criado',
title: 'Terceiro conteúdo criado',
status: 'published',
source_url: null,
created_at: thirdRootContent.created_at.toISOString(),
updated_at: thirdRootContent.updated_at.toISOString(),
published_at: thirdRootContent.published_at.toISOString(),
deleted_at: null,
owner_username: thirdUser.username,
tabcoins: 1,
children_deep_count: 1,
},
{
id: secondRootContent.id,
owner_id: secondUser.id,
parent_id: null,
slug: 'segundo-conteudo-criado',
title: 'Segundo conteúdo criado',
status: 'published',
source_url: null,
created_at: secondRootContent.created_at.toISOString(),
updated_at: secondRootContent.updated_at.toISOString(),
published_at: secondRootContent.published_at.toISOString(),
deleted_at: null,
owner_username: secondUser.username,
tabcoins: 1,
children_deep_count: 1,
}
);

if (!content.includes('draft')) {
rootReturn.push({
id: firstRootContent.id,
owner_id: firstUser.id,
parent_id: null,
slug: 'primeiro-conteudo-criado',
title: 'Primeiro conteúdo criado',
status: 'published',
source_url: null,
created_at: firstRootContent.created_at.toISOString(),
updated_at: firstRootContent.updated_at.toISOString(),
published_at: firstRootContent.published_at.toISOString(),
deleted_at: null,
owner_username: firstUser.username,
tabcoins: 1,
children_deep_count: 0,
});
}
}

if (params.includes('with_children=true')) {
function createCommentObject(comment, owner, parent) {
return {
id: comment.id,
owner_id: owner.id,
parent_id: parent.id,
slug: comment.slug,
title: null,
body: comment.body,
status: 'published',
source_url: null,
created_at: comment.created_at.toISOString(),
updated_at: comment.updated_at.toISOString(),
published_at: comment.published_at.toISOString(),
deleted_at: null,
owner_username: owner.username,
tabcoins: 0,
children_deep_count: 0,
};
}

const comments = [
createCommentObject(secondComment, thirdUser, secondRootContent),
createCommentObject(firstComment, firstUser, thirdRootContent),
];

params.includes('strategy=new') ? rootReturn.unshift(...comments) : rootReturn.push(...comments);
}

expect(responseBody).toStrictEqual(rootReturn);
});
});
});

0 comments on commit 1f1a8ba

Please sign in to comment.