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