Skip to content

Commit

Permalink
test: add observable __proto__ test case
Browse files Browse the repository at this point in the history
  • Loading branch information
yiyunwan committed Dec 7, 2023
1 parent 5384dab commit 5ce9a86
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
14 changes: 14 additions & 0 deletions packages/reactive/src/__tests__/observable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,17 @@ test('observable contains', () => {
expect(contains(obj, obj.arr)).toBe(true)
expect(contains(obj, arr)).toBe(true)
})

test('observable __proto__', () => {
const observableArr = observable([] as any[])
// @ts-ignore
observableArr.__proto__ = Object.create(Array.prototype)
observableArr[0] = {}
expect(observableArr).toEqual([{}])

const observableObj = observable({} as any)
// @ts-ignore
observableObj.__proto__ = Object.create(Object.prototype)
observableObj.aa = {}
expect(observableObj).toEqual({ aa: {} })
})
3 changes: 1 addition & 2 deletions packages/reactive/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
import { ProxyRaw, RawProxy } from './environment'
import { isObservable, isSupportObservable } from './externals'
import { createObservable } from './internals'
import { isArr } from './checkers'

const wellKnownSymbols = new Set(
Object.getOwnPropertyNames(Symbol).reduce((buf: Symbol[], key) => {
Expand Down Expand Up @@ -202,7 +201,7 @@ export const baseHandlers: ProxyHandler<any> = {
},
set(target, key, value, receiver) {
// vue2中有对数组原型重写,因此需去除此处proxy
if (key === '__proto__' && isArr(target)) {
if (key === '__proto__') {
target[key] = value
return true
}
Expand Down

0 comments on commit 5ce9a86

Please sign in to comment.