Skip to content

Commit

Permalink
fix(reactivity): readonly() compat with classes
Browse files Browse the repository at this point in the history
fix #12574
  • Loading branch information
yyx990803 committed Jun 23, 2022
1 parent 530b56a commit 44ab1cd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/v3/reactivity/readonly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions test/unit/features/v3/reactivity/readonly.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})

0 comments on commit 44ab1cd

Please sign in to comment.