fix(reactivity): fix shallow/readonly edge cases

This commit is contained in:
Evan You
2022-07-22 11:10:52 +08:00
parent 8dcb6c7bbd
commit a95554d35c
5 changed files with 69 additions and 14 deletions

View File

@@ -7,6 +7,7 @@ import {
} from '../src/reactive'
import { effect } from '../src/effect'
import { Ref, isRef, ref } from '../src/ref'
describe('shallowReactive', () => {
test('should not make non-reactive properties reactive', () => {
@@ -46,6 +47,27 @@ describe('shallowReactive', () => {
expect(isReactive(r.foo.bar)).toBe(false)
})
// vuejs/vue#12597
test('should not unwrap refs', () => {
const foo = shallowReactive({
bar: ref(123)
})
expect(isRef(foo.bar)).toBe(true)
expect(foo.bar.value).toBe(123)
})
// vuejs/vue#12688
test('should not mutate refs', () => {
const original = ref(123)
const foo = shallowReactive<{ bar: Ref<number> | number }>({
bar: original
})
expect(foo.bar).toBe(original)
foo.bar = 234
expect(foo.bar).toBe(234)
expect(original.value).toBe(123)
})
test('should respect shallow/deep versions of same target on access', () => {
const original = {}
const shallow = shallowReactive(original)