From 0614e8290a7fb45ca0f847086a9d067731ebba3a Mon Sep 17 00:00:00 2001 From: neiyichao03 Date: Sat, 25 Sep 2021 03:01:04 +0800 Subject: [PATCH] fix: When using proxyRefs, the internal variable composition-api.refKey is exposed on the object itself #817 --- src/reactivity/ref.ts | 4 +++- test/v3/reactivity/ref.spec.ts | 13 ++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/reactivity/ref.ts b/src/reactivity/ref.ts index bf49d1c7..97bb2a8c 100644 --- a/src/reactivity/ref.ts +++ b/src/reactivity/ref.ts @@ -1,5 +1,5 @@ import { RefKey } from '../utils/symbols' -import { proxy, isPlainObject, warn } from '../utils' +import { proxy, isPlainObject, warn, def } from '../utils' import { reactive, isReactive, shallowReactive } from './reactive' import { readonlySet } from '../utils/sets' @@ -172,6 +172,8 @@ export function proxyRefs( } const value: Record = reactive({ [RefKey]: objectWithRefs }) + def(value, RefKey, value[RefKey], false) + for (const key of Object.keys(objectWithRefs)) { proxy(value, key, { get() { diff --git a/test/v3/reactivity/ref.spec.ts b/test/v3/reactivity/ref.spec.ts index 5f5a3371..54d94016 100644 --- a/test/v3/reactivity/ref.spec.ts +++ b/test/v3/reactivity/ref.spec.ts @@ -13,7 +13,9 @@ import { isReactive, shallowRef, proxyRefs, + ShallowUnwrapRef, } from '../../../src' +import { RefKey } from '../../../src/utils/symbols' describe('reactivity/ref', () => { it('should hold a value', () => { @@ -379,19 +381,28 @@ describe('reactivity/ref', () => { y: ref('foo'), }, } - const p = proxyRefs(a) + const reactiveA = { + [RefKey]: a, + ...a, + } + const p = proxyRefs(a) as ShallowUnwrapRef expect(p.x).toBe(1) expect(p.obj.y).toBe('foo') + expect(p[RefKey]).toBe(a) + expect(Object.keys(p)).toStrictEqual(['x', 'obj']) // @ts-expect-error p.obj.y = 'bar' p.x = 2 expect(a.x).toBe(2) expect(a.obj.y).toBe('bar') + expect(p[RefKey]).toBe(a) + expect(Object.keys(p)).toStrictEqual(['x', 'obj']) const r = reactive({ k: 'v' }) const s = proxyRefs(r) expect(s.k).toBe('v') + expect(s[RefKey]) r.k = 'k' expect(s.k).toBe('k')