Skip to content

Commit

Permalink
fix: toRaw typo (#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jul 5, 2020
1 parent 94d4d87 commit 9468f72
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ export function mixin(Vue: VueConstructor) {
if (isPlainObject(binding)) {
const bindingObj = binding
vmStateManager.set(vm, 'rawBindings', binding)

Object.keys(binding).forEach((name) => {
let bindingValue = bindingObj[name]
// only make primitive value reactive
Expand All @@ -243,6 +244,7 @@ export function mixin(Vue: VueConstructor) {
}
asVmProperty(vm, name, bindingValue)
})

return
}

Expand Down
2 changes: 1 addition & 1 deletion src/reactivity/reactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ export function markRaw<T extends object>(obj: T): T {
}

export function toRaw<T>(observed: T): T {
if (isRaw(observe) || !Object.isExtensible(observed)) {
if (isRaw(observed) || !Object.isExtensible(observed)) {
return observed
}

Expand Down
5 changes: 4 additions & 1 deletion src/reactivity/unwrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export function unwrapRefProxy(value: any, map = new WeakMap()) {

// copy __ob__
if (value.__ob__) {
Object.defineProperty(obj, '__ob__', value.__ob__)
Object.defineProperty(obj, '__ob__', {
enumerable: false,
value: value.__ob__,
})
}

for (const k of Object.keys(value)) {
Expand Down
16 changes: 15 additions & 1 deletion test/setup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
reactive,
toRefs,
markRaw,
toRaw,
} = require('../src')

describe('setup', () => {
Expand Down Expand Up @@ -548,6 +549,8 @@ describe('setup', () => {
expect(
JSON.parse(vm.$el.querySelector('#refList').textContent)
).toMatchObject([{ value: '1' }, { value: '2' }, { value: '3' }])

expect(warn).not.toHaveBeenCalled()
})

test('nested', () => {
Expand Down Expand Up @@ -614,6 +617,8 @@ describe('setup', () => {
expect(vm.$el.querySelector('#nested_aaa_b').textContent).toBe('aaa')
expect(vm.$el.querySelector('#nested_aaa_bb_c').textContent).toBe('aaa')
expect(vm.$el.querySelector('#nested_aaa_bb_cc').textContent).toBe('aaa')

expect(warn).not.toHaveBeenCalled()
})

it('recursive', () => {
Expand Down Expand Up @@ -671,6 +676,8 @@ describe('setup', () => {
expect(
vm.$el.querySelector('#recursive_b_recursive_recursive_r').textContent
).toBe('r')

expect(warn).not.toHaveBeenCalled()
})

// #384
Expand All @@ -695,10 +702,14 @@ describe('setup', () => {
value: 'r',
},
})

expect(warn).not.toHaveBeenCalled()
})

// #392
it('should copy __ob__ and make toRaw work when passing via props', () => {
let propsObj = null

const Foo = {
template: '<p>{{obj.bar}}</p>',
props: {
Expand All @@ -708,7 +719,7 @@ describe('setup', () => {
},
},
setup(props) {
expect(toRaw(props.obj)).toEqual({ bar: 1 })
propsObj = toRaw(props.obj)
return {}
},
}
Expand All @@ -722,6 +733,9 @@ describe('setup', () => {
}).$mount()

expect(vm.$el.textContent).toBe('1')
expect(propsObj).toEqual({ bar: 1 })

expect(warn).not.toHaveBeenCalled()
})
})

Expand Down

0 comments on commit 9468f72

Please sign in to comment.