]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat: unwrap refs in toDisplayString
authorEvan You <yyx990803@gmail.com>
Tue, 27 Jul 2021 22:34:15 +0000 (18:34 -0400)
committerEvan You <yyx990803@gmail.com>
Tue, 27 Jul 2021 22:42:30 +0000 (18:42 -0400)
packages/shared/__tests__/toDisplayString.spec.ts
packages/shared/src/toDisplayString.ts

index a998904b1ab99516e4cfec266c4bfe2be629cb90..f9e20841e490283a6258eaa025bb74a7f2678123 100644 (file)
@@ -1,3 +1,4 @@
+import { computed, ref } from '@vue/reactivity'
 import { toDisplayString } from '../src'
 
 describe('toDisplayString', () => {
@@ -20,6 +21,17 @@ describe('toDisplayString', () => {
     expect(toDisplayString(arr)).toBe(JSON.stringify(arr, null, 2))
   })
 
+  test('refs', () => {
+    const n = ref(1)
+    const np = computed(() => n.value + 1)
+    expect(
+      toDisplayString({
+        n,
+        np
+      })
+    ).toBe(JSON.stringify({ n: 1, np: 2 }, null, 2))
+  })
+
   test('native objects', () => {
     const div = document.createElement('div')
     expect(toDisplayString(div)).toBe(`"[object HTMLDivElement]"`)
index a8ee3bbb54ed31820c2dfb4307d62df1bde726da..bcfe1a90721e402cba4f68439b4462d28221ffb1 100644 (file)
@@ -12,8 +12,11 @@ export const toDisplayString = (val: unknown): string => {
     : String(val)
 }
 
-const replacer = (_key: string, val: any) => {
-  if (isMap(val)) {
+const replacer = (_key: string, val: any): any => {
+  // can't use isRef here since @vue/shared has no deps
+  if (val && val.__v_isRef) {
+    return replacer(_key, val.value)
+  } else if (isMap(val)) {
     return {
       [`Map(${val.size})`]: [...val.entries()].reduce((entries, [key, val]) => {
         ;(entries as any)[`${key} =>`] = val