Skip to content

Commit

Permalink
fix(reactivity): fix toRaw for objects prototype inherting reactive
Browse files Browse the repository at this point in the history
fix #1246
  • Loading branch information
yyx990803 committed Jun 11, 2020
1 parent f3623e4 commit 10bb34b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 9 additions & 1 deletion packages/reactivity/__tests__/reactive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,21 @@ describe('reactivity/reactive', () => {
expect(original.bar).toBe(original2)
})

test('unwrap', () => {
test('toRaw', () => {
const original = { foo: 1 }
const observed = reactive(original)
expect(toRaw(observed)).toBe(original)
expect(toRaw(original)).toBe(original)
})

test('toRaw on object using reactive as prototype', () => {
const original = reactive({})
const obj = Object.create(original)
const raw = toRaw(obj)
expect(raw).toBe(obj)
expect(raw).not.toBe(toRaw(original))
})

test('should not unwrap Ref<T>', () => {
const observedNumberRef = reactive(ref(1))
const observedObjectRef = reactive(ref({ foo: 1 }))
Expand Down
8 changes: 7 additions & 1 deletion packages/reactivity/src/baseHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ function createGetter(isReadonly = false, shallow = false) {
return !isReadonly
} else if (key === ReactiveFlags.isReadonly) {
return isReadonly
} else if (key === ReactiveFlags.raw) {
} else if (
key === ReactiveFlags.raw &&
receiver ===
(isReadonly
? (target as any).__v_readonly
: (target as any).__v_reactive)
) {
return target
}

Expand Down

0 comments on commit 10bb34b

Please sign in to comment.