mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
set up unit tests for messages
This commit is contained in:
parent
ae131cc656
commit
d2beca22c9
32
backend/src/graphql/messages.ts
Normal file
32
backend/src/graphql/messages.ts
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
}
|
||||
93
backend/src/schema/resolvers/messages.spec.ts
Normal file
93
backend/src/schema/resolvers/messages.spec.ts
Normal 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,
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -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 { .* }
|
||||
`
|
||||
Loading…
x
Reference in New Issue
Block a user