fix(reactivity): readonly() compat with classes

fix #12574
This commit is contained in:
Evan You 2022-06-23 08:34:26 +08:00
parent 530b56a36e
commit 44ab1cda74
2 changed files with 22 additions and 1 deletions

View File

@ -69,7 +69,7 @@ function createReadonly(target: any, shallow: boolean) {
return existingProxy
}
const proxy = {}
const proxy = Object.create(Object.getPrototypeOf(target))
def(target, existingFlag, proxy)
def(proxy, ReactiveFlags.IS_READONLY, true)

View File

@ -499,4 +499,25 @@ describe('reactivity/readonly', () => {
expect(obj.ror).toBe(true)
expect(toRaw(obj).ror).not.toBe(ror) // ref successfully replaced
})
test('compatiblity with classes', () => {
const spy = vi.fn()
class Foo {
x = 1
log() {
spy(this.x)
}
change() {
this.x++
}
}
const foo = new Foo()
const readonlyFoo = readonly(foo)
readonlyFoo.log()
expect(spy).toHaveBeenCalledWith(1)
readonlyFoo.change()
expect(readonlyFoo.x).toBe(1)
expect(`et operation on key "x" failed`).toHaveBeenWarned()
})
})