-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.test.js
124 lines (107 loc) · 3.12 KB
/
server.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
const createServer = require("./server");
const Post = require("./models/Post");
const mongoose = require("mongoose");
const supertest = require("supertest");
beforeEach((done) => {
mongoose.connect(
"mongodb://localhost:27017/acmedb",
{ useNewUrlParser: true, useUnifiedTopology: true },
() => done()
);
});
afterEach((done) => {
mongoose.connection.db.dropDatabase(() => {
mongoose.connection.close(() => done());
});
});
const app = createServer();
test("GET /api/posts", async () => {
const post = await Post.create({
title: "Post 1",
content: "Lorem ipsum",
});
await supertest(app)
.get("/api/posts")
.expect(200)
.then((response) => {
// Check the response type and length
expect(Array.isArray(response.body)).toBeTruthy();
expect(response.body.length).toEqual(1);
// Check the response data
expect(response.body[0]._id).toBe(post.id);
expect(response.body[0].title).toBe(post.title);
expect(response.body[0].content).toBe(post.content);
});
});
test("POST /api/posts", async () => {
const data = {
title: "Post 1",
content: "Lorem ipsum",
};
await supertest(app)
.post("/api/posts")
.send(data)
.expect(200)
.then(async (response) => {
// Check the response
expect(response.body._id).toBeTruthy();
expect(response.body.title).toBe(data.title);
expect(response.body.content).toBe(data.content);
// Check the data in the database
const post = await Post.findOne({ _id: response.body._id });
expect(post).toBeTruthy();
expect(post.title).toBe(data.title);
expect(post.content).toBe(data.content);
});
});
test("GET /api/posts/:id", async () => {
const post = await Post.create({
title: "Post 1",
content: "Lorem ipsum",
});
await supertest(app)
.get("/api/posts/" + post.id)
.expect(200)
.then((response) => {
expect(response.body._id).toBe(post.id);
expect(response.body.title).toBe(post.title);
expect(response.body.content).toBe(post.content);
});
});
test("PATCH /api/posts/:id", async () => {
const post = await Post.create({
title: "Post 1",
content: "Lorem ipsum",
});
const data = {
title: "New title",
content: "dolor sit amet",
};
await supertest(app)
.patch("/api/posts/" + post.id)
.send(data)
.expect(200)
.then(async (response) => {
// Check the response
expect(response.body._id).toBe(post.id);
expect(response.body.title).toBe(data.title);
expect(response.body.content).toBe(data.content);
// Check the data in the database
const newPost = await Post.findOne({ _id: response.body._id });
expect(newPost).toBeTruthy();
expect(newPost.title).toBe(data.title);
expect(newPost.content).toBe(data.content);
});
});
test("DELETE /api/posts/:id", async () => {
const post = await Post.create({
title: "Post 1",
content: "Lorem ipsum",
});
await supertest(app)
.delete("/api/posts/" + post.id)
.expect(204)
.then(async () => {
expect(await Post.findOne({ _id: post.id })).toBeFalsy();
});
});