mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
test roomms resolver
This commit is contained in:
parent
761aa74498
commit
e96b9426d1
28
backend/src/graphql/rooms.ts
Normal file
28
backend/src/graphql/rooms.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import gql from 'graphql-tag'
|
||||||
|
|
||||||
|
export const createRoomMutation = () => {
|
||||||
|
return gql`
|
||||||
|
mutation (
|
||||||
|
$userId: ID!
|
||||||
|
) {
|
||||||
|
CreateRoom(
|
||||||
|
userId: $userId
|
||||||
|
) {
|
||||||
|
id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
}
|
||||||
|
|
||||||
|
export const roomQuery = () => {
|
||||||
|
return gql`
|
||||||
|
query {
|
||||||
|
Room {
|
||||||
|
id
|
||||||
|
users {
|
||||||
|
id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
}
|
||||||
198
backend/src/schema/resolvers/rooms.spec.ts
Normal file
198
backend/src/schema/resolvers/rooms.spec.ts
Normal file
@ -0,0 +1,198 @@
|
|||||||
|
import { createTestClient } from 'apollo-server-testing'
|
||||||
|
import Factory, { cleanDatabase } from '../../db/factories'
|
||||||
|
import { getNeode, getDriver } from '../../db/neo4j'
|
||||||
|
import { createRoomMutation, roomQuery } from '../../graphql/rooms'
|
||||||
|
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('Room', () => {
|
||||||
|
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 room', () => {
|
||||||
|
describe('unauthenticated', () => {
|
||||||
|
it('throws authorization error', async () => {
|
||||||
|
await expect(mutate({ mutation: createRoomMutation(), variables: {
|
||||||
|
userId: 'some-id' } })).resolves.toMatchObject({
|
||||||
|
errors: [{ message: 'Not Authorized!' }],
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('authenticated', () => {
|
||||||
|
beforeAll(async () => {
|
||||||
|
authenticatedUser = await chattingUser.toJson()
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('user id does not exist', () => {
|
||||||
|
it('returns null', async () => {
|
||||||
|
await expect(mutate({
|
||||||
|
mutation: createRoomMutation(),
|
||||||
|
variables: {
|
||||||
|
userId: 'not-existing-user',
|
||||||
|
},
|
||||||
|
})).resolves.toMatchObject({
|
||||||
|
errors: undefined,
|
||||||
|
data: {
|
||||||
|
CreateRoom: null,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('user id exists', () => {
|
||||||
|
it('returns the id of the room', async () => {
|
||||||
|
await expect(mutate({
|
||||||
|
mutation: createRoomMutation(),
|
||||||
|
variables: {
|
||||||
|
userId: 'other-chatting-user',
|
||||||
|
},
|
||||||
|
})).resolves.toMatchObject({
|
||||||
|
errors: undefined,
|
||||||
|
data: {
|
||||||
|
CreateRoom: {
|
||||||
|
id: expect.any(String),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('query room', () => {
|
||||||
|
describe('unauthenticated', () => {
|
||||||
|
beforeAll(() => {
|
||||||
|
authenticatedUser = null
|
||||||
|
})
|
||||||
|
|
||||||
|
it('throws authorization error', async () => {
|
||||||
|
await expect(query({ query: roomQuery() })).resolves.toMatchObject({
|
||||||
|
errors: [{ message: 'Not Authorized!' }],
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('authenticated', () => {
|
||||||
|
describe('as creater of room', () => {
|
||||||
|
beforeAll(async () => {
|
||||||
|
authenticatedUser = await chattingUser.toJson()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns the room', async () => {
|
||||||
|
await expect(query({ query: roomQuery() })).resolves.toMatchObject({
|
||||||
|
errors: undefined,
|
||||||
|
data: {
|
||||||
|
Room: [
|
||||||
|
{
|
||||||
|
id: expect.any(String),
|
||||||
|
users: expect.arrayContaining([
|
||||||
|
{
|
||||||
|
id: 'chatting-user',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'other-chatting-user',
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('as chatter of room', () => {
|
||||||
|
beforeAll(async () => {
|
||||||
|
authenticatedUser = await otherChattingUser.toJson()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns the room', async () => {
|
||||||
|
await expect(query({ query: roomQuery() })).resolves.toMatchObject({
|
||||||
|
errors: undefined,
|
||||||
|
data: {
|
||||||
|
Room: [
|
||||||
|
{
|
||||||
|
id: expect.any(String),
|
||||||
|
users: expect.arrayContaining([
|
||||||
|
{
|
||||||
|
id: 'chatting-user',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'other-chatting-user',
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('as not chatter of room', () => {
|
||||||
|
beforeAll(async () => {
|
||||||
|
authenticatedUser = await notChattingUser.toJson()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns no rooms', async () => {
|
||||||
|
await expect(query({ query: roomQuery() })).resolves.toMatchObject({
|
||||||
|
errors: undefined,
|
||||||
|
data: {
|
||||||
|
Room: [],
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
Loading…
x
Reference in New Issue
Block a user