Skip to content

Commit b4ca954

Browse files
committed
Add more test cases
1 parent 7a0d2f2 commit b4ca954

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

test/str.spec.ts renamed to test/object.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import stringify from "../src";
22

3+
test("empty object", () => {
4+
expect(stringify({})).toEqual("{}");
5+
});
6+
37
test("simple object", () => {
48
const obj = { c: 6, b: [4, 5], a: 3, z: null };
59
expect(stringify(obj)).toEqual('{"a":3,"b":[4,5],"c":6,"z":null}');
@@ -30,7 +34,38 @@ test("object with empty string", () => {
3034
expect(stringify(obj)).toEqual('{"a":3,"z":""}');
3135
});
3236

37+
test("object with Symbol", () => {
38+
const obj = { a: 12, z: Symbol("") };
39+
expect(stringify(obj)).toEqual('{"a":12}');
40+
});
41+
42+
test("non-enumerable properties", () => {
43+
const obj = Object.create(null, {
44+
x: { value: "x", enumerable: false },
45+
y: { value: "y", enumerable: true },
46+
});
47+
expect(stringify(obj)).toEqual('{"y":"y"}');
48+
});
49+
3350
test("array with empty string", () => {
3451
const obj = [4, "", 6];
3552
expect(stringify(obj)).toEqual('[4,"",6]');
3653
});
54+
55+
test("simple function", () => {
56+
const obj = () => {
57+
// empty
58+
};
59+
expect(stringify(obj)).toEqual(undefined);
60+
});
61+
62+
test("standard data structures", () => {
63+
expect(
64+
stringify([
65+
new Set([1]),
66+
new Map([[1, 2]]),
67+
new WeakSet([{ a: 1 }]),
68+
new WeakMap([[{ a: 1 }, 2]]),
69+
])
70+
).toEqual("[{},{},{},{}]");
71+
});

test/primitive.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import stringify from "../src";
2+
3+
test("undefined", () => {
4+
expect(stringify(undefined)).toEqual(undefined);
5+
});
6+
7+
test("true", () => {
8+
expect(stringify(true)).toEqual("true");
9+
});
10+
11+
test("string", () => {
12+
expect(stringify("foo")).toEqual(`"foo"`);
13+
});
14+
15+
test("symbol", () => {
16+
expect(stringify(Symbol(""))).toEqual(undefined);
17+
});
18+
19+
test("NaN", () => {
20+
expect(stringify(NaN)).toEqual("null");
21+
});
22+
23+
test("Infinity", () => {
24+
expect(stringify(Infinity)).toEqual("null");
25+
});
26+
27+
test("null", () => {
28+
expect(stringify(null)).toEqual("null");
29+
});

0 commit comments

Comments
 (0)