Skip to content

Commit 3201224

Browse files
authored
test(shared): improve test case for toDisplayString (#4337)
1 parent 42a334e commit 3201224

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

packages/shared/__tests__/toDisplayString.spec.ts

+40-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,46 @@ describe('toDisplayString', () => {
1919
expect(toDisplayString(obj)).toBe(JSON.stringify(obj, null, 2))
2020
const arr = [obj]
2121
expect(toDisplayString(arr)).toBe(JSON.stringify(arr, null, 2))
22-
const foo = Object.create(null)
23-
foo.bar = 1
24-
expect(toDisplayString(foo)).toBe(JSON.stringify(foo, null, 2))
22+
23+
const objWithToStringOverride = {
24+
foo: 555,
25+
toString() {
26+
return 'override'
27+
}
28+
}
29+
expect(toDisplayString(objWithToStringOverride)).toBe('override')
30+
31+
const objWithNonInvokeableToString = {
32+
foo: 555,
33+
toString: null
34+
}
35+
expect(toDisplayString(objWithNonInvokeableToString)).toBe(
36+
`{
37+
"foo": 555,
38+
"toString": null
39+
}`
40+
)
41+
42+
// object created from null does not have .toString in its prototype
43+
const nullObjectWithoutToString = Object.create(null)
44+
nullObjectWithoutToString.bar = 1
45+
expect(toDisplayString(nullObjectWithoutToString)).toBe(
46+
`{
47+
"bar": 1
48+
}`
49+
)
50+
51+
// array toString override is ignored
52+
const arrWithToStringOverride = [1, 2, 3]
53+
arrWithToStringOverride.toString = () =>
54+
'override for array is not supported'
55+
expect(toDisplayString(arrWithToStringOverride)).toBe(
56+
`[
57+
1,
58+
2,
59+
3
60+
]`
61+
)
2562
})
2663

2764
test('refs', () => {

0 commit comments

Comments
 (0)