|
1 |
| -import { replaceSuperJsonPayload } from "../src/v3/utils/ioSerialization.js"; |
| 1 | +import { replaceSuperJsonPayload, prettyPrintPacket } from "../src/v3/utils/ioSerialization.js"; |
2 | 2 |
|
3 | 3 | describe("ioSerialization", () => {
|
4 | 4 | describe("replaceSuperJsonPayload", () => {
|
@@ -188,4 +188,160 @@ describe("ioSerialization", () => {
|
188 | 188 | await expect(replaceSuperJsonPayload(originalSerialized, invalidPayload)).rejects.toThrow();
|
189 | 189 | });
|
190 | 190 | });
|
| 191 | + |
| 192 | + describe("prettyPrintPacket", () => { |
| 193 | + it("should return empty string for undefined data", async () => { |
| 194 | + const result = await prettyPrintPacket(undefined); |
| 195 | + expect(result).toBe(""); |
| 196 | + }); |
| 197 | + |
| 198 | + it("should return string data as-is", async () => { |
| 199 | + const result = await prettyPrintPacket("Hello, World!"); |
| 200 | + expect(result).toBe("Hello, World!"); |
| 201 | + }); |
| 202 | + |
| 203 | + it("should pretty print JSON data with default options", async () => { |
| 204 | + const data = { name: "John", age: 30, nested: { value: true } }; |
| 205 | + const result = await prettyPrintPacket(data, "application/json"); |
| 206 | + |
| 207 | + expect(result).toBe(JSON.stringify(data, null, 2)); |
| 208 | + }); |
| 209 | + |
| 210 | + it("should handle JSON data as string", async () => { |
| 211 | + const data = { name: "John", age: 30 }; |
| 212 | + const jsonString = JSON.stringify(data); |
| 213 | + const result = await prettyPrintPacket(jsonString, "application/json"); |
| 214 | + |
| 215 | + expect(result).toBe(JSON.stringify(data, null, 2)); |
| 216 | + }); |
| 217 | + |
| 218 | + it("should pretty print SuperJSON data", async () => { |
| 219 | + const data = { |
| 220 | + name: "John", |
| 221 | + date: new Date("2023-01-01"), |
| 222 | + bigInt: BigInt(123), |
| 223 | + set: new Set(["a", "b"]), |
| 224 | + map: new Map([["key", "value"]]), |
| 225 | + }; |
| 226 | + |
| 227 | + const superjson = await import("superjson"); |
| 228 | + const serialized = superjson.stringify(data); |
| 229 | + |
| 230 | + const result = await prettyPrintPacket(serialized, "application/super+json"); |
| 231 | + |
| 232 | + // Should deserialize and pretty print the data |
| 233 | + expect(result).toContain('"name": "John"'); |
| 234 | + expect(result).toContain('"date": "2023-01-01T00:00:00.000Z"'); |
| 235 | + expect(result).toContain('"bigInt": "123"'); |
| 236 | + expect(result).toContain('"set": [\n "a",\n "b"\n ]'); |
| 237 | + expect(result).toContain('"map": {\n "key": "value"\n }'); |
| 238 | + }); |
| 239 | + |
| 240 | + it("should handle circular references", async () => { |
| 241 | + const data: any = { name: "John" }; |
| 242 | + data.self = data; // Create circular reference |
| 243 | + |
| 244 | + // Create a SuperJSON serialized version to test the circular reference detection |
| 245 | + const superjson = await import("superjson"); |
| 246 | + const serialized = superjson.stringify(data); |
| 247 | + |
| 248 | + const result = await prettyPrintPacket(serialized, "application/super+json"); |
| 249 | + |
| 250 | + expect(result).toContain('"name": "John"'); |
| 251 | + expect(result).toContain('"self": "[Circular]"'); |
| 252 | + }); |
| 253 | + |
| 254 | + it("should handle regular non-circular references", async () => { |
| 255 | + const person = { name: "John" }; |
| 256 | + |
| 257 | + const data: any = { person1: person, person2: person }; |
| 258 | + |
| 259 | + // Create a SuperJSON serialized version to test the circular reference detection |
| 260 | + const superjson = await import("superjson"); |
| 261 | + const serialized = superjson.stringify(data); |
| 262 | + |
| 263 | + const result = await prettyPrintPacket(serialized, "application/super+json"); |
| 264 | + |
| 265 | + expect(result).toContain('"person1": {'); |
| 266 | + expect(result).toContain('"person2": {'); |
| 267 | + }); |
| 268 | + |
| 269 | + it("should filter out specified keys", async () => { |
| 270 | + const data = { name: "John", password: "secret", age: 30 }; |
| 271 | + const result = await prettyPrintPacket(data, "application/json", { |
| 272 | + filteredKeys: ["password"], |
| 273 | + }); |
| 274 | + |
| 275 | + expect(result).toContain('"name": "John"'); |
| 276 | + expect(result).toContain('"age": 30'); |
| 277 | + expect(result).not.toContain('"password"'); |
| 278 | + }); |
| 279 | + |
| 280 | + it("should handle BigInt values", async () => { |
| 281 | + const data = { id: BigInt(123456789), name: "John" }; |
| 282 | + const result = await prettyPrintPacket(data, "application/json"); |
| 283 | + |
| 284 | + expect(result).toContain('"id": "123456789"'); |
| 285 | + expect(result).toContain('"name": "John"'); |
| 286 | + }); |
| 287 | + |
| 288 | + it("should handle RegExp values", async () => { |
| 289 | + const data = { pattern: /test/gi, name: "John" }; |
| 290 | + const result = await prettyPrintPacket(data, "application/json"); |
| 291 | + |
| 292 | + expect(result).toContain('"pattern": "/test/gi"'); |
| 293 | + expect(result).toContain('"name": "John"'); |
| 294 | + }); |
| 295 | + |
| 296 | + it("should handle Set values", async () => { |
| 297 | + const data = { tags: new Set(["tag1", "tag2"]), name: "John" }; |
| 298 | + const result = await prettyPrintPacket(data, "application/json"); |
| 299 | + |
| 300 | + expect(result).toContain('"tags": [\n "tag1",\n "tag2"\n ]'); |
| 301 | + expect(result).toContain('"name": "John"'); |
| 302 | + }); |
| 303 | + |
| 304 | + it("should handle Map values", async () => { |
| 305 | + const data = { mapping: new Map([["key1", "value1"]]), name: "John" }; |
| 306 | + const result = await prettyPrintPacket(data, "application/json"); |
| 307 | + |
| 308 | + expect(result).toContain('"mapping": {\n "key1": "value1"\n }'); |
| 309 | + expect(result).toContain('"name": "John"'); |
| 310 | + }); |
| 311 | + |
| 312 | + it("should handle complex nested data", async () => { |
| 313 | + const data = { |
| 314 | + user: { |
| 315 | + id: BigInt(123), |
| 316 | + createdAt: new Date("2023-01-01"), |
| 317 | + settings: { |
| 318 | + theme: "dark", |
| 319 | + tags: new Set(["admin", "user"]), |
| 320 | + config: new Map([["timeout", "30s"]]), |
| 321 | + }, |
| 322 | + }, |
| 323 | + metadata: { |
| 324 | + version: 1, |
| 325 | + pattern: /^test$/, |
| 326 | + }, |
| 327 | + }; |
| 328 | + |
| 329 | + const result = await prettyPrintPacket(data, "application/json"); |
| 330 | + |
| 331 | + expect(result).toContain('"id": "123"'); |
| 332 | + expect(result).toContain('"createdAt": "2023-01-01T00:00:00.000Z"'); |
| 333 | + expect(result).toContain('"theme": "dark"'); |
| 334 | + expect(result).toContain('"tags": [\n "admin",\n "user"\n ]'); |
| 335 | + expect(result).toContain('"config": {\n "timeout": "30s"\n }'); |
| 336 | + expect(result).toContain('"version": 1'); |
| 337 | + expect(result).toContain('"pattern": "/^test$/"'); |
| 338 | + }); |
| 339 | + |
| 340 | + it("should handle data without dataType parameter", async () => { |
| 341 | + const data = { name: "John", age: 30 }; |
| 342 | + const result = await prettyPrintPacket(data); |
| 343 | + |
| 344 | + expect(result).toBe(JSON.stringify(data, null, 2)); |
| 345 | + }); |
| 346 | + }); |
191 | 347 | });
|
0 commit comments