forked from lukePeavey/quotable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration.test.js
141 lines (127 loc) · 4.21 KB
/
integration.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
require('dotenv').config()
const request = require('supertest')
const mongoose = require('mongoose')
const range = require('lodash/range')
const app = require('../src/app')
const Quotes = require('../src/models/Quotes')
const { MONGODB_URI } = process.env
beforeAll(async () => {
try {
await mongoose.connect(MONGODB_URI, {
useUnifiedTopology: true,
useNewUrlParser: true,
useCreateIndex: true,
})
} catch (error) {
process.exit(1)
}
})
afterAll(() => {
// close the mongo connection after tests
mongoose.connection.close()
})
describe('GET /random', () => {
it('Request completed successfully', async () => {
const response = await request(app).get('/random')
expect(response.status).toBe(200)
})
it('Responds with a JSON object containing a single quote when tags param is "love|life".', async () => {
const response = await request(app).get('/random?tags=love|life')
expect(response.type).toBe('application/json')
expect(response.body).toEqual({
_id: expect.any(String),
author: expect.any(String),
content: expect.any(String),
length: expect.any(Number),
tags: expect.any(Array),
})
expect(
response.body.tags.find(tag => tag === 'love' || tag === 'life')
).not.toBeUndefined()
})
it('Responds with a different quote on each request', async () => {
// This just fetches two quotes and checks that they are different.
const responses = await Promise.all(
range(2).map(() => request(app).get('/random'))
)
const [quoteA, quoteB] = responses.map(response => response.body)
expect(quoteA.content).not.toEqual(quoteB.content)
})
})
describe('GET /quotes', () => {
it('without params should respond successfully', async () => {
const response = await request(app).get('/quotes')
expect(response.status).toBe(200)
expect(response.type).toBe('application/json')
expect(response.body).toEqual({
count: expect.any(Number),
totalCount: expect.any(Number),
lastItemIndex: expect.any(Number),
results: expect.any(Array),
})
expect(response.body.results[0]).toEqual({
_id: expect.any(String),
author: expect.any(String),
content: expect.any(String),
tags: expect.any(Array),
length: expect.any(Number),
})
})
it('with params of "limit=2&skip=1&tags=life,love" should respond successfully', async () => {
const response = await request(app).get(
'/quotes?limit=2&skip=1&tags=life,love'
)
const { status, type, body } = response
expect(status).toBe(200)
expect(type).toBe('application/json')
expect(body).toEqual({
count: expect.any(Number),
totalCount: expect.any(Number),
lastItemIndex: expect.any(Number),
results: expect.any(Array),
})
expect(body.results[0]).toEqual({
_id: expect.any(String),
author: expect.any(String),
content: expect.any(String),
tags: expect.any(Array),
length: expect.any(Number),
})
expect(body.results[0].tags).toContain('love')
expect(body.results[0].tags).toContain('life')
})
})
describe('GET /authors', () => {
it('Request completed successfully', async () => {
const response = await request(app).get('/authors')
expect(response.status).toBe(200)
expect(response.type).toBe('application/json')
})
})
describe('GET /quotes/:id', () => {
it('Request completed successfully', async () => {
const quote = await Quotes.findOne()
const response = await request(app).get(`/quotes/${quote._id}`)
expect(response.status).toBe(200)
expect(response.type).toBe('application/json')
expect(response.body).toEqual({
_id: quote._id,
author: quote.author,
content: quote.content,
tags: expect.any(Array),
length: expect.any(Number)
})
})
it('Responds with error', async () => {
const response = await request(app).get('/quotes/fakeId')
expect(response.status).toBe(404)
expect(response.type).toBe('application/json')
})
})
describe('GET /tags', () => {
it('Request completed successfully', async () => {
const response = await request(app).get('/tags')
expect(response.status).toBe(200)
expect(response.type).toBe('application/json')
})
})