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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
const productController = require("../../controller/product");
const productModel = require('../../models/Product')
const httpMocks = require('node-mocks-http')
const newProduct = require('../data/new-product.json')
const {Promise} = require("mongoose");
//๋จ์ํ
์คํธ์ด๊ธฐ ๋๋ฌธ์ ์์กด์ฑ์ด ์๋ ๊ฒ์ mock function์ผ๋ก ๋ง๋ค์ด์ค๋ค.
//์ด ํจ์๊ฐ ๋ญ ์ฌ์ฉํ๋์ง ์ด๋ค๊ฒ์ ๋ฆฌํดํ๋์ง ์ถ์ ์ด ๊ฐ๋ฅํด์ง๋ค.
productModel.create = jest.fn();
productModel.find = jest.fn();
productModel.findById = jest.fn();
productModel.findByIdAndUpdate = jest.fn();
//์ ์ญ์ผ๋ก ์ฐ์ผ ๋ณ์์ด๊ธฐ ๋๋ฌธ์ ๋ค์๊ณผ ๊ฐ์ด ์ ์ธํด์ค๋ค.
let req, res, next;
const productId = '618761fd1febbe2fe466a211';
//๊ฐ test๋ง๋ค ๊ณตํต์ ์ผ๋ก ์ฐ์ด๋ ๋ถ๋ถ์ด๊ธฐ์ beforeEach๋ฅผ ์ฌ์ฉํ์ฌ ์ฒ๋ฆฌํ๋ค.
beforeEach(() => {
req = httpMocks.createRequest();
res = httpMocks.createResponse();
next = jest.fn();
})
describe("Product Controller Create", () => {
beforeEach(() => {
req.body = newProduct;
})
it('should have a createProduct function', () => {
expect(typeof productController.createProduct).toBe("function");
});
it('should call ProductModel.create', async () => {
req.body = newProduct;
await productController.createProduct(req, res, next);
expect(productModel.create).toBeCalledWith(newProduct);
})
it('should return 201 response code', async () => {
await productController.createProduct(req, res, next);
expect(res.statusCode).toBe(201);
//๊ฐ์ด ๋ฌด์์ธ์ง ์ ๊ฒฝ ์ฐ์ง ์๊ณ Boolean ์ปจํ
์คํธ์์ ๊ฐ์ด ์ฐธ์ธ์ง ํ์ธ
expect(res._isEndCalled()).toBeTruthy();
})
it('should return json body in response', async () => {
productModel.create.mockReturnValue(newProduct)
await productController.createProduct(req, res, next);
expect(res._getJSONData()).toStrictEqual(newProduct)
})
it('should handle errors', async () => {
//์๋ฌ๋ฉ์์ง ์ ์ธ
const errorMessage = {message : 'description property missing'};
//Promise.reject(reason) ๋ฉ์๋๋ ์ฃผ์ด์ง ์ด์ (reason)๋ก ๊ฑฐ๋ถ๋ Promise ๊ฐ์ฒด๋ฅผ ๋ฐํ
//๋ฐ๋ผ์ ์๋์ ์ฝ๋๋ errorMessage๋ฅผ ์ด์ ๋ก reject๋ ์ํฉ์ ๋ง๋ค๊ธฐ์ํจ์ด๋ค.
const rejectedPromise = Promise.reject(errorMessage);
//์ ์ํฉ์ผ๋ก returnValue๋ฅผ ๋ง๋ค์ด์ค๋ค.
productModel.create.mockReturnValue(rejectedPromise);
//์๋ ๋ฉ์๋๋ฅผ ์คํํ๋ฉด create์์ ์์ ์๋ฌ๊ฐ ๋ฐ์ํ ๊ฒ์ด๋ค.
await productController.createProduct(req,res,next);
//ํด๋น ์๋ฌ๋ฉ์์ง๊ฐ ๋ฐ์ํ์๋์ง ํ์ธํ๋ค.
expect(next).toBeCalledWith(errorMessage);
});
});
describe("Product Controller Get", () => {
it('should have a getProducts function', function () {
expect(typeof productController.getProducts).toBe("function");
});
it('should call ProductModel.find({})', async () => {
await productController.getProducts(req,res,next);
expect(productModel.find).toHaveBeenCalledWith({})
});
it('should return 200 response',async function () {
await productController.getProducts(req,res,next);
expect(res.statusCode).toBe(200);
//๊ฐ์ด ๋ฌด์์ธ์ง ์ ๊ฒฝ ์ฐ์ง ์๊ณ Boolean ์ปจํ
์คํธ์์ ๊ฐ์ด ์ฐธ์ธ์ง ํ์ธ
expect(res._isEndCalled).toBeTruthy()
});
// it('should return json boyd in response',async function () {
// productModel.find.mockReturnValue()
// });
it('should handle errors', async function () {
const errorMessage = {message : "Error finding product data"};
const rejectedPromise = Promise.reject(errorMessage);
productModel.find.mockReturnValue(rejectedPromise);
await productController.getProducts(req,res,next);
expect(next).toBeCalledWith(errorMessage);
});
})
describe('Product Controller GetById', () => {
it('should have a getProductById', function () {
expect(typeof productController.getProductById).toBe("function");
});
it('should call productModel findById', async function () {
req.params.productId = productId;
await productController.getProductById(req,res,next);
expect(productModel.findById).toBeCalledWith(productId)
});
it('should return json body and response code 200', async function () {
productModel.findById.mockReturnValue(newProduct);
await productController.getProductById(req,res,next);
expect(res.statusCode).toBe(200)
expect(res._getJSONData()).toStrictEqual(newProduct);
expect(res._isEndCalled()).toBeTruthy();
});
it('should return 404 when item doesnt exist', async function () {
productModel.findById.mockReturnValue(null);
await productController.getProductById(req,res,next);
expect(res.statusCode).toBe(404);
expect(res._isEndCalled()).toBeTruthy();
});
it('should handle errors', async function () {
const errorMessage = {message : 'error'};
const rejectPromise = Promise.reject(errorMessage);
productModel.findById.mockReturnValue(rejectPromise);
await productController.getProductById(req,res,next);
expect(next).toHaveBeenCalledWith(errorMessage);
});
})
describe('Product Controller Update',() => {
it('should have an updateProduct function', function () {
expect(typeof productController.updateProduct).toBe("function");
});
it('should call productModel findByIdAndUpadte', async () => {
//๊ฒ์ํ ์์ด๋๋ฅผ ์ ์ธํ๋ค.
req.params.productId = productId;
//์
๋ฐ์ดํธํ ๊ฐ์ฒด์ option์ ์ ์ธํ๋ค.
//option์ ๋ฆฌํดํ๋ ๊ฐ์ฒด๊ฐ ์
๋ฐ์ดํธ ๋๊ธฐ์ ๊ฐ์ฒด์ธ์ง ์๋ก์ด ๊ฐ์ฒด์ธ์ง๋ฅผ ๊ฒฐ์ ํ๋ ๊ฒ์ด๋ค. true๋ฅผ ์
๋ ฅํ์์ผ๋ฏ๋ก ์
๋ฐ์ดํธ ๋ ๊ฐ์ฒด๋ฅผ ๋ฆฌํดํ๋ค.
req.body = {name:"updated name"},{new : true};
await productController.updateProduct(req,res,next);
expect(productModel.findByIdAndUpdate).toHaveBeenCalledWith(productId,{name:"updated name"},{new : true})
})
it('should return json body and response code 200', async function () {
req.params.productId = productId;
req.body = {name:"updated name",description: "updated description"};
productModel.findByIdAndUpdate.mockReturnValue({name:"updated name",description: "updated description"})
await productController.updateProduct(req,res,next);
expect(res._isEndCalled()).toBeTruthy();
expect(res.statusCode).toBe(200);
expect(res._getJSONData()).toStrictEqual({name:"updated name",description: "updated description"});
});
it('should handle 404 when item doesnt exist',async function () {
productModel.findByIdAndUpdate.mockReturnValue(null);
await productController.updateProduct(req,res,next);
expect(res.statusCode).toBe(404);
expect(res._isEndCalled()).toBeTruthy();
});
it('should handle errors',async function () {
const errorMessage = {"message" : "error"};
const rejectPromise = Promise.reject(errorMessage);
productModel.findByIdAndUpdate.mockReturnValue(rejectPromise);
await productController.updateProduct(req,res,next);
expect(next).toHaveBeenCalledWith(errorMessage);
});
})
describe('Product Controller Delete', () => {
it('should have a delete product funciton', function () {
expect(typeof productController.deleteProduct).toBe("function");
});
it('should call productmodel.findByIdAndDelete',async function () {
req.params.productId = productId;
await productController.deleteProduct(req);
expect(productModel.findByIdAndDelete).toBeCalledWith(productId);
});
it('should return 200 response', async function () {
let deletedProduct = {
name : "deleteProduct",
description: "test"
}
productModel.findByIdAndDelete.mockReturnValue(deletedProduct);
await productController.deleteProduct(req,res,next);
expect(res.statusCode).toBe(200);
expect(res._getJSONData()).toStrictEqual(deletedProduct);
expect(res._isEndCalled()).toBeTruthy();
});
it('should handle 404 when item doesnt exist', async function () {
productModel.findByIdAndDelete.mockReturnValue(null);
await productController.deleteProduct(req,res,next);
expect(res.statusCode).toBe(404);
expect(res._isEndCalled()).toBeTruthy();
});
it('should handle errors', async function () {
const errorMessage = {message : "Error deleting"}
const rejectPromise = Promise.reject(errorMessage);
productModel.findByIdAndDelete.mockReturnValue(rejectPromise);
await productController.deleteProduct(req,res,next);
expect(next).toHaveBeenCalledWith(errorMessage);
});
})
|