forked from HackYourFuture/Node.js
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapp.test.js
More file actions
46 lines (36 loc) · 1.23 KB
/
app.test.js
File metadata and controls
46 lines (36 loc) · 1.23 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
const request = require("supertest");
jest.mock("node-fetch", () => jest.fn());
const fetch = require("node-fetch");
const app = require("../app.js");
describe("POST /weather", () => {
it("should return weather text for a valid city", async () => {
fetch.mockResolvedValue({
json: async () => ({
main: { temp: 280 },
cod: 200,
}),
});
const response = await request(app)
.post("/weather")
.send({ cityName: "London" });
expect(response.statusCode).toBe(200);
expect(response.body.weatherText).toContain("The weather in London");
});
it("should return an error if city name is missing", async () => {
const response = await request(app).post("/weather").send({});
expect(response.statusCode).toBe(400);
expect(response.body.weatherText).toBe("City name is required!");
});
it("should return city not found for invalid city", async () => {
// نرجّع كود 404 من الـ API
fetch.mockResolvedValue({
json: async () => ({
cod: "404",
}),
});
const response = await request(app)
.post("/weather")
.send({ cityName: "asdkjashdkjashd" });
expect(response.body.weatherText).toBe("City is not found!");
});
});