Clean up helper.js

This commit is contained in:
Robert Schäfer 2019-01-14 23:31:00 +01:00
parent 6f487e2687
commit 9d541d8f27
2 changed files with 9 additions and 32 deletions

View File

@ -11,28 +11,7 @@ export async function authenticatedHeaders ({ email, password }) {
}
}`
const response = await request(host, mutation)
const { token } = response.login
if (!token) throw new Error(`Could not get a JWT token from the backend:\n${response}`)
return {
authorization: `Bearer ${token}`
authorization: `Bearer ${response.login.token}`
}
}
export async function queryServer ({ headers, query, variables }) {
const defaultHeaders = {
Accept: 'application/json',
'Content-Type': 'application/json'
}
const response = await fetch(host, {
method: 'POST',
authenticatedHeaders,
headers: Object.assign({}, defaultHeaders, headers),
body: JSON.stringify({
operationName: null,
query,
variables
})
})
const json = await response.json()
return json.data
}

View File

@ -1,5 +1,6 @@
import { create, cleanDatabase } from '../seed/factories'
import { authenticatedHeaders, queryServer } from '../jest/helpers'
import { host, authenticatedHeaders, queryServer } from '../jest/helpers'
import { GraphQLClient } from 'graphql-request'
describe('authorization', () => {
describe('given two existing users', () => {
@ -21,15 +22,12 @@ describe('authorization', () => {
describe('access email address', () => {
let headers = {}
const action = async (headers) => {
const options = {
headers,
query: `{
User(email: "owner@example.org") {
email
}
}`
}
return await queryServer(options)
const graphQLClient = new GraphQLClient(host, { headers })
return await graphQLClient.request(`{
User(email: "owner@example.org") {
email
}
}`)
}
describe('not logged in', async () => {