mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
refactor(backend): use apollo test setup (#8750)
.. in two more specs. Apparently this got overlooked. In #8558 these tests will fail.
This commit is contained in:
parent
ee7af8e788
commit
e0719c405e
@ -1,46 +1,38 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
||||||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
||||||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
||||||
import { createTestClient } from 'apollo-server-testing'
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
|
|
||||||
import Factory, { cleanDatabase } from '@db/factories'
|
import Factory, { cleanDatabase } from '@db/factories'
|
||||||
import { getNeode, getDriver } from '@db/neo4j'
|
|
||||||
import { CreateMessage } from '@graphql/queries/CreateMessage'
|
import { CreateMessage } from '@graphql/queries/CreateMessage'
|
||||||
import { createRoomMutation } from '@graphql/queries/createRoomMutation'
|
import { createRoomMutation } from '@graphql/queries/createRoomMutation'
|
||||||
import { roomQuery } from '@graphql/queries/roomQuery'
|
import { roomQuery } from '@graphql/queries/roomQuery'
|
||||||
import { unreadRoomsQuery } from '@graphql/queries/unreadRoomsQuery'
|
import { unreadRoomsQuery } from '@graphql/queries/unreadRoomsQuery'
|
||||||
import createServer from '@src/server'
|
import type { ApolloTestSetup } from '@root/test/helpers'
|
||||||
|
import { createApolloTestSetup } from '@root/test/helpers'
|
||||||
|
import type { Context } from '@src/context'
|
||||||
|
|
||||||
const driver = getDriver()
|
|
||||||
const neode = getNeode()
|
|
||||||
|
|
||||||
let query
|
|
||||||
let mutate
|
|
||||||
let authenticatedUser
|
|
||||||
let chattingUser, otherChattingUser, notChattingUser
|
let chattingUser, otherChattingUser, notChattingUser
|
||||||
|
let authenticatedUser: Context['user']
|
||||||
|
const context = () => ({ authenticatedUser })
|
||||||
|
let mutate: ApolloTestSetup['mutate']
|
||||||
|
let query: ApolloTestSetup['query']
|
||||||
|
let database: ApolloTestSetup['database']
|
||||||
|
let server: ApolloTestSetup['server']
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
await cleanDatabase()
|
await cleanDatabase()
|
||||||
|
const apolloSetup = createApolloTestSetup({ context })
|
||||||
const { server } = createServer({
|
mutate = apolloSetup.mutate
|
||||||
context: () => {
|
query = apolloSetup.query
|
||||||
return {
|
database = apolloSetup.database
|
||||||
driver,
|
server = apolloSetup.server
|
||||||
neode,
|
|
||||||
user: authenticatedUser,
|
|
||||||
cypherParams: {
|
|
||||||
currentUserId: authenticatedUser ? authenticatedUser.id : null,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
})
|
|
||||||
query = createTestClient(server).query
|
|
||||||
mutate = createTestClient(server).mutate
|
|
||||||
})
|
})
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await cleanDatabase()
|
await cleanDatabase()
|
||||||
await driver.close()
|
void server.stop()
|
||||||
|
void database.driver.close()
|
||||||
|
database.neode.close()
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Room', () => {
|
describe('Room', () => {
|
||||||
@ -73,6 +65,10 @@ describe('Room', () => {
|
|||||||
|
|
||||||
describe('create room', () => {
|
describe('create room', () => {
|
||||||
describe('unauthenticated', () => {
|
describe('unauthenticated', () => {
|
||||||
|
beforeAll(() => {
|
||||||
|
authenticatedUser = null
|
||||||
|
})
|
||||||
|
|
||||||
it('throws authorization error', async () => {
|
it('throws authorization error', async () => {
|
||||||
await expect(
|
await expect(
|
||||||
mutate({
|
mutate({
|
||||||
@ -133,13 +129,13 @@ describe('Room', () => {
|
|||||||
userId: 'other-chatting-user',
|
userId: 'other-chatting-user',
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
roomId = result.data.CreateRoom.id
|
roomId = (result.data as any).CreateRoom.id
|
||||||
expect(result).toMatchObject({
|
expect(result).toMatchObject({
|
||||||
errors: undefined,
|
errors: undefined,
|
||||||
data: {
|
data: {
|
||||||
CreateRoom: {
|
CreateRoom: {
|
||||||
id: expect.any(String),
|
id: expect.any(String),
|
||||||
roomId: result.data.CreateRoom.id,
|
roomId: (result.data as any).CreateRoom.id,
|
||||||
roomName: 'Other Chatting User',
|
roomName: 'Other Chatting User',
|
||||||
unreadCount: 0,
|
unreadCount: 0,
|
||||||
users: expect.arrayContaining([
|
users: expect.arrayContaining([
|
||||||
@ -215,7 +211,7 @@ describe('Room', () => {
|
|||||||
Room: [
|
Room: [
|
||||||
{
|
{
|
||||||
id: expect.any(String),
|
id: expect.any(String),
|
||||||
roomId: result.data.Room[0].id,
|
roomId: (result.data as any).Room[0].id,
|
||||||
roomName: 'Other Chatting User',
|
roomName: 'Other Chatting User',
|
||||||
users: expect.arrayContaining([
|
users: expect.arrayContaining([
|
||||||
{
|
{
|
||||||
@ -255,7 +251,7 @@ describe('Room', () => {
|
|||||||
Room: [
|
Room: [
|
||||||
{
|
{
|
||||||
id: expect.any(String),
|
id: expect.any(String),
|
||||||
roomId: result.data.Room[0].id,
|
roomId: (result.data as any).Room[0].id,
|
||||||
roomName: 'Chatting User',
|
roomName: 'Chatting User',
|
||||||
unreadCount: 0,
|
unreadCount: 0,
|
||||||
users: expect.arrayContaining([
|
users: expect.arrayContaining([
|
||||||
@ -325,7 +321,7 @@ describe('Room', () => {
|
|||||||
userId: 'not-chatting-user',
|
userId: 'not-chatting-user',
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
otherRoomId = result.data.CreateRoom.roomId
|
otherRoomId = (result.data as any).CreateRoom.roomId
|
||||||
await mutate({
|
await mutate({
|
||||||
mutation: CreateMessage,
|
mutation: CreateMessage,
|
||||||
variables: {
|
variables: {
|
||||||
@ -354,7 +350,7 @@ describe('Room', () => {
|
|||||||
userId: 'not-chatting-user',
|
userId: 'not-chatting-user',
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
otherRoomId = result2.data.CreateRoom.roomId
|
otherRoomId = (result2.data as any).CreateRoom.roomId
|
||||||
await mutate({
|
await mutate({
|
||||||
mutation: CreateMessage,
|
mutation: CreateMessage,
|
||||||
variables: {
|
variables: {
|
||||||
@ -591,7 +587,6 @@ describe('Room', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('query single room', () => {
|
describe('query single room', () => {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
||||||
let result: any = null
|
let result: any = null
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
|
|||||||
@ -1,21 +1,25 @@
|
|||||||
/* eslint-disable @typescript-eslint/await-thenable */
|
|
||||||
/* eslint-disable @typescript-eslint/require-await */
|
/* eslint-disable @typescript-eslint/require-await */
|
||||||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
||||||
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
||||||
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
||||||
import { createTestClient } from 'apollo-server-testing'
|
|
||||||
import gql from 'graphql-tag'
|
import gql from 'graphql-tag'
|
||||||
|
|
||||||
import Factory, { cleanDatabase } from '@db/factories'
|
import Factory, { cleanDatabase } from '@db/factories'
|
||||||
import { getNeode, getDriver } from '@db/neo4j'
|
import type { ApolloTestSetup } from '@root/test/helpers'
|
||||||
import createServer from '@src/server'
|
import { createApolloTestSetup } from '@root/test/helpers'
|
||||||
|
import type { Context } from '@src/context'
|
||||||
const neode = getNeode()
|
|
||||||
const driver = getDriver()
|
|
||||||
|
|
||||||
const categoryIds = ['cat9']
|
const categoryIds = ['cat9']
|
||||||
let query, graphqlQuery, authenticatedUser, user, moderator, troll
|
let graphqlQuery
|
||||||
|
let moderator
|
||||||
|
let user
|
||||||
|
let troll
|
||||||
|
let authenticatedUser: Context['user']
|
||||||
|
const context = () => ({ authenticatedUser })
|
||||||
|
let query: ApolloTestSetup['query']
|
||||||
|
let database: ApolloTestSetup['database']
|
||||||
|
let server: ApolloTestSetup['server']
|
||||||
|
|
||||||
const action = () => {
|
const action = () => {
|
||||||
return query({ query: graphqlQuery })
|
return query({ query: graphqlQuery })
|
||||||
@ -23,8 +27,15 @@ const action = () => {
|
|||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
await cleanDatabase()
|
await cleanDatabase()
|
||||||
|
const apolloSetup = createApolloTestSetup({ context })
|
||||||
|
query = apolloSetup.query
|
||||||
|
database = apolloSetup.database
|
||||||
|
server = apolloSetup.server
|
||||||
|
|
||||||
// For performance reasons we do this only once
|
// For performance reasons we do this only once
|
||||||
|
const avatar = await Factory.build('image', {
|
||||||
|
url: 'http://localhost/some/offensive/avatar.jpg',
|
||||||
|
})
|
||||||
const users = await Promise.all([
|
const users = await Promise.all([
|
||||||
Factory.build('user', { id: 'u1', role: 'user' }),
|
Factory.build('user', { id: 'u1', role: 'user' }),
|
||||||
Factory.build(
|
Factory.build(
|
||||||
@ -47,12 +58,10 @@ beforeAll(async () => {
|
|||||||
about: 'This self description is very offensive',
|
about: 'This self description is very offensive',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
avatar: Factory.build('image', {
|
avatar,
|
||||||
url: 'http://localhost/some/offensive/avatar.jpg',
|
|
||||||
}),
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
neode.create('Category', {
|
database.neode.create('Category', {
|
||||||
id: 'cat9',
|
id: 'cat9',
|
||||||
name: 'Democracy & Politics',
|
name: 'Democracy & Politics',
|
||||||
icon: 'university',
|
icon: 'university',
|
||||||
@ -136,18 +145,6 @@ beforeAll(async () => {
|
|||||||
),
|
),
|
||||||
])
|
])
|
||||||
|
|
||||||
const { server } = createServer({
|
|
||||||
context: () => {
|
|
||||||
return {
|
|
||||||
driver,
|
|
||||||
neode,
|
|
||||||
user: authenticatedUser,
|
|
||||||
}
|
|
||||||
},
|
|
||||||
})
|
|
||||||
const client = createTestClient(server)
|
|
||||||
query = client.query
|
|
||||||
|
|
||||||
const trollingPost = resources[1]
|
const trollingPost = resources[1]
|
||||||
const trollingComment = resources[2]
|
const trollingComment = resources[2]
|
||||||
|
|
||||||
@ -202,7 +199,9 @@ beforeAll(async () => {
|
|||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await cleanDatabase()
|
await cleanDatabase()
|
||||||
await driver.close()
|
void server.stop()
|
||||||
|
void database.driver.close()
|
||||||
|
database.neode.close()
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('softDeleteMiddleware', () => {
|
describe('softDeleteMiddleware', () => {
|
||||||
@ -222,7 +221,7 @@ describe('softDeleteMiddleware', () => {
|
|||||||
}
|
}
|
||||||
`
|
`
|
||||||
const { data } = await action()
|
const { data } = await action()
|
||||||
subject = data.User[0].following[0].comments[0]
|
subject = (data as any).User[0].following[0].comments[0]
|
||||||
}
|
}
|
||||||
const beforeUser = async () => {
|
const beforeUser = async () => {
|
||||||
graphqlQuery = gql`
|
graphqlQuery = gql`
|
||||||
@ -240,7 +239,7 @@ describe('softDeleteMiddleware', () => {
|
|||||||
}
|
}
|
||||||
`
|
`
|
||||||
const { data } = await action()
|
const { data } = await action()
|
||||||
subject = data.User[0].following[0]
|
subject = (data as any).User[0].following[0]
|
||||||
}
|
}
|
||||||
const beforePost = async () => {
|
const beforePost = async () => {
|
||||||
graphqlQuery = gql`
|
graphqlQuery = gql`
|
||||||
@ -261,7 +260,7 @@ describe('softDeleteMiddleware', () => {
|
|||||||
}
|
}
|
||||||
`
|
`
|
||||||
const { data } = await action()
|
const { data } = await action()
|
||||||
subject = data.User[0].following[0].contributions[0]
|
subject = (data as any).User[0].following[0].contributions[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('as moderator', () => {
|
describe('as moderator', () => {
|
||||||
@ -276,10 +275,11 @@ describe('softDeleteMiddleware', () => {
|
|||||||
it('displays slug', () => expect(subject.slug).toEqual('offensive-name'))
|
it('displays slug', () => expect(subject.slug).toEqual('offensive-name'))
|
||||||
it('displays about', () =>
|
it('displays about', () =>
|
||||||
expect(subject.about).toEqual('This self description is very offensive'))
|
expect(subject.about).toEqual('This self description is very offensive'))
|
||||||
it('displays avatar', () =>
|
it('displays avatar', async () => {
|
||||||
expect(subject.avatar).toEqual({
|
expect(subject.avatar).toEqual({
|
||||||
url: expect.stringMatching('http://localhost/some/offensive/avatar.jpg'),
|
url: expect.stringMatching('http://localhost/some/offensive/avatar.jpg'),
|
||||||
}))
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Post', () => {
|
describe('Post', () => {
|
||||||
@ -369,10 +369,9 @@ describe('softDeleteMiddleware', () => {
|
|||||||
|
|
||||||
it('shows disabled but hides deleted posts', async () => {
|
it('shows disabled but hides deleted posts', async () => {
|
||||||
const expected = [{ title: 'Disabled post' }, { title: 'Publicly visible post' }]
|
const expected = [{ title: 'Disabled post' }, { title: 'Publicly visible post' }]
|
||||||
const {
|
const { data } = await action()
|
||||||
data: { Post },
|
const { Post } = data as any
|
||||||
} = await action()
|
expect(Post).toEqual(expect.arrayContaining(expected))
|
||||||
await expect(Post).toEqual(expect.arrayContaining(expected))
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -400,12 +399,11 @@ describe('softDeleteMiddleware', () => {
|
|||||||
{ content: 'Enabled comment on public post' },
|
{ content: 'Enabled comment on public post' },
|
||||||
{ content: 'UNAVAILABLE' },
|
{ content: 'UNAVAILABLE' },
|
||||||
]
|
]
|
||||||
|
const { data } = await action()
|
||||||
const {
|
const {
|
||||||
data: {
|
Post: [{ comments }],
|
||||||
Post: [{ comments }],
|
} = data as any
|
||||||
},
|
expect(comments).toEqual(expect.arrayContaining(expected))
|
||||||
} = await action()
|
|
||||||
await expect(comments).toEqual(expect.arrayContaining(expected))
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -419,12 +417,11 @@ describe('softDeleteMiddleware', () => {
|
|||||||
{ content: 'Enabled comment on public post' },
|
{ content: 'Enabled comment on public post' },
|
||||||
{ content: 'Disabled comment' },
|
{ content: 'Disabled comment' },
|
||||||
]
|
]
|
||||||
|
const { data } = await action()
|
||||||
const {
|
const {
|
||||||
data: {
|
Post: [{ comments }],
|
||||||
Post: [{ comments }],
|
} = data as any
|
||||||
},
|
expect(comments).toEqual(expect.arrayContaining(expected))
|
||||||
} = await action()
|
|
||||||
await expect(comments).toEqual(expect.arrayContaining(expected))
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user