-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.spec.js
102 lines (85 loc) · 2.66 KB
/
index.spec.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
/* eslint-disable no-unused-expressions */
const chai = require("chai");
const supertest = require("supertest");
const getServer = require("../../test");
const { connect: connectDB, squel } = require("../../../db");
const { expect } = chai;
describe("/api/v1/users", () => {
let server;
let skills;
let user;
let newUserId;
before(async () => {
server = await getServer();
const { body } = await supertest(server)
.get("/api/v1/skills")
.set("Accept", "application/json");
skills = body;
user = {
firstName: "John",
lastName: "Doe",
email: "[email protected]",
skills: [{ skill: skills[1] }],
};
await connectDB((dbClient) =>
dbClient.query(squel.remove().from('"Users"').toParam())
);
});
it("should create user", async () => {
const { body: newUser } = await supertest(server)
.post("/api/v1/users")
.send(user)
.set("Accept", "application/json")
.expect(201);
expect(newUser)
.to.be.an("object")
.with.keys("id", "firstName", "lastName", "email", "skills");
newUserId = newUser.id;
});
it("should get users", async () => {
const { body: users } = await supertest(server)
.get("/api/v1/users")
.set("Accept", "application/json")
.expect(200);
expect(users).to.be.an("array").that.is.not.empty;
users.forEach((u) =>
expect(u)
.to.be.an("object")
.with.keys("id", "firstName", "lastName", "email", "skills")
);
});
it("should get user", async () => {
const { body: getUser } = await supertest(server)
.get(`/api/v1/users/${newUserId}`)
.set("Accept", "application/json")
.expect(200);
expect(getUser)
.to.be.an("object")
.with.keys("id", "firstName", "lastName", "email", "skills");
expect(getUser).to.eql({ ...user, id: newUserId });
});
it("should patch user", async () => {
const newFirstName = "new first name";
const newSkills = [{ skill: skills[2] }];
const { body: patchedUser } = await supertest(server)
.patch(`/api/v1/users/${newUserId}`)
.send({
firstName: newFirstName,
skills: newSkills,
})
.set("Accept", "application/json")
.expect(200);
expect(patchedUser)
.to.be.an("object")
.with.keys("id", "firstName", "lastName", "email", "skills");
expect(patchedUser.firstName).to.eql(newFirstName);
expect(patchedUser.skills).to.eql(newSkills);
});
it("should remove user", async () => {
const { body } = await supertest(server)
.del(`/api/v1/users/${newUserId}`)
.set("Accept", "application/json")
.expect(204);
expect(body).to.be.empty;
});
});