-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.test.js
More file actions
159 lines (153 loc) · 4.96 KB
/
server.test.js
File metadata and controls
159 lines (153 loc) · 4.96 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const request = require('supertest')
const app = require('./app')
const {promisify} = require("util");
const fs = require("fs");
const writeFile = promisify(fs.writeFile)
const filePath = './public/data.json'
async function resetFile() {
const data = {
"recipes": [
{
"name": "scrambledEggs",
"ingredients": [
"1 tsp oil",
"2 eggs",
"salt"
],
"instructions": [
"Beat eggs with salt",
"Heat oil in pan",
"Add eggs to pan when hot",
"Gather eggs into curds, remove when cooked",
"Salt to taste and enjoy"
]
},
{
"name": "garlicPasta",
"ingredients": [
"500mL water",
"100g spaghetti",
"25mL olive oil",
"4 cloves garlic",
"Salt"
],
"instructions": [
"Heat garlic in olive oil",
"Boil water in pot",
"Add pasta to boiling water",
"Remove pasta from water and mix with garlic olive oil",
"Salt to taste and enjoy"
]
},
{
"name": "chai",
"ingredients": [
"400mL water",
"100mL milk",
"5g chai masala",
"2 tea bags or 20 g loose tea leaves"
],
"instructions": [
"Heat water until 80 C",
"Add milk, heat until 80 C",
"Add tea leaves/tea bags, chai masala; mix and steep for 3-4 minutes",
"Remove mixture from heat; strain and enjoy"
]
}
]
}
await writeFile(filePath, JSON.stringify(data))
}
describe("GET /recipes", () => {
test("Should respond with list of recipe names", async () => {
await resetFile()
const response = await request(app).get('/recipes')
expect(response.body).toEqual({"recipeNames": ["scrambledEggs", "garlicPasta", "chai"]})
expect(response.statusCode).toBe(200)
})
test("Should respond with details and number of steps", async () => {
await resetFile()
const response = await request(app).get('/recipes/details/garlicPasta')
expect(response.body).toEqual({
"details": {
"ingredients": [
"500mL water",
"100g spaghetti",
"25mL olive oil",
"4 cloves garlic",
"Salt"
],
"numSteps": 5
}
})
expect(response.statusCode).toBe(200)
})
})
describe('POST /recipes', () => {
test("Should respond with 201", async() => {
await resetFile()
const response = await request(app).post('/recipes').send({
"name": "butteredBagel",
"ingredients": [
"1 bagel",
"butter"
],
"instructions": [
"cut the bagel",
"spread butter on bagel"
]
})
expect(response.body).toEqual({})
expect(response.statusCode).toBe(201)
})
test("Should respond with 201", async() => {
const response = await request(app).post('/recipes').send({
"name": "butteredBagel",
"ingredients": [
"1 bagel",
"butter"
],
"instructions": [
"cut the bagel",
"spread butter on bagel"
]
})
expect(response.body).toEqual({
"error": "Recipe already exists"
})
expect(response.statusCode).toBe(400)
})
})
describe("PUT /recipes", () => {
test("Should respond with 204", async() => {
const response = await request(app).put('/recipes').send({
"name": "butteredBagel",
"ingredients": [
"1 bagel",
"2 tbsp butter"
],
"instructions": [
"cut the bagel",
"spread butter on bagel"
]
})
expect(response.body).toEqual({})
expect(response.statusCode).toBe(204)
})
test("Should be 404", async() => {
await resetFile()
const response = await request(app).put('/recipes').send({
"name": "butteredBagel",
"ingredients": [
"1 bagel",
"2 tbsp butter"
],
"instructions": [
"cut the bagel",
"spread butter on bagel"
]
})
expect(response.body).toEqual({error: "Recipe does not exist"})
expect(response.statusCode).toBe(404)
})
})