Skip to content

Commit

Permalink
fix: tests and randomVerse (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
omarciovsena committed May 22, 2020
1 parent d1c483d commit e22ebb7
Show file tree
Hide file tree
Showing 25 changed files with 229 additions and 920 deletions.
8 changes: 6 additions & 2 deletions __test__/app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import express from 'express'

import appRoutes from '../routes/index'

import '../models/book'
import '../models/verse'
import '../models/request'
import '../models/user'

import { startRedis } from '../controllers/rateLimit'
import appRoutes from '../routes/index'

startRedis()

const app = express()

app.use(express.json())
Expand Down
37 changes: 23 additions & 14 deletions __test__/controllers/book.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,56 @@ import supertest from 'supertest'

import Book from '../../models/book'
import app from '../app'
import { connect } from '../utils'
import { connect, getUser } from '../utils'

jest.mock('axios')
describe('controllers:book', () => {
let connection
let connection, user

beforeAll(async () => {
connection = await connect()
user = await getUser()
})

afterAll(async () => {
return connection.disconnect()
})

it('should have 66 books', async () => {
it('should have 8 books', async () => {
const count = await Book.countDocuments()
expect(count).toEqual(66)
expect(count).toEqual(8)
})

describe('getBook', () => {
it('should return error 404 and "Book not found" message', async () => {
const { body, statusCode } = await supertest(app).get('/books/gns')
const { body, statusCode } = await supertest(app)
.get('/books/app')
.set('Authorization', `Bearer ${user.token}`)
expect(statusCode).toBe(404)
expect(body.msg).toEqual('Book not found')
})

it('should return object named "Juízes"', async () => {
const { body } = await supertest(app).get('/books/jz')
expect(body.name).toBe('Juízes')
it('should return object named Tiago (James)', async () => {
const { body } = await supertest(app)
.get('/books/tg')
.set('Authorization', `Bearer ${user.token}`)
expect(body.name).toBe('Tiago')
})

it('should return object named Juizes', async () => {
const { body } = await supertest(app).get('/books/jud')
expect(body.name).toBe('Juízes')
it('should return object named Tiago (James)', async () => {
const { body } = await supertest(app)
.get('/books/jm')
.set('Authorization', `Bearer ${user.token}`)
expect(body.name).toBe('Tiago')
})
})

describe('getBooks', () => {
it('should return 66 books', async () => {
const { body } = await supertest(app).get('/books')
expect(body.length).toBe(66)
it('should return 8 books', async () => {
const { body } = await supertest(app)
.get('/books')
.set('Authorization', `Bearer ${user.token}`)
expect(body.length).toBe(8)
})
})
})
16 changes: 12 additions & 4 deletions __test__/controllers/request.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import supertest from 'supertest'

import { saveRequest } from '../../controllers/request'
import Request from '../../models/request'
import User from '../../models/user'
import app from '../app'
import { connect, resetDatabase } from '../utils'
import { connect, getUser } from '../utils'

jest.mock('axios')
describe('controllers:request', () => {
Expand All @@ -12,8 +12,7 @@ describe('controllers:request', () => {

beforeAll(async () => {
connection = await connect()
await resetDatabase()
user = await User.findOne()
user = await getUser()
})

afterAll(async () => {
Expand Down Expand Up @@ -50,4 +49,13 @@ describe('controllers:request', () => {
expect(body.msg).toEqual('Not authorized token')
})
})

describe('saveRequest', () => {
it('should have the same amount of requests saved', async () => {
const numberOfRequestsBefore = await Request.countDocuments()
await saveRequest(null)
const numberOfRequestsAfter = await Request.countDocuments()
expect(numberOfRequestsBefore).toEqual(numberOfRequestsAfter)
})
})
})
7 changes: 4 additions & 3 deletions __test__/controllers/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import supertest from 'supertest'
import { generateToken } from '../../controllers/session'
import User from '../../models/user'
import app from '../app'
import { connect, resetDatabase } from '../utils'
import { connect, getUser } from '../utils'

jest.mock('axios')
describe('controllers:user', () => {
Expand All @@ -14,11 +14,12 @@ describe('controllers:user', () => {

beforeAll(async () => {
connection = await connect()
await resetDatabase()
user = await User.findOne()
await User.deleteMany({ email: { $in: [/^fake/i] } })
user = await getUser()
})

afterAll(async () => {
await User.deleteMany({ email: { $in: [/^fake/i] } })
return connection.disconnect()
})

Expand Down
94 changes: 69 additions & 25 deletions __test__/controllers/verse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,136 @@ import supertest from 'supertest'

import Verse from '../../models/verse'
import app from '../app'
import { connect } from '../utils'
import { connect, getUser } from '../utils'

jest.mock('axios')
describe('controllers:verse', () => {
let connection
let connection, user

beforeAll(async () => {
connection = await connect()
user = await getUser()
})

afterAll(async () => {
return connection.disconnect()
})

it('should have 31.105 verses', async done => {
it('should have 837 verses', async done => {
const count = await Verse.countDocuments()
expect(count).toEqual(31105)
expect(count).toEqual(837)
done()
})

describe('getChapter', () => {
it('should return object with 6 verses and with the book of Salmos', async () => {
const { body } = await supertest(app).get('/verses/nvi/sl/23')
expect(body.verses.length).toBe(6)
expect(body.book.name).toBe('Salmos')
it('should return object with 27 verses and with the book of Tiago (James)', async () => {
const { body } = await supertest(app)
.get('/verses/nvi/tg/1')
.set('Authorization', `Bearer ${user.token}`)
expect(body.verses.length).toBe(27)
expect(body.book.name).toBe('Tiago')
})

it('should return object with 27 verses and with the book of James (Tiago)', async () => {
const { body } = await supertest(app)
.get('/verses/nvi/jm/1')
.set('Authorization', `Bearer ${user.token}`)
expect(body.verses.length).toBe(27)
expect(body.book.name).toBe('Tiago')
})

it('should return error 404 and "Book not found" message', async () => {
const { body, statusCode } = await supertest(app).get('/verses/nvi/fake/23')
const { body, statusCode } = await supertest(app)
.get('/verses/nvi/fake/1')
.set('Authorization', `Bearer ${user.token}`)
expect(statusCode).toBe(404)
expect(body.msg).toBe('Book not found')
})

it('should return error 404 and "Chapter not found" message', async () => {
const { body, statusCode } = await supertest(app).get('/verses/nvi/sl/160')
const { body, statusCode } = await supertest(app)
.get('/verses/nvi/tg/10')
.set('Authorization', `Bearer ${user.token}`)
expect(statusCode).toBe(404)
expect(body.msg).toBe('Chapter not found')
})
})

describe('getVerse', () => {
it('should return error 404 and "Verse not found" message', async () => {
const { body, statusCode } = await supertest(app).get('/verses/nvi/sl/23/50')
const { body, statusCode } = await supertest(app)
.get('/verses/nvi/tg/1/100')
.set('Authorization', `Bearer ${user.token}`)
expect(statusCode).toBe(404)
expect(body.msg).toBe('Verse not found')
})

it('should return error 404 and "Book not found" message', async () => {
const { body, statusCode } = await supertest(app).get('/verses/nvi/fake/23/1')
const { body, statusCode } = await supertest(app)
.get('/verses/nvi/fake/23/1')
.set('Authorization', `Bearer ${user.token}`)
expect(statusCode).toBe(404)
expect(body.msg).toBe('Book not found')
})

it('should return object with text and with the book of Mateus', async () => {
const { body } = await supertest(app).get('/verses/nvi/mt/28/19')
it('should return object with text and with the book of Tiago', async () => {
const { body } = await supertest(app)
.get('/verses/nvi/tg/1/1')
.set('Authorization', `Bearer ${user.token}`)
expect(body.text).toBe(
'Tiago, servo de Deus e do Senhor Jesus Cristo, às doze tribos dispersas entre as nações: Saudações.'
)
expect(body.book.name).toBe('Tiago')
})

it('should return object with text and with the book of James', async () => {
const { body } = await supertest(app)
.get('/verses/nvi/jm/1/1')
.set('Authorization', `Bearer ${user.token}`)
expect(body.text).toBe(
'Portanto, vão e façam discípulos de todas as nações, batizando-os em nome do Pai e do Filho e do Espírito Santo,'
'Tiago, servo de Deus e do Senhor Jesus Cristo, às doze tribos dispersas entre as nações: Saudações.'
)
expect(body.book.name).toBe('Mateus')
expect(body.book.name).toBe('Tiago')
})
})

describe('getRandomVerse', () => {
it('should return object with 1 verse', async () => {
const { body } = await supertest(app)
.get('/verses/nvi/random')
.set('Authorization', `Bearer ${user.token}`)
expect(body.text.length > 0).toBeTruthy()
})
})

describe('search', () => {
it('should return error 404 and "Version not found" message', async () => {
const { body, statusCode } = await supertest(app).post('/verses/search').send({ search: 'No princípio' })
const { body, statusCode } = await supertest(app).post('/verses/search')
.send({ search: 'No princípio' })
.set('Authorization', `Bearer ${user.token}`)
expect(statusCode).toBe(404)
expect(body.msg).toBe('Version not found')
})

it('should return 5 occurences', async () => {
it('should return 16 occurences', async () => {
const { body } = await supertest(app)
.post('/verses/search')
.send({ version: 'nvi', search: 'No princípio' })
expect(body.occurrence).toBe(5)
.send({ version: 'nvi', search: 'luz' })
.set('Authorization', `Bearer ${user.token}`)
expect(body.occurrence).toBe(16)
expect(body.verses[0].text).toBe(
'No princípio Deus criou os céus e a terra.'
'Então a cobiça, tendo engravidado, dá à luz o pecado; e o pecado, após ter-se consumado, gera a morte.'
)
})

it('should return 5 occurences - deprecated', async () => {
it('should return 16 occurences - deprecated', async () => {
const { body } = await supertest(app)
.post('/search')
.send({ version: 'nvi', search: 'No princípio' })
expect(body.occurrence).toBe(5)
.send({ version: 'nvi', search: 'luz' })
.set('Authorization', `Bearer ${user.token}`)
expect(body.occurrence).toBe(16)
expect(body.verses[0].text).toBe(
'No princípio Deus criou os céus e a terra.'
'Então a cobiça, tendo engravidado, dá à luz o pecado; e o pecado, após ter-se consumado, gera a morte.'
)
})

Expand Down
14 changes: 14 additions & 0 deletions __test__/helpers/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { randomNumber } from '../../helpers/'

jest.mock('axios')
describe('helpers:randomNumber', () => {
it('must return a number less than 5', async () => {
const response = randomNumber(5)
expect(response < 5).toBeTruthy()
})

it('must be greater than 0', async () => {
const response = randomNumber(0)
expect(response > 0).toBeTruthy()
})
})
61 changes: 61 additions & 0 deletions __test__/initDatabase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import mongoose from 'mongoose'

import Book from '../models/book'
import Request from '../models/request'
import User from '../models/user'
import Verse from '../models/verse'
import books from './mock/books'
import verses from './mock/verses'

export const connect = async () => {
return mongoose.connect('mongodb://localhost/bibleapi_test', {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
})
}

const createBooks = async () => {
const promises = books.map(async book => new Book(book).save())
return Promise.all(promises)
}

const createVerses = async (books) => {
const bookByKey = {}
await books.map(book => {
bookByKey[book.abbrev.en] = book
})

const promises = verses.map(async verse => {
verse.book = {
id: bookByKey[verse.book.abbrev.en]._id,
abbrev: bookByKey[verse.book.abbrev.en].abbrev
}
return new Verse(verse).save()
})
return Promise.all(promises)
}

const createUser = async () => {
return new User({
token: '1234567890',
name: 'Fake User',
email: 'root@bibleapi.co',
password: '123456',
notifications: false
}).save()
}

export const initDatabase = async () => {
const connection = await connect()
await User.deleteMany()
await Book.deleteMany()
await Verse.deleteMany()
await Request.deleteMany()
const books = await createBooks()
await createVerses(books)
await createUser()
connection.disconnect()
}

initDatabase()
1 change: 1 addition & 0 deletions __test__/mock/books.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"abbrev":{"pt":"tg","en":"jm"},"name":"Tiago","chapters":5},{"abbrev":{"pt":"1pe","en":"1pe"},"name":"1 Pedro","chapters":5},{"abbrev":{"pt":"2pe","en":"2pe"},"name":"2 Pedro","chapters":3},{"abbrev":{"pt":"1jo","en":"1jo"},"name":"1 João","chapters":5},{"abbrev":{"pt":"2jo","en":"2jo"},"name":"2 João","chapters":1},{"abbrev":{"pt":"3jo","en":"3jo"},"name":"3 João","chapters":1},{"abbrev":{"pt":"jd","en":"jd"},"name":"Judas","chapters":1},{"abbrev":{"pt":"ap","en":"re"},"name":"Apocalipse","chapters":22}]
1 change: 1 addition & 0 deletions __test__/mock/verses.json

Large diffs are not rendered by default.

0 comments on commit e22ebb7

Please sign in to comment.