set up unit tests for messages

This commit is contained in:
Moriz Wahl 2023-06-14 17:27:42 +02:00
parent ae131cc656
commit d2beca22c9
3 changed files with 126 additions and 1 deletions

View File

@ -0,0 +1,32 @@
import gql from 'graphql-tag'
export const createMessageMutation = () => {
return gql`
mutation (
$roomId: ID!
$content: String!
) {
CreateMessage(
roomId: $roomId
content: $content
) {
id
content
}
}
`
}
export const messageQuery = () => {
return gql`
query($roomId: ID!) {
Message(roomId: $roomId) {
id
content
author {
id
}
}
}
`
}

View File

@ -0,0 +1,93 @@
import { createTestClient } from 'apollo-server-testing'
import Factory, { cleanDatabase } from '../../db/factories'
import { getNeode, getDriver } from '../../db/neo4j'
import { createRoomMutation } from '../../graphql/rooms'
import { createMessageMutation, messageQuery } from '../../graphql/messages'
import createServer from '../../server'
const driver = getDriver()
const neode = getNeode()
let query
let mutate
let authenticatedUser
let chattingUser, otherChattingUser, notChattingUser
beforeAll(async () => {
await cleanDatabase()
const { server } = createServer({
context: () => {
return {
driver,
neode,
user: authenticatedUser,
}
},
})
query = createTestClient(server).query
mutate = createTestClient(server).mutate
})
afterAll(async () => {
// await cleanDatabase()
driver.close()
})
describe('Message', () => {
beforeAll(async () => {
;[chattingUser, otherChattingUser, notChattingUser] = await Promise.all([
Factory.build(
'user',
{
id: 'chatting-user',
name: 'Chatting User',
},
),
Factory.build(
'user',
{
id: 'other-chatting-user',
name: 'Other Chatting User',
},
),
Factory.build(
'user',
{
id: 'not-chatting-user',
name: 'Not Chatting User',
},
),
])
})
describe('create message', () => {
describe('unauthenticated', () => {
it('throws authorization error', async () => {
await expect(mutate({ mutation: createMessageMutation(), variables: {
roomId: 'some-id', content: 'Some bla bla bla', } })).resolves.toMatchObject({
errors: [{ message: 'Not Authorized!' }],
})
})
})
describe('authenticated', () => {
beforeAll(async () => {
authenticatedUser = await chattingUser.toJson()
})
describe('room does not exist', () => {
it('returns null', async () => {
await expect(mutate({ mutation: createMessageMutation(), variables: {
roomId: 'some-id', content: 'Some bla bla bla', } })).resolves.toMatchObject({
errors: undefined,
data: {
CreateMessage: null,
},
})
})
})
})
})
})

View File

@ -24,7 +24,7 @@ export default {
MATCH (currentUser:User { id: $currentUserId })-[:CHATS_IN]->(room:Room { id: $roomId })
MERGE (currentUser)-[:CREATED]->(message:Message)-[:INSIDE]->(room)
SET message.createdAt = toString(datetime()),
message.id = $messageId
message.id = $messageId,
message.content = $content
RETURN message { .* }
`