Skip to content

Commit 9be0786

Browse files
committed
Added tests for #25
1 parent 859f157 commit 9be0786

File tree

1 file changed

+83
-11
lines changed

1 file changed

+83
-11
lines changed

packages/core/test/Utils.test.ts

+83-11
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe("Utils", () => {
7171
expect(stringify([{ one: aFoo, two: aFoo }])).toBe("[{\"one\":{\"a\":\"foo\"}}]");
7272
});
7373

74-
test.skip("deep circular reference", () => {
74+
test("deep circular object reference", () => {
7575
const a: { b?: unknown } = {};
7676
const b: { c?: unknown } = {};
7777
const c: { a?: unknown, d: string } = { d: "test" };
@@ -86,6 +86,69 @@ describe("Utils", () => {
8686
expect(actual).toBe(expected);
8787
});
8888

89+
test("deep circular array reference", () => {
90+
type Circular = { circularRef?: Circular, list?: Circular[] };
91+
const circular: Circular = {};
92+
circular.circularRef = circular;
93+
circular.list = [circular, circular];
94+
95+
const expected = "{\"circularRef\":\"[Circular]\",\"list\":[\"[Circular]\",\"[Circular]\"]}";
96+
97+
const actual = stringify(circular);
98+
expect(actual).toBe(expected);
99+
});
100+
101+
test("should serialize all data types", () => {
102+
const value = {
103+
"undefined": undefined,
104+
"null": null,
105+
"string": "string",
106+
"number": 1,
107+
"boolean": true,
108+
"array": [1, 2, 3],
109+
"object": { "a": 1, "b": 2, "c": 3 },
110+
"date": new Date(),
111+
"regex": /regex/,
112+
"function": () => { return undefined; },
113+
"error": new Error("error"),
114+
"symbol": Symbol("symbol"),
115+
"bigint": BigInt(1),
116+
"map": new Map([["a", 1], ["b", 2], ["c", 3]]),
117+
"weakMap": new WeakMap([[{}, 1], [{}, 2], [{}, 3]]),
118+
"set": new Set([1, 2, 3]),
119+
"arrayBuffer": new ArrayBuffer(1),
120+
"dataView": new DataView(new ArrayBuffer(1)),
121+
"int8Array": new Int8Array(1),
122+
"uint8Array": new Uint8Array(1),
123+
"uint8ClampedArray": new Uint8ClampedArray(1),
124+
"int16Array": new Int16Array(1),
125+
"uint16Array": new Uint16Array(1),
126+
"int32Array": new Int32Array(1),
127+
"uint32Array": new Uint32Array(1),
128+
"float32Array": new Float32Array(1),
129+
"float64Array": new Float64Array(1),
130+
"bigint64Array": new BigInt64Array(1),
131+
"bigUint64Array": new BigUint64Array(1),
132+
"promise": Promise.resolve(1),
133+
"generator": (function* () { yield 1; })(),
134+
};
135+
136+
expect(stringify(value)).toBe(JSON.stringify(value));
137+
});
138+
139+
test("should handle toJSON", () => {
140+
const value = {
141+
number: 1,
142+
toJSON() {
143+
return {
144+
test: "test"
145+
};
146+
}
147+
};
148+
149+
expect(stringify(value)).toBe(JSON.stringify(value));
150+
});
151+
89152
describe("should behave like JSON.stringify", () => {
90153
[new Date(), 1, true, null, undefined, () => { return undefined; }, user].forEach((value) => {
91154
test("for " + typeof (value), () => {
@@ -94,21 +157,30 @@ describe("Utils", () => {
94157
});
95158
});
96159

97-
/*
98-
test.skip("should respect maxDepth", () => {
99-
const deepObject = {
100-
a: {
101-
b: {
102-
c: {
103-
d: {}
160+
test("should respect maxDepth", () => {
161+
const value = {
162+
ao: {
163+
bo: {
164+
cn: 1,
165+
co: {
166+
do: {}
167+
}
168+
},
169+
ba: [
170+
{
171+
cn: 1,
172+
co: {
173+
do: {}
174+
}
104175
}
105-
}
176+
],
177+
bn: 1
106178
}
107179
};
108180

109-
expect(deepObject).toBe("TODO");
181+
expect(stringify(value, undefined, 1)).toBe(JSON.stringify({ ao: { bn: 1 } }));
182+
expect(stringify(value, undefined, 2)).toBe(JSON.stringify({ ao: { bo: { cn: 1 }, bn: 1 } }));
110183
});
111-
*/
112184

113185
test("should serialize inherited properties", () => {
114186
// @ts-expect-error TS2683

0 commit comments

Comments
 (0)