Skip to content

Commit

Permalink
fix(reactivity): ensure readonly refs can be replaced with new refs i…
Browse files Browse the repository at this point in the history
…n reactive objects (#5310)

fix #5307
  • Loading branch information
LinusBorg authored Jan 23, 2022
1 parent 059c63e commit 4be1037
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 12 additions & 1 deletion packages/reactivity/__tests__/readonly.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand All @@ -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
})
})
2 changes: 1 addition & 1 deletion packages/reactivity/src/baseHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit 4be1037

Please sign in to comment.