Skip to content

Commit

Permalink
fix(reactivity): ref as a reactive parameter should return (vuejs#6358)
Browse files Browse the repository at this point in the history
  • Loading branch information
hubvue committed Jul 26, 2022
1 parent a95554d commit 6b85ada
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 10 additions & 0 deletions packages/reactivity/__tests__/ref.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,16 @@ describe('reactivity/ref', () => {
expect(spy2).toBeCalledTimes(1)
})

test('ref is called as an argument to reactive, effectFn should only be triggered once', () => {
const obj = reactive(ref(1))
const spy1 = jest.fn(() => obj.value)

effect(spy1)

obj.value = 2
expect(spy1).toBeCalledTimes(2)
})

test('ref should preserve value shallow/readonly-ness', () => {
const original = {}
const r = reactive(original)
Expand Down
4 changes: 2 additions & 2 deletions packages/reactivity/src/reactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
shallowCollectionHandlers,
shallowReadonlyCollectionHandlers
} from './collectionHandlers'
import type { UnwrapRefSimple, Ref, RawSymbol } from './ref'
import { UnwrapRefSimple, Ref, RawSymbol, isRef } from './ref'

export const enum ReactiveFlags {
SKIP = '__v_skip',
Expand Down Expand Up @@ -89,7 +89,7 @@ export type UnwrapNestedRefs<T> = T extends Ref ? T : UnwrapRefSimple<T>
export function reactive<T extends object>(target: T): UnwrapNestedRefs<T>
export function reactive(target: object) {
// if trying to observe a readonly proxy, return the readonly version.
if (isReadonly(target)) {
if (isReadonly(target) || isRef(target)) {
return target
}
return createReactiveObject(
Expand Down

0 comments on commit 6b85ada

Please sign in to comment.