]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
test(shared): improve test case for toDisplayString (#4337)
authorlidlanca <8693091+lidlanca@users.noreply.github.com>
Mon, 16 Aug 2021 19:34:59 +0000 (15:34 -0400)
committerGitHub <noreply@github.com>
Mon, 16 Aug 2021 19:34:59 +0000 (15:34 -0400)
packages/shared/__tests__/toDisplayString.spec.ts

index 385e40e35c975e42010a040c3da5d9e9e3ce5489..53cfcb87a1f1b334cb0f2ffd78a0e3e7a55aedff 100644 (file)
@@ -19,9 +19,46 @@ describe('toDisplayString', () => {
     expect(toDisplayString(obj)).toBe(JSON.stringify(obj, null, 2))
     const arr = [obj]
     expect(toDisplayString(arr)).toBe(JSON.stringify(arr, null, 2))
-    const foo = Object.create(null)
-    foo.bar = 1
-    expect(toDisplayString(foo)).toBe(JSON.stringify(foo, null, 2))
+
+    const objWithToStringOverride = {
+      foo: 555,
+      toString() {
+        return 'override'
+      }
+    }
+    expect(toDisplayString(objWithToStringOverride)).toBe('override')
+
+    const objWithNonInvokeableToString = {
+      foo: 555,
+      toString: null
+    }
+    expect(toDisplayString(objWithNonInvokeableToString)).toBe(
+      `{
+  "foo": 555,
+  "toString": null
+}`
+    )
+
+    // object created from null does not have .toString in its prototype
+    const nullObjectWithoutToString = Object.create(null)
+    nullObjectWithoutToString.bar = 1
+    expect(toDisplayString(nullObjectWithoutToString)).toBe(
+      `{
+  "bar": 1
+}`
+    )
+
+    // array toString override is ignored
+    const arrWithToStringOverride = [1, 2, 3]
+    arrWithToStringOverride.toString = () =>
+      'override for array is not supported'
+    expect(toDisplayString(arrWithToStringOverride)).toBe(
+      `[
+  1,
+  2,
+  3
+]`
+    )
   })
 
   test('refs', () => {