-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.test.ts
60 lines (51 loc) · 1.75 KB
/
api.test.ts
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
import mongoose from "mongoose";
import request from "supertest";
import { handler } from "../index";
import { MongoMemoryServer } from "mongodb-memory-server";
// Mock environment variables
process.env.MONGO_URI = "mongodb://localhost:27017/testdb";
let mongoServer: MongoMemoryServer;
// Start in-memory MongoDB server
beforeAll(async () => {
mongoServer = await MongoMemoryServer.create();
process.env.MONGO_URI = mongoServer.getUri();
await mongoose.connect(process.env.MONGO_URI);
});
// Close MongoDB connection after tests
afterAll(async () => {
await mongoose.connection.close();
await mongoServer.stop();
});
describe("API Tests", () => {
test("GET / should return welcome message", async () => {
const event = { path: "/", httpMethod: "GET" };
const response = await handler(event);
expect(response.statusCode).toBe(200);
expect(JSON.parse(response.body)).toHaveProperty("message");
});
test("GET /fetchHosts should return an empty array initially", async () => {
const event = { path: "/fetchHosts", httpMethod: "GET" };
const response = await handler(event);
expect(response.statusCode).toBe(200);
expect(JSON.parse(response.body)).toEqual([]);
});
test("POST /insert_review should add a new review", async () => {
const reviewData = {
name: "John Doe",
host: 1,
review: "Great place!",
visit_date: "2025-03-06",
room: "Deluxe",
amenities: "WiFi, Pool",
};
const event = {
path: "/insert_review",
httpMethod: "POST",
body: JSON.stringify(reviewData),
};
const response = await handler(event);
expect(response.statusCode).toBe(201);
const savedReview = JSON.parse(response.body);
expect(savedReview.name).toBe("John Doe");
});
});