-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.ts
128 lines (109 loc) · 2.79 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
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
import { convertJsonToTs } from "../src/index";
describe("convertJsonToTs - Additional Test Cases", () => {
it.skip("should handle empty arrays gracefully", () => {
const json = { emptyArray: [] };
const result = convertJsonToTs(json, "Root");
const expected = `
interface Root {
emptyArray: any[];
}
`.trim();
expect(result.trim().replace(/\s+/g, " ")).toBe(
expected.replace(/\s+/g, " ")
);
});
it("should handle mixed nested arrays", () => {
const json = { nested: [[[1]], [2], 3] };
const result = convertJsonToTs(json, "Root");
const expected = `
interface Root {
nested: number[][][];
}
`.trim();
expect(result.trim().replace(/\s+/g, " ")).toBe(
expected.replace(/\s+/g, " ")
);
});
it("should handle dates as Date type", () => {
const json = { createdAt: "2023-01-01T12:00:00Z", updatedAt: null };
const result = convertJsonToTs(json, "Root");
const expected = `
interface Root {
createdAt: Date;
updatedAt?: any;
}
`.trim();
expect(result.trim().replace(/\s+/g, " ")).toBe(
expected.replace(/\s+/g, " ")
);
});
it("should handle deeply nested objects", () => {
const json = {
level1: {
level2: {
level3: {
level4: {
value: "deep",
},
},
},
},
};
const result = convertJsonToTs(json, "Root");
const expected = `
interface Root {
level1: Level1;
}
interface Level1 {
level2: Level2;
}
interface Level2 {
level3: Level3;
}
interface Level3 {
level4: Level4;
}
interface Level4 {
value: string;
}
`.trim();
expect(result.trim().replace(/\s+/g, " ")).toBe(
expected.replace(/\s+/g, " ")
);
});
it("should handle circular references", () => {
const json: any = {};
json.self = json; // Circular reference
expect(() => convertJsonToTs(json, "Root")).toThrow(
/Circular reference detected in Root/
);
});
it("should handle empty objects", () => {
const json = { emptyObject: {} };
const result = convertJsonToTs(json, "Root");
const expected = `
interface Root {
emptyObject: EmptyObject;
}
interface EmptyObject {}
`.trim();
expect(result.trim().replace(/\s+/g, " ")).toBe(
expected.replace(/\s+/g, " ")
);
});
it("should handle arrays of objects", () => {
const json = { items: [{ name: "Item 1" }, { name: "Item 2" }] };
const result = convertJsonToTs(json, "Root");
const expected = `
interface Root {
items: Item[];
}
interface Item {
name: string;
}
`.trim();
expect(result.trim().replace(/\s+/g, " ")).toBe(
expected.replace(/\s+/g, " ")
);
});
});