Refactor badges test

CC @Tirokk @grenzfrequence

* the top level block should correspond with the name of the resolver
  * the block below should be `CreatePost` or `UpdatePost`
* the arguments of client.request are `query/mutation`, `variables` but
  you passed in the `headers` which should go into `new
  GraphQlClient(host, options)`
* re-use the very same mutation to avoid bugs in the tests
* use `await expect(someAsyncMethod).resolves.toEqual(expected)` style for
  extra test assurance
This commit is contained in:
Robert Schäfer 2019-03-03 14:01:50 +01:00
parent 6937c60ef8
commit f25708875a

View File

@ -4,7 +4,7 @@ import { host, login } from '../jest/helpers'
const factory = Factory()
describe('Badge', () => {
describe('badges', () => {
beforeEach(async () => {
await factory.create('User', {
email: 'user@example.org',
@ -27,82 +27,68 @@ describe('Badge', () => {
await factory.cleanDatabase()
})
const params = {
id: 'b1',
key: 'indiegogo_en_racoon',
type: 'crowdfunding',
status: 'permanent',
icon: '/img/badges/indiegogo_en_racoon.svg'
}
describe('CreateBadge', () => {
const params = {
id: 'b1',
key: 'indiegogo_en_racoon',
type: 'crowdfunding',
status: 'permanent',
icon: '/img/badges/indiegogo_en_racoon.svg'
}
describe('unauthenticated', () => {
let client
const mutation = `
mutation(
$id: ID
$key: String!
$type: BadgeTypeEnum!
$status: BadgeStatusEnum!
$icon: String!
) {
CreateBadge(id: $id, key: $key, type: $type, status: $status, icon: $icon) {
id
}
}
`
it('throws authorization error', async () => {
client = new GraphQLClient(host)
let { id, key, type, status, icon } = params
await expect(
client.request(`mutation {
CreateBadge(
id: "${id}",
key: "${key}",
type: ${type},
status: ${status},
icon: "${icon}"
) { id }
}`)
).rejects.toThrow('Not Authorised')
describe('unauthenticated', () => {
let client
it('throws authorization error', async () => {
client = new GraphQLClient(host)
await expect(
client.request(mutation, params)
).rejects.toThrow('Not Authorised')
})
})
})
describe('authenticated admin', () => {
let client
let headers
let response
let { id, key, type, status, icon } = params
beforeEach(async () => {
headers = await login({ email: 'admin@example.org', password: '1234' })
client = new GraphQLClient(host, { headers })
response = await client.request(`mutation {
CreateBadge(
id: "${id}",
key: "${key}",
type: ${type},
status: ${status},
icon: "${icon}"
) { id }
}`,
{ headers }
)
describe('authenticated admin', () => {
let client
beforeEach(async () => {
const headers = await login({ email: 'admin@example.org', password: '1234' })
client = new GraphQLClient(host, { headers })
})
it('creates a badge', async () => {
const expected = {
CreateBadge: {
id: 'b1'
}
}
await expect(client.request(mutation, params)).resolves.toEqual(expected)
})
})
it('creates a badge', () => {
let { id } = response.CreateBadge
expect(response).toEqual({
CreateBadge: { id }
describe('authenticated moderator', () => {
let client
beforeEach(async () => {
const headers = await login({ email: 'moderator@example.org', password: '1234' })
client = new GraphQLClient(host, { headers })
})
it('throws authorization error', async () => {
await expect(
client.request(mutation, params)
).rejects.toThrow('Not Authorised')
})
})
})
describe('authenticated moderator', () => {
let client
let headers
let { id, key, type, status, icon } = params
beforeEach(async () => {
headers = await login({ email: 'moderator@example.org', password: '1234' })
client = new GraphQLClient(host, { headers })
})
it('throws authorization error', async () => {
await expect(client.request(`mutation {
CreateBadge(
id: "${id}",
key: "${key}",
type: ${type},
status: ${status},
icon: "${icon}"
) { id }
}`,
{ headers }
)).rejects.toThrow('Not Authorised')
})
})
})