Refactore and cleanup backend tests

- Removed double declarations.
- Put assignments in 'beforeEach'.
This commit is contained in:
Wolfgang Huß 2020-08-05 08:53:45 +02:00
parent 9ff1fc5b62
commit 17a296b82e

View File

@ -18,35 +18,6 @@ let variables
const driver = getDriver() const driver = getDriver()
const neode = getNeode() const neode = getNeode()
const deleteUserMutation = gql`
mutation($id: ID!, $resource: [Deletable]) {
DeleteUser(id: $id, resource: $resource) {
id
name
about
deleted
contributions {
id
content
contentExcerpt
deleted
comments {
id
content
contentExcerpt
deleted
}
}
comments {
id
content
contentExcerpt
deleted
}
}
}
`
beforeAll(() => { beforeAll(() => {
const { server } = createServer({ const { server } = createServer({
context: () => { context: () => {
@ -67,18 +38,22 @@ beforeEach(async () => {
describe('User', () => { describe('User', () => {
describe('query by email address', () => { describe('query by email address', () => {
beforeEach(async () => { let userQuery
await Factory.build('user', { name: 'Johnny' }, { email: 'any-email-address@example.org' })
})
const userQuery = gql` beforeEach(async () => {
userQuery = gql`
query($email: String) { query($email: String) {
User(email: $email) { User(email: $email) {
name name
} }
} }
` `
const variables = { email: 'any-email-address@example.org' } variables = {
email: 'any-email-address@example.org',
}
await Factory.build('user', { name: 'Johnny' }, { email: 'any-email-address@example.org' })
})
it('is forbidden', async () => { it('is forbidden', async () => {
await expect(query({ query: userQuery, variables })).resolves.toMatchObject({ await expect(query({ query: userQuery, variables })).resolves.toMatchObject({
@ -127,16 +102,10 @@ describe('User', () => {
}) })
describe('UpdateUser', () => { describe('UpdateUser', () => {
let variables let updateUserMutation
beforeEach(async () => { beforeEach(async () => {
variables = { updateUserMutation = gql`
id: 'u47',
name: 'John Doughnut',
}
})
const updateUserMutation = gql`
mutation( mutation(
$id: ID! $id: ID!
$name: String $name: String
@ -157,7 +126,11 @@ describe('UpdateUser', () => {
} }
} }
` `
beforeEach(async () => { variables = {
id: 'u47',
name: 'John Doughnut',
}
user = await Factory.build( user = await Factory.build(
'user', 'user',
{ {
@ -276,7 +249,37 @@ describe('UpdateUser', () => {
}) })
describe('Delete a user', () => { describe('Delete a user', () => {
let deleteUserMutation
beforeEach(async () => { beforeEach(async () => {
deleteUserMutation = gql`
mutation($id: ID!, $resource: [Deletable]) {
DeleteUser(id: $id, resource: $resource) {
id
name
about
deleted
contributions {
id
content
contentExcerpt
deleted
comments {
id
content
contentExcerpt
deleted
}
}
comments {
id
content
contentExcerpt
deleted
}
}
}
`
variables = { id: ' u343', resource: [] } variables = { id: ' u343', resource: [] }
user = await Factory.build('user', { user = await Factory.build('user', {
@ -348,7 +351,7 @@ describe('Delete a user', () => {
describe('deleting a user account', () => { describe('deleting a user account', () => {
beforeEach(() => { beforeEach(() => {
variables = { ...variables, id: 'u343' } variables = { id: 'u343' }
}) })
describe('given posts and comments', () => { describe('given posts and comments', () => {