-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes.integration.test.js
More file actions
137 lines (114 loc) · 4.27 KB
/
notes.integration.test.js
File metadata and controls
137 lines (114 loc) · 4.27 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
// Defining Constants
const app = require("./notes-controller");
const request = require("supertest");
const { expect } = require("@jest/globals");
// making Id a global variable
let id = null;
// ------------------------------------------------------------
// TEST FOR POST
describe("TEST FOR POST", () => {
it("Should return an error if the **body** is empty", async () => {
const postBody = { notes: "" }; // creating an empty note
const resultPostEmpty = await request(app)
.post("/notes")
.set("Content-Type", "application/json")
.send(postBody);
expect(resultPostEmpty.status).toBe(400);
expect(resultPostEmpty.body.message).toBe(
"The note field is empty so a note was not sent (つ﹏<。)"
);
});
it("Should return an error if the **notes** is empty", async () => {
const postBody = { notes: "" }; // creating an empty note
const resultPostEmpty = await request(app)
.post("/notes")
.set("Content-Type", "application/json")
.send(postBody);
expect(resultPostEmpty.status).toBe(400);
expect(resultPostEmpty.body.message).toBe(
"The note field is empty so a note was not sent (つ﹏<。)"
);
});
it("Should add a new note into the database", async () => {
postBody = {
notes: "this is a test note :)",
};
const resultPost = await request(app)
.post("/notes")
.set("Content-Type", "application/json")
.send(postBody);
expect(resultPost.status).toBe(201);
expect(resultPost.body.message).toBe("You have added a new note ( ´∀`)b");
id = resultPost.body.id;
});
});
// ------------------------------------------------------------
// TEST FOR GET
describe("TEST FOR GET", () => {
it("/notes should return all notes", async () => {
const resultGet = await request(app).get("/notes");
expect(resultGet.status).toBe(200);
});
it("/notes/{id} should return that id's notes", async () => {
const resultGetId = await request(app).get("/notes/" + String(id));
expect(resultGetId.status).toBe(200);
expect(resultGetId.body._id).toBe(id);
});
it("/notes/{invalid id} should return an error", async () => {
const resultGetId = await request(app).get("/notes/" + String(id + 1));
expect(resultGetId.status).toBe(404);
expect(resultGetId.body.message).toBe(
"There is no entry with this id ( >Д< )ゝ"
);
});
});
// ------------------------------------------------------------
// TEST FOR UPDATE
describe("TEST FOR UPDATE ", () => {
it("Should return an error if the **body** is empty", async () => {
const updateEmptyBody = { notes: "" }; // creating an empty note
const resultPatchEmpty = await request(app)
.patch("/notes/" + id)
.set("Content-Type", "application/json")
.send(updateEmptyBody);
expect(resultPatchEmpty.status).toBe(400);
expect(resultPatchEmpty.body.message).toBe(
"The note field is empty the note was not updated (つ﹏<。)"
);
});
it("/notes/{id} with a body, should update the note", async () => {
const updateBody = {
notes: "this is a update",
};
resultUpdate = await request(app)
.patch("/notes/" + id)
.set("Content-Type", "application/json")
.send(updateBody);
expect(resultUpdate.status).toBe(200);
expect(resultUpdate.body.message).toBe(
"this note has been updated successfully ٩(`・ω・´)و"
);
const resultUpdateNewNote = await request(app).get("/notes/" + id);
expect(resultUpdateNewNote.body.notes).toBe("this is a update");
});
});
// ------------------------------------------------------------
// TEST FOR DElETE
describe("TEST FOR DELETE ", () => {
it("/notes/{id} should delete notes based on an id", async () => {
resultDelId = await request(app).delete("/notes/" + id);
expect(resultDelId.status).toBe(200);
expect(resultDelId.body.message).toBe(
"this note has been deleted successfully deleted ٩(`・ω・´)و"
);
});
//
it("/notes should delete all notes", async () => {
resultDelAll = await request(app).delete("/notes");
expect(resultDelAll.status).toBe(200);
expect(resultDelAll.body.message).toBe(
"All notes have been deleted ٩(`・ω・´)و"
);
});
});
// ------------------------------------------------------------