Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(toRefs): do not warn when toRefs is called in a prop value #405

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/setup.ts
Expand Up @@ -220,8 +220,12 @@ export function mixin(Vue: VueConstructor) {
if (isFunction(bindingValue)) {
bindingValue = bindingValue.bind(vm)
}
// unwrap all ref properties
const unwrapped = unwrapRefProxy(bindingValue)
// mark the object as reactive
markReactive(unwrapped)
// a non-reactive should not don't get reactivity
bindingValue = ref(markRaw(unwrapRefProxy(bindingValue)))
bindingValue = ref(markRaw(unwrapped))
}
}
asVmProperty(vm, name, bindingValue)
Expand Down
50 changes: 50 additions & 0 deletions test/setup.spec.js
Expand Up @@ -9,6 +9,7 @@ const {
toRefs,
markRaw,
} = require('../src')
const { nextTick } = require('process')
pikax marked this conversation as resolved.
Show resolved Hide resolved

describe('setup', () => {
beforeEach(() => {
Expand Down Expand Up @@ -176,6 +177,55 @@ describe('setup', () => {
)
})

it('not warn doing toRef on props', async () => {
const Foo = {
props: {
obj: {
type: Object,
required: true,
},
},
setup(props) {
return () =>
h('div', null, [
h('span', toRefs(props.obj).bar.value),
h('span', toRefs(props.obj.nested).baz.value),
])
},
}

let bar
let baz

const vm = new Vue({
template: `<div id="app"><Foo :obj="obj" /></div>`,
components: { Foo },
setup() {
bar = ref(3)
baz = ref(1)
return {
obj: {
bar,
nested: {
baz,
},
},
}
},
})
vm.$mount()

expect(warn).not.toHaveBeenCalled()
expect(vm.$el.textContent).toBe('31')

bar.value = 4
baz.value = 2

await vm.$nextTick()
expect(warn).not.toHaveBeenCalled()
expect(vm.$el.textContent).toBe('42')
})

it('should merge result properly', () => {
const injectKey = Symbol('foo')
const A = Vue.extend({
Expand Down