specs for userData resolver

This commit is contained in:
Moriz Wahl 2020-09-29 17:24:43 +02:00
parent f0354c337f
commit 2eff1ab97b
2 changed files with 165 additions and 1 deletions

View File

@ -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)) }

View File

@ -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',
},
],
},
],
},
},
})
})
})
})
})
})