diff --git a/api/src/services/comments/comments.js b/api/src/services/comments/comments.js index c55034e..d83990b 100644 --- a/api/src/services/comments/comments.js +++ b/api/src/services/comments/comments.js @@ -1,7 +1,7 @@ import { db } from 'src/lib/db' -export const comments = () => { - return db.comment.findMany() +export const comments = ({ postId }) => { + return db.comment.findMany({ where: { postId } }) } export const comment = ({ id }) => { diff --git a/api/src/services/comments/comments.test.js b/api/src/services/comments/comments.test.js index 8bd5f0a..001d9ec 100644 --- a/api/src/services/comments/comments.test.js +++ b/api/src/services/comments/comments.test.js @@ -1,4 +1,5 @@ import { comments, createComment } from './comments' +import { db } from 'src/lib/db' // Generated boilerplate tests do not account for all circumstances // and can fail without adjustments, e.g. Float and DateTime types. @@ -7,11 +8,17 @@ import { comments, createComment } from './comments' // https://redwoodjs.com/docs/testing#jest-expect-type-considerations describe('comments', () => { - scenario('returns all comments', async (scenario) => { - const result = await comments() - - expect(result.length).toEqual(Object.keys(scenario.comment).length) - }) + scenario( + 'returns all comments for a single post from the database', + async (scenario) => { + const result = await comments({ postId: scenario.comment.jane.postId }) + const post = await db.post.findUnique({ + where: { id: scenario.comment.jane.postId }, + include: { comments: true }, + }) + expect(result.length).toEqual(post.comments.length) + } + ) scenario('postOnly', 'creates a new comment', async (scenario) => { const comment = await createComment({