Skip to content

Commit 37bc651

Browse files
Add tests
1 parent 2d61041 commit 37bc651

File tree

2 files changed

+501
-0
lines changed

2 files changed

+501
-0
lines changed

src/__test__/items.e2e.spec.ts

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
import request from "supertest";
2+
import dotenv from "dotenv";
3+
import { describe, it, expect } from "@jest/globals";
4+
import { StatusCodes } from "http-status-codes";
5+
6+
dotenv.config({ path: ".local.env" });
7+
8+
const BASE_ENDPOINT = `http://127.0.0.1:4566/restapis/${process.env.LOCAL_API_ID}/test/_user_request_`;
9+
10+
describe("Get all items - [GET /items]", () => {
11+
it("Should return all items", async () => {
12+
return request(BASE_ENDPOINT)
13+
.get("/items")
14+
.expect(StatusCodes.OK)
15+
.then(({ body }) => {
16+
expect(body).toEqual(
17+
expect.arrayContaining([
18+
{
19+
name: expect.any(String),
20+
description: expect.any(String),
21+
createdAt: expect.any(String),
22+
id: expect.any(String),
23+
},
24+
{
25+
name: expect.any(String),
26+
description: expect.any(String),
27+
createdAt: expect.any(String),
28+
id: expect.any(String),
29+
},
30+
])
31+
);
32+
});
33+
});
34+
});
35+
36+
describe("Get item by ID - [GET /items/{itemId}]", () => {
37+
it("Should return an item", async () => {
38+
return request(BASE_ENDPOINT)
39+
.get("/items/1")
40+
.expect(StatusCodes.OK)
41+
.then(({ body }) => {
42+
expect(body).toEqual(
43+
expect.objectContaining({
44+
name: expect.any(String),
45+
description: expect.any(String),
46+
createdAt: expect.any(String),
47+
id: "1",
48+
})
49+
);
50+
});
51+
});
52+
53+
it("Should return 404 if item not found", async () => {
54+
return request(BASE_ENDPOINT)
55+
.get("/items/100")
56+
.expect(StatusCodes.NOT_FOUND)
57+
.then(({ body }) =>
58+
expect(body).toEqual({
59+
message: "Item not found.",
60+
})
61+
);
62+
});
63+
});
64+
65+
describe("Create item - [POST /items]", () => {
66+
it("Should create an item", async () => {
67+
const PostDTO = {
68+
name: "test-post-name",
69+
description: "test-post-description",
70+
};
71+
72+
return request(BASE_ENDPOINT)
73+
.post("/items")
74+
.send(PostDTO)
75+
.expect(StatusCodes.CREATED)
76+
.then(({ body }) => {
77+
expect(body).toEqual(
78+
expect.objectContaining({
79+
name: PostDTO.name,
80+
description: PostDTO.description,
81+
createdAt: expect.any(String),
82+
id: expect.any(String),
83+
})
84+
);
85+
});
86+
});
87+
88+
it("Should create an item without description", async () => {
89+
const PostDTO = {
90+
name: "test-post-name",
91+
};
92+
93+
return request(BASE_ENDPOINT)
94+
.post("/items")
95+
.send(PostDTO)
96+
.expect(StatusCodes.CREATED)
97+
.then(({ body }) => {
98+
expect(body).toEqual(
99+
expect.objectContaining({
100+
name: PostDTO.name,
101+
createdAt: expect.any(String),
102+
id: expect.any(String),
103+
})
104+
);
105+
});
106+
});
107+
108+
it("Should return error if invalid data provided", async () => {
109+
const PostDTO = {
110+
tier: "premium",
111+
};
112+
113+
return request(BASE_ENDPOINT)
114+
.post("/items")
115+
.send(PostDTO)
116+
.expect(StatusCodes.BAD_REQUEST)
117+
.then(({ body }) => {
118+
expect(body).toEqual({
119+
message: "Invalid data provided.",
120+
});
121+
});
122+
});
123+
});
124+
125+
describe("Update item - [PUT /items/{itemId}]", () => {
126+
it("Should update an item", async () => {
127+
const PutDTO = {
128+
name: "test-put-name",
129+
description: "test-put-description",
130+
};
131+
132+
return request(BASE_ENDPOINT)
133+
.put("/items/1")
134+
.send(PutDTO)
135+
.expect(StatusCodes.OK)
136+
.then(({ body }) => {
137+
expect(body).toEqual(
138+
expect.objectContaining({
139+
name: PutDTO.name,
140+
description: PutDTO.description,
141+
createdAt: expect.any(String),
142+
id: "1",
143+
})
144+
);
145+
});
146+
});
147+
148+
it("Should return error if invalid data provided", async () => {
149+
const PutDTO = {
150+
tier: "premium",
151+
};
152+
153+
return request(BASE_ENDPOINT)
154+
.put("/items/1")
155+
.send(PutDTO)
156+
.expect(StatusCodes.BAD_REQUEST)
157+
.then(({ body }) => {
158+
expect(body).toEqual({
159+
message: "Invalid data provided.",
160+
});
161+
});
162+
});
163+
164+
it("Should return 404 if item not found", async () => {
165+
const PutDTO = {
166+
name: "test-put-name",
167+
description: "test-put-description",
168+
};
169+
170+
return request(BASE_ENDPOINT)
171+
.put("/items/100")
172+
.send(PutDTO)
173+
.expect(StatusCodes.NOT_FOUND)
174+
.then(({ body }) =>
175+
expect(body).toEqual({
176+
message: "Item not found.",
177+
})
178+
);
179+
});
180+
});
181+
182+
describe("Delete item - [DELETE /items/{itemId}]", () => {
183+
it("Should delete an item", async () => {
184+
return request(BASE_ENDPOINT)
185+
.delete("/items/1")
186+
.expect(StatusCodes.OK)
187+
.then(({ body }) => {
188+
expect(body).toEqual(
189+
expect.objectContaining({
190+
name: expect.any(String),
191+
description: expect.any(String),
192+
createdAt: expect.any(String),
193+
id: "1",
194+
})
195+
);
196+
});
197+
});
198+
199+
it("Should return 404 if item not found", async () => {
200+
return request(BASE_ENDPOINT)
201+
.delete("/items/100")
202+
.expect(StatusCodes.NOT_FOUND)
203+
.then(({ body }) =>
204+
expect(body).toEqual({
205+
message: "Item not found.",
206+
})
207+
);
208+
});
209+
});

0 commit comments

Comments
 (0)