Skip to content

Commit 542a835

Browse files
committed
feat: add test
1 parent 61b2199 commit 542a835

File tree

7 files changed

+162
-8
lines changed

7 files changed

+162
-8
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,6 @@ dist
174174
# Finder (MacOS) folder config
175175
.DS_Store
176176
.vercel
177+
178+
# tap test
179+
.tap

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
- 把 data/movies.js 中的 series 条目放到 data/series.js 中
66
-https://neodb.social/users/tianheg/ 中标记的内容放到 api 中
77
- add https://github.com/SkeLLLa/fastify-metrics
8-
- add test
98
- read https://www.mock-server.com/
109

1110
## DONE
@@ -14,6 +13,7 @@
1413
- add page, limit, search ability
1514
- added security measures
1615
- refactor code structure([src](https://github.com/tianheg/api/tree/2b12cb2e3c382428a2af11761c52b9baa478a8c2))
16+
- add test
1717

1818
## Problems
1919

bun.lockb

94.6 KB
Binary file not shown.

package.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
"type": "module",
55
"devDependencies": {
66
"@biomejs/biome": "^1.7.1",
7-
"nodemon": "^3.1.0"
7+
"nodemon": "^3.1.0",
8+
"pino-pretty": "^11.1.0",
9+
"tap": "^19.0.2",
10+
"cheerio": "^1.0.0-rc.12",
11+
"pino": "^9.0.0"
812
},
913
"dependencies": {
1014
"@fastify/autoload": "^5.8.0",
@@ -15,13 +19,11 @@
1519
"@fastify/swagger": "^8.14.0",
1620
"@fastify/swagger-ui": "^3.0.0",
1721
"@fastify/under-pressure": "^8.3.0",
18-
"cheerio": "^1.0.0-rc.12",
19-
"fastify": "^4.26.2",
20-
"pino": "^9.0.0",
21-
"pino-pretty": "^11.0.0"
22+
"fastify": "^4.26.2"
2223
},
2324
"scripts": {
2425
"dev": "export NODE_ENV=development && nodemon .",
26+
"test": "tap *.test.js",
2527
"lint": "biome check --apply ."
2628
}
27-
}
29+
}

routes.test.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import tap from "tap";
2+
import Fastify from "fastify";
3+
import registerRoutes from "./routes.js";
4+
5+
tap.test("routes.js", async (t) => {
6+
const app = Fastify();
7+
await registerRoutes(app);
8+
9+
t.test("GET /", async (t) => {
10+
const response = await app.inject({
11+
method: "GET",
12+
url: "/",
13+
});
14+
15+
const baseUrl = "https://api.tianheg.org";
16+
const routes = [
17+
`${baseUrl}/books`,
18+
`${baseUrl}/feeds`,
19+
`${baseUrl}/movies`,
20+
`${baseUrl}/music`,
21+
`${baseUrl}/prompts`,
22+
`${baseUrl}/series`,
23+
`${baseUrl}/words`,
24+
];
25+
26+
t.equal(response.statusCode, 200);
27+
t.match(JSON.parse(response.body), {
28+
repo: "https://github.com/tianheg/api/",
29+
doc: `${baseUrl}/doc`,
30+
tech: "https://fastify.dev/",
31+
deploy: "https://vercel.com/",
32+
routes: routes,
33+
});
34+
});
35+
36+
t.test("GET /books", async (t) => {
37+
const response = await app.inject({
38+
method: "GET",
39+
url: "/books",
40+
});
41+
42+
t.equal(response.statusCode, 200);
43+
t.type(response.body, String);
44+
});
45+
46+
t.test("GET /feeds", async (t) => {
47+
const response = await app.inject({
48+
method: "GET",
49+
url: "/feeds",
50+
});
51+
52+
t.equal(response.statusCode, 200);
53+
t.type(response.body, String);
54+
});
55+
56+
t.test("GET /movies", async (t) => {
57+
const response = await app.inject({
58+
method: "GET",
59+
url: "/movies",
60+
});
61+
62+
t.equal(response.statusCode, 200);
63+
t.type(response.body, String);
64+
});
65+
66+
t.test("GET /music", async (t) => {
67+
const response = await app.inject({
68+
method: "GET",
69+
url: "/music",
70+
});
71+
72+
t.equal(response.statusCode, 200);
73+
t.type(response.body, String);
74+
});
75+
76+
t.test("GET /prompts", async (t) => {
77+
const response = await app.inject({
78+
method: "GET",
79+
url: "/prompts",
80+
});
81+
82+
t.equal(response.statusCode, 200);
83+
t.type(response.body, String);
84+
});
85+
86+
t.test("GET /series", async (t) => {
87+
const response = await app.inject({
88+
method: "GET",
89+
url: "/series",
90+
});
91+
92+
t.equal(response.statusCode, 200);
93+
t.type(response.body, String);
94+
});
95+
96+
t.test("GET /words", async (t) => {
97+
const response = await app.inject({
98+
method: "GET",
99+
url: "/words",
100+
});
101+
102+
t.equal(response.statusCode, 200);
103+
t.type(response.body, String);
104+
});
105+
106+
t.end();
107+
});

utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @param {number} limit - The limit of items per page.
77
* @return {Object} An object containing paginated data, current page, limit, total data count, total pages.
88
*/
9-
function getPaginatedData(data, searchTerm, page, limit) {
9+
export function getPaginatedData(data, searchTerm, page, limit) {
1010
// Filter data if searchTerm is provided
1111
const filteredData = searchTerm
1212
? data.filter((item) =>

utils.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import tap from "tap";
2+
import { getPaginatedData } from "./utils.js";
3+
4+
tap.test("getPaginatedData", async (t) => {
5+
const data = [
6+
{ id: 1, name: "John" },
7+
{ id: 2, name: "Jane" },
8+
{ id: 3, name: "Bob" },
9+
];
10+
11+
t.test("should return paginated data with default values", async (t) => {
12+
const result = getPaginatedData(data, "", 1, 3);
13+
14+
t.same(result, {
15+
page: 1,
16+
limit: 3,
17+
total: 3,
18+
totalPages: 1,
19+
data: [
20+
{ id: 1, name: "John" },
21+
{ id: 2, name: "Jane" },
22+
{ id: 3, name: "Bob" },
23+
]
24+
});
25+
});
26+
27+
t.test("should return paginated data with custom values", async (t) => {
28+
const result = getPaginatedData(data, "j", 2, 1);
29+
30+
t.same(result, {
31+
page: 2,
32+
limit: 1,
33+
total: 2,
34+
totalPages: 2,
35+
data: [
36+
{ id: 2, name: "Jane" },
37+
],
38+
});
39+
});
40+
41+
t.end();
42+
});

0 commit comments

Comments
 (0)