-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.spec.ts
More file actions
177 lines (142 loc) · 4.42 KB
/
testing.spec.ts
File metadata and controls
177 lines (142 loc) · 4.42 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { describe, it, expect, beforeEach, afterEach, mock } from "bun:test";
import { Jetflare, createRoutes } from "./dist/index.js";
describe("Jetflare SDK", () => {
const mockOrigin = "http://api.example.com";
const mockRoutes = createRoutes({
getUsers: {
path: "/users",
method: "get",
query: { page: Number, limit: Number },
},
createUser: {
path: "/users",
method: "post",
body: { name: String, email: String },
},
getUserById: {
path: "/users/:id",
method: "get",
params: { id: String },
},
wsConnect: {
path: "/ws",
method: "websocket",
},
sseEvents: {
path: "/events",
method: "sse",
},
});
let api: ReturnType<typeof Jetflare>;
beforeEach(() => {
api = Jetflare(mockOrigin, mockRoutes);
});
describe("HTTP Requests", () => {
it("should make GET request with query parameters", async () => {
const response = await api.getUsers({
query: { page: 1, limit: 10 },
});
expect(response.ok).toBe(true);
expect(response.status).toBe(200);
const data = await response.json();
expect(Array.isArray(data)).toBe(true);
});
it("should make POST request with body", async () => {
const userData = {
name: "John Doe",
email: "john@example.com",
};
const response = await api.createUser({
body: userData,
});
expect(response.ok).toBe(true);
expect(response.status).toBe(201);
});
it("should handle path parameters correctly", async () => {
const response = await api.getUserById({
params: { id: "123" },
});
expect(response.ok).toBe(true);
expect(response.url).toInclude("/users/123");
});
});
describe("Caching", () => {
it("should cache GET requests when configured", async () => {
const firstResponse = await api.getUsers({
query: { page: 1, limit: 10 },
cache: { ttl: 5000 },
});
const secondResponse = await api.getUsers({
query: { page: 1, limit: 10 },
cache: { ttl: 5000 },
});
expect(secondResponse.fromCache).toBe(true);
expect(await firstResponse.json()).toEqual(await secondResponse.json());
});
it("should invalidate cache when specified", async () => {
await api.getUsers({
cache: true,
});
await api.createUser({
body: { name: "Test", email: "test@example.com" },
});
const cachedData = api.cache.get("/users");
expect(cachedData).toBe(null);
});
});
describe("WebSocket Connection", () => {
it("should establish WebSocket connection", () => {
const ws = api.wsConnect();
ws.connect();
const socket = ws.socket();
expect(socket).toBeDefined();
expect(socket?.readyState).toBe(WebSocket.CONNECTING);
});
it("should handle WebSocket events", () => {
const ws = api.wsConnect();
const mockHandler = mock((data: any) => {});
ws.connect();
ws.on("message", mockHandler);
ws.send("test message");
expect(mockHandler).toHaveBeenCalled();
});
});
describe("Server-Sent Events", () => {
it("should establish SSE connection", () => {
const sse = api.sseEvents();
sse.connect();
const eventSource = sse.event();
expect(eventSource).toBeDefined();
expect(eventSource?.readyState).toBe(EventSource.CONNECTING);
});
it("should handle SSE events", () => {
const sse = api.sseEvents();
const mockHandler = mock((event: any) => {});
sse.connect();
sse.on("message", mockHandler);
expect(mockHandler).toBeDefined();
});
});
describe("Error Handling", () => {
it("should handle network errors", async () => {
api.setBaseURL("http://invalid-url");
await expect(api.getUsers()).rejects.toThrow();
});
it("should handle timeout errors", async () => {
api.setDefaultTimeout(1);
await expect(api.getUsers()).rejects.toThrow(/timeout/i);
});
});
describe("Configuration", () => {
it("should set default headers", () => {
const headers = { Authorization: "Bearer token" };
api.setDefaultHeaders(headers);
expect(api["defaultHeaders"]).toEqual(headers);
});
it("should update base URL", () => {
const newUrl = "https://new-api.example.com";
api.setBaseURL(newUrl);
expect(api.origin).toBe(newUrl);
});
});
});