diff --git a/backend/src/schema/resolvers/userData.js b/backend/src/schema/resolvers/userData.js index e8dc75588..33ac282c8 100644 --- a/backend/src/schema/resolvers/userData.js +++ b/backend/src/schema/resolvers/userData.js @@ -5,7 +5,7 @@ export default { const cypher = ` MATCH (u:User { id: $id }) WITH u AS user - MATCH (p:Post) + OPTIONAL MATCH (p:Post) WHERE (p)<-[:COMMENTS]-(:Comment)<-[:WROTE]-(user) OR (user)-[:WROTE]->(p) RETURN { user: properties(user), posts: collect(properties(p)) } diff --git a/backend/src/schema/resolvers/userData.spec.js b/backend/src/schema/resolvers/userData.spec.js new file mode 100644 index 000000000..0bc71c62f --- /dev/null +++ b/backend/src/schema/resolvers/userData.spec.js @@ -0,0 +1,164 @@ +import Factory, { cleanDatabase } from '../../db/factories' +import { gql } from '../../helpers/jest' +import { getNeode, getDriver } from '../../db/neo4j' +import createServer from '../../server' +import { createTestClient } from 'apollo-server-testing' + +let query, authenticatedUser + +const driver = getDriver() +const neode = getNeode() + +beforeAll(async () => { + await cleanDatabase() + const user = await Factory.build('user', { + id: 'a-user', + name: 'John Doe', + slug: 'john-doe', + }) + authenticatedUser = await user.toJson() + const { server } = createServer({ + context: () => { + return { + driver, + neode, + user: authenticatedUser, + } + }, + }) + query = createTestClient(server).query +}) + +afterAll(async () => { + await cleanDatabase() +}) + +const userDataQuery = gql` + query($id: ID!) { + userData(id: $id) { + user { + id + name + slug + } + posts { + id + title + content + comments { + content + author { + slug + } + } + } + } + } +` + +describe('resolvers/userData', () => { + const variables = { id: 'a-user' } + + describe('given one user who did not write anything so far', () => { + it("returns the user's data and no posts", async () => { + await expect(query({ query: userDataQuery, variables })).resolves.toMatchObject({ + data: { + userData: { + user: { + id: 'a-user', + name: 'John Doe', + slug: 'john-doe', + }, + posts: [], + }, + }, + }) + }) + + describe('the user writes a post', () => { + beforeAll(async () => { + await Factory.build( + 'post', + { + id: 'a-post', + title: 'A post', + content: 'A post', + }, + { authorId: 'a-user' }, + ) + }) + + it("returns the user's data and the post", async () => { + await expect(query({ query: userDataQuery, variables })).resolves.toMatchObject({ + data: { + userData: { + user: { + id: 'a-user', + name: 'John Doe', + slug: 'john-doe', + }, + posts: [ + { + id: 'a-post', + title: 'A post', + content: 'A post', + }, + ], + }, + }, + }) + }) + + describe('the user comments another post', () => { + beforeAll(async () => { + await Factory.build('post', { + id: 'b-post', + title: 'B post', + content: 'B post', + }) + await Factory.build( + 'comment', + { + content: 'A comment to post B', + }, + { + postId: 'b-post', + authorId: 'a-user', + }, + ) + }) + + it('returns the written post and the commented post', async () => { + await expect(query({ query: userDataQuery, variables })).resolves.toMatchObject({ + data: { + userData: { + user: { + id: 'a-user', + name: 'John Doe', + slug: 'john-doe', + }, + posts: [ + { + id: 'a-post', + title: 'A post', + content: 'A post', + }, + { + id: 'b-post', + title: 'B post', + content: 'B post', + comments: [ + { + content: 'A comment to post B', + }, + ], + }, + ], + }, + }, + }) + }) + }) + }) + }) +})