Skip to content

Commit

Permalink
change graphql tests to use supertest
Browse files Browse the repository at this point in the history
  • Loading branch information
chumarrento committed Jan 28, 2022
1 parent 6719b9f commit f4efa38
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 266 deletions.
65 changes: 0 additions & 65 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"@types/swagger-ui-express": "^4.1.3",
"@types/validator": "^13.7.1",
"@typescript-eslint/eslint-plugin": "^4.0.1",
"apollo-server-integration-testing": "^2.3.0",
"bson-objectid": "^2.0.2",
"copyfiles": "^2.4.1",
"coveralls": "^3.1.1",
Expand Down
12 changes: 6 additions & 6 deletions tests/domain/mocks/mock-survey-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ export const mockSurveyResultModel = (): SurveyResultModel => ({
question: faker.random.words(),
answers: [{
answer: faker.random.word(),
count: faker.random.number({ min: 0, max: 1000 }),
percent: faker.random.number({ min: 0, max: 100 }),
isCurrentAccountAnswer: faker.random.boolean()
count: faker.datatype.number({ min: 0, max: 1000 }),
percent: faker.datatype.number({ min: 0, max: 100 }),
isCurrentAccountAnswer: faker.datatype.boolean()
}, {
answer: faker.random.word(),
image: faker.image.imageUrl(),
count: faker.random.number({ min: 0, max: 1000 }),
percent: faker.random.number({ min: 0, max: 100 }),
isCurrentAccountAnswer: faker.random.boolean()
count: faker.datatype.number({ min: 0, max: 1000 }),
percent: faker.datatype.number({ min: 0, max: 100 }),
isCurrentAccountAnswer: faker.datatype.boolean()
}],
date: faker.date.recent()
})
Expand Down
12 changes: 0 additions & 12 deletions tests/main/graphql/helpers.ts

This file was deleted.

96 changes: 35 additions & 61 deletions tests/main/graphql/login.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { MongoHelper } from '@/infra/db'
import { makeApolloServer } from './helpers'
import request from 'supertest'
import app from '@/main/config/app'

import { createTestClient } from 'apollo-server-integration-testing'
import { Collection } from 'mongodb'
import { hash } from 'bcrypt'
import { ApolloServer, gql } from 'apollo-server-express'

let accountCollection: Collection
let apolloServer: ApolloServer

describe('Login GraphQL', () => {
beforeAll(async () => {
apolloServer = makeApolloServer()
await MongoHelper.connect(process.env.MONGO_URL)
})

Expand All @@ -25,14 +22,12 @@ describe('Login GraphQL', () => {
})

describe('Login Query', () => {
const loginQuery = gql`
query login ($email: String!, $password: String!) {
login (email: $email, password: $password) {
accessToken
name
}
}
`
const query = `query {
login (email: "test_email@email.com", password: "secret") {
accessToken
name
}
}`

test('Should return an Account on valid credentials', async () => {
const password = await hash('secret', 12)
Expand All @@ -41,52 +36,37 @@ describe('Login GraphQL', () => {
email: 'test_email@email.com',
password
})
const { query } = createTestClient({ apolloServer })
const res: any = await query(loginQuery, {
variables: {
email: 'test_email@email.com',
password: 'secret'
}
})
expect(res.data.login.accessToken).toBeTruthy()
expect(res.data.login.name).toBe('Test Name')

const res = await request(app).post('/graphql').send({ query })

expect(res.status).toBe(200)
expect(res.body.data.login.accessToken).toBeTruthy()
expect(res.body.data.login.name).toBe('Test Name')
})

test('Should return Unauthorized error on invalid credentials', async () => {
const { query } = createTestClient({ apolloServer })
const res: any = await query(loginQuery, {
variables: {
email: 'test_email@email.com',
password: 'secret'
}
})
expect(res.data).toBeFalsy()
expect(res.errors[0].message).toBe('Unauthorized')
const res = await request(app).post('/graphql').send({ query })

expect(res.status).toBe(401)
expect(res.body.data).toBeFalsy()
expect(res.body.errors[0].message).toBe('Unauthorized')
})
})

describe('SignUp Mutation', () => {
const signUpMutation = gql`
mutation signUp ($name: String!, $email: String!, $password: String!, $passwordConfirmation: String!) {
signUp (name: $name, email: $email, password: $password, passwordConfirmation: $passwordConfirmation) {
accessToken
name
}
}
`
const query = `mutation {
signUp (name: "Test Name", email: "test_email@email.com", password: "secret", passwordConfirmation: "secret") {
accessToken
name
}
}`

test('Should return an Account on valid data', async () => {
const { mutate } = createTestClient({ apolloServer })
const res: any = await mutate(signUpMutation, {
variables: {
name: 'Test Name',
email: 'test_email@email.com',
password: 'secret',
passwordConfirmation: 'secret'
}
})
expect(res.data.signUp.accessToken).toBeTruthy()
expect(res.data.signUp.name).toBe('Test Name')
const res = await request(app).post('/graphql').send({ query })

expect(res.status).toBe(200)
expect(res.body.data.signUp.accessToken).toBeTruthy()
expect(res.body.data.signUp.name).toBe('Test Name')
})

test('Should return EmailInUseError on invalid data', async () => {
Expand All @@ -96,17 +76,11 @@ describe('Login GraphQL', () => {
email: 'test_email@email.com',
password
})
const { mutate } = createTestClient({ apolloServer })
const res: any = await mutate(signUpMutation, {
variables: {
name: 'Test Name',
email: 'test_email@email.com',
password: 'secret',
passwordConfirmation: 'secret'
}
})
expect(res.data).toBeFalsy()
expect(res.errors[0].message).toBe('The received email is already in use')
const res = await request(app).post('/graphql').send({ query })

expect(res.status).toBe(403)
expect(res.body.data).toBeFalsy()
expect(res.body.errors[0].message).toBe('The received email is already in use')
})
})
})
Loading

0 comments on commit f4efa38

Please sign in to comment.