Skip to content
Permalink
Browse files
fix(reactivity): ensure readonly refs can be replaced with new refs i…
…n reactive objects (#5310)

fix #5307
  • Loading branch information
LinusBorg committed Jan 23, 2022
1 parent 059c63e commit 4be1037f31e169d667059c44364fc3e43803accb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
@@ -476,7 +476,7 @@ describe('reactivity/readonly', () => {
expect(isReadonly(rr.foo)).toBe(true)
})

test('attemptingt to write to a readonly ref nested in a reactive object should fail', () => {
test('attemptingt to write plain value to a readonly ref nested in a reactive object should fail', () => {
const r = ref(false)
const ror = readonly(r)
const obj = reactive({ ror })
@@ -486,4 +486,15 @@ describe('reactivity/readonly', () => {

expect(obj.ror).toBe(false)
})
test('replacing a readonly ref nested in a reactive object with a new ref', () => {
const r = ref(false)
const ror = readonly(r)
const obj = reactive({ ror })
try {
obj.ror = ref(true) as unknown as boolean
} catch (e) {}

expect(obj.ror).toBe(true)
expect(toRaw(obj).ror).not.toBe(ror) // ref successfully replaced
})
})
@@ -150,7 +150,7 @@ function createSetter(shallow = false) {
receiver: object
): boolean {
let oldValue = (target as any)[key]
if (isReadonly(oldValue) && isRef(oldValue)) {
if (isReadonly(oldValue) && isRef(oldValue) && !isRef(value)) {
return false
}
if (!shallow && !isReadonly(value)) {

0 comments on commit 4be1037

Please sign in to comment.