diff --git a/backend/src/schema/resolvers/comments.spec.js b/backend/src/schema/resolvers/comments.spec.js index dcb2d31f8..ba7364a7f 100644 --- a/backend/src/schema/resolvers/comments.spec.js +++ b/backend/src/schema/resolvers/comments.spec.js @@ -8,10 +8,7 @@ const driver = getDriver() const neode = getNeode() const factory = Factory() -let variables -let mutate -let authenticatedUser -let commentAuthor +let variables, mutate, authenticatedUser, commentAuthor, newlyCreatedComment beforeAll(() => { const { server } = createServer({ @@ -57,7 +54,7 @@ const setupPostAndComment = async () => { content: 'Post to be commented', categoryIds: ['cat9'], }) - await factory.create('Comment', { + newlyCreatedComment = await factory.create('Comment', { id: 'c456', postId: 'p1', author: commentAuthor, @@ -160,6 +157,8 @@ describe('UpdateComment', () => { UpdateComment(content: $content, id: $id) { id content + createdAt + updatedAt } } ` @@ -200,6 +199,33 @@ describe('UpdateComment', () => { ) }) + it('updates a comment, but maintains non-updated attributes', async () => { + const expected = { + data: { + UpdateComment: { + id: 'c456', + content: 'The comment is updated', + createdAt: expect.any(String), + }, + }, + } + await expect(mutate({ mutation: updateCommentMutation, variables })).resolves.toMatchObject( + expected, + ) + }) + + it('updates the updatedAt attribute', async () => { + newlyCreatedComment = await newlyCreatedComment.toJson() + const { + data: { UpdateComment }, + } = await mutate({ mutation: updateCommentMutation, variables }) + expect(newlyCreatedComment.updatedAt).toBeTruthy() + expect(Date.parse(newlyCreatedComment.updatedAt)).toEqual(expect.any(Number)) + expect(UpdateComment.updatedAt).toBeTruthy() + expect(Date.parse(UpdateComment.updatedAt)).toEqual(expect.any(Number)) + expect(newlyCreatedComment.updatedAt).not.toEqual(UpdateComment.updatedAt) + }) + describe('if `content` empty', () => { beforeEach(() => { variables = { ...variables, content: '

' } diff --git a/backend/src/schema/resolvers/posts.spec.js b/backend/src/schema/resolvers/posts.spec.js index 1fc8c51f6..0e7272e8e 100644 --- a/backend/src/schema/resolvers/posts.spec.js +++ b/backend/src/schema/resolvers/posts.spec.js @@ -361,7 +361,7 @@ describe('CreatePost', () => { }) describe('UpdatePost', () => { - let author + let author, newlyCreatedPost const updatePostMutation = gql` mutation($id: ID!, $title: String!, $content: String!, $categoryIds: [ID]) { UpdatePost(id: $id, title: $title, content: $content, categoryIds: $categoryIds) { @@ -370,12 +370,14 @@ describe('UpdatePost', () => { categories { id } + createdAt + updatedAt } } ` beforeEach(async () => { author = await factory.create('User', { slug: 'the-author' }) - await factory.create('Post', { + newlyCreatedPost = await factory.create('Post', { author, id: 'p9876', title: 'Old title', @@ -421,6 +423,29 @@ describe('UpdatePost', () => { ) }) + it('updates a post, but maintains non-updated attributes', async () => { + const expected = { + data: { + UpdatePost: { id: 'p9876', content: 'New content', createdAt: expect.any(String) }, + }, + } + await expect(mutate({ mutation: updatePostMutation, variables })).resolves.toMatchObject( + expected, + ) + }) + + it('updates the updatedAt attribute', async () => { + newlyCreatedPost = await newlyCreatedPost.toJson() + const { + data: { UpdatePost }, + } = await mutate({ mutation: updatePostMutation, variables }) + expect(newlyCreatedPost.updatedAt).toBeTruthy() + expect(Date.parse(newlyCreatedPost.updatedAt)).toEqual(expect.any(Number)) + expect(UpdatePost.updatedAt).toBeTruthy() + expect(Date.parse(UpdatePost.updatedAt)).toEqual(expect.any(Number)) + expect(newlyCreatedPost.updatedAt).not.toEqual(UpdatePost.updatedAt) + }) + describe('no new category ids provided for update', () => { it('resolves and keeps current categories', async () => { const expected = {