-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.ts
97 lines (87 loc) · 2.67 KB
/
index.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
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
import axios, { AxiosError } from "axios";
import fetchAdapter from "./index";
import { test, expect, mock, describe } from "bun:test";
//lets mock the fetch function #mocking the dependency
const mockFetch = mock(fetch);
// test for different scenarios:
describe("fetchAdapter", () => {
// create the adapter
const adapter = fetchAdapter(mockFetch);
// create the axios instance
const client = axios.create({
baseURL: "https://dummyjson.com",
adapter,
withCredentials: true,
});
// test for successful request
test("successful request", async () => {
//make the request
const res = await client.get("/users/1");
//expect status success and header to be application/json
expect(res.status).toBeGreaterThanOrEqual(200);
expect(res.status).toBeLessThan(300);
expect(res.headers["content-type"]).toContain("application/json");
});
// test for failed request
test("failed request", async () => {
//make the request
try {
await client.get("/potato");
} catch (err) {
if (err instanceof AxiosError) {
//expect status to be 404
expect(err.response).toBeDefined();
expect(err.response?.status).toBe(404);
expect(err.response?.statusText).toBe("Not Found");
}
}
});
//test post request
test("post request", async () => {
//make the request
const res = await client.post("/users/add", {
firstName: "John",
lastName: "Doet",
age: 25,
});
expect(res.status).toBeGreaterThanOrEqual(200);
expect(res.status).toBeLessThan(300);
expect(res.headers["content-type"]).toContain("application/json");
});
//test to check query params
test("query params", async () => {
//make the request
const res = await client.get("/users/search", {
params: {
q: "John",
},
});
expect(res.status).toBeGreaterThanOrEqual(200);
expect(res.status).toBeLessThan(300);
expect(res.headers["content-type"]).toContain("application/json");
});
//test to check set-cookie header
test("set-cookie header", async () => {
//make the request
try {
const res = await client.post(
"/user/emp/login",
{
email: "[email protected]",
password: "Rahat123#",
shopId: "helloShop",
},
{
withCredentials: true,
baseURL: "http://localhost:4000",
}
);
expect(res.status).toBeGreaterThanOrEqual(200);
expect(res.status).toBeLessThan(300);
expect(res.headers["content-type"]).toContain("application/json");
expect(res.headers["set-cookie"]).toBeDefined();
} catch (err) {
console.log("I am in error!");
}
});
});