From 31c8b6e35d2af573c2ef1b1088d381a314a25986 Mon Sep 17 00:00:00 2001 From: Matt Rider Date: Fri, 3 May 2019 13:41:20 -0300 Subject: [PATCH] Use single cypher query in CreateComments - to create relation between post, comment, author - fix test to create a post so said cypher query doesn't silently fail --- backend/src/resolvers/comments.js | 13 ++------ backend/src/resolvers/comments.spec.js | 44 ++++++++++++++++---------- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/backend/src/resolvers/comments.js b/backend/src/resolvers/comments.js index 56280d6ea..eb792ecb8 100644 --- a/backend/src/resolvers/comments.js +++ b/backend/src/resolvers/comments.js @@ -16,17 +16,10 @@ export default { const session = context.driver.session() await session.run(` - MATCH (author:User {id: $userId}), (comment:Comment {id: $commentId}) - MERGE (comment)<-[:WROTE]-(author) - RETURN author`, { - userId: context.user.id, - commentId: comment.id - } - ) - await session.run(` - MATCH (post:Post {id: $postId}), (comment:Comment {id: $commentId}) - MERGE (post)<-[:COMMENTS]-(comment) + MATCH (post:Post {id: $postId}), (comment:Comment {id: $commentId}), (author:User {id: $userId}) + MERGE (post)<-[:COMMENTS]-(comment)<-[:WROTE]-(author) RETURN post`, { + userId: context.user.id, postId, commentId: comment.id } diff --git a/backend/src/resolvers/comments.spec.js b/backend/src/resolvers/comments.spec.js index e572e206b..9a54acd17 100644 --- a/backend/src/resolvers/comments.spec.js +++ b/backend/src/resolvers/comments.spec.js @@ -4,7 +4,8 @@ import { host, login } from '../jest/helpers' const factory = Factory() let client -let variables +let createCommentVariables +let createPostVariables beforeEach(async () => { await factory.create('User', { @@ -18,7 +19,7 @@ afterEach(async () => { }) describe('CreateComment', () => { - const mutation = ` + const createCommentMutation = ` mutation($postId: ID, $content: String!) { CreateComment(postId: $postId, content: $content) { id @@ -26,14 +27,21 @@ describe('CreateComment', () => { } } ` + const createPostMutation = ` + mutation($id: ID!, $title: String!, $content: String!) { + CreatePost(id: $id, title: $title, content: $content) { + id + } + } + ` describe('unauthenticated', () => { it('throws authorization error', async () => { - variables = { + createCommentVariables = { postId: 'p1', content: 'I\'m not authorised to comment' } client = new GraphQLClient(host) - await expect(client.request(mutation, variables)).rejects.toThrow('Not Authorised') + await expect(client.request(createCommentMutation, createCommentVariables)).rejects.toThrow('Not Authorised') }) }) @@ -42,28 +50,30 @@ describe('CreateComment', () => { beforeEach(async () => { headers = await login({ email: 'test@example.org', password: '1234' }) client = new GraphQLClient(host, { headers }) - }) - - it('creates a comment', async () => { - variables = { + createCommentVariables = { postId: 'p1', content: 'I\'m authorised to comment' } + }) + + it('creates a comment', async () => { const expected = { CreateComment: { content: 'I\'m authorised to comment' } } - await expect(client.request(mutation, variables)).resolves.toMatchObject(expected) + await expect(client.request(createCommentMutation, createCommentVariables)).resolves.toMatchObject(expected) }) it('assigns the authenticated user as author', async () => { - variables = { - postId: 'p1', - content: 'I\'m authorised to comment' + createPostVariables = { + id: 'p1', + title: 'post to comment on', + content: 'please comment on me' } - await client.request(mutation, variables) + await client.request(createPostMutation, createPostVariables) + await client.request(createCommentMutation, createCommentVariables) const { User } = await client.request(`{ User(email: "test@example.org") { @@ -77,22 +87,22 @@ describe('CreateComment', () => { }) it('throw an error if an empty string is sent as content', async () => { - variables = { + createCommentVariables = { postId: 'p1', content: '

' } - await expect(client.request(mutation, variables)) + await expect(client.request(createCommentMutation, createCommentVariables)) .rejects.toThrow('Comment must be at least 1 character long!') }) it('throws an error if a comment does not contain a single character', async () => { - variables = { + createCommentVariables = { postId: 'p1', content: '

' } - await expect(client.request(mutation, variables)) + await expect(client.request(createCommentMutation, createCommentVariables)) .rejects.toThrow('Comment must be at least 1 character long!') }) })