fix(inject): should auto unwrap injected refs

fix #4196
This commit is contained in:
Evan You
2021-07-27 17:52:31 -04:00
parent 8681c12c0c
commit 561e210157
4 changed files with 119 additions and 15 deletions

View File

@@ -9,7 +9,8 @@ import {
renderToString,
ref,
defineComponent,
createApp
createApp,
computed
} from '@vue/runtime-test'
describe('api: options', () => {
@@ -426,6 +427,69 @@ describe('api: options', () => {
expect(renderToString(h(Root))).toBe(`1111234522`)
})
test('provide/inject refs', async () => {
const n = ref(0)
const np = computed(() => n.value + 1)
const Parent = defineComponent({
provide() {
return {
n,
np
}
},
render: () => h(Child)
})
const Child = defineComponent({
inject: ['n', 'np'],
render(this: any) {
return this.n + this.np
}
})
const app = createApp(Parent)
// TODO remove in 3.3
app.config.unwrapInjectedRef = true
const root = nodeOps.createElement('div')
app.mount(root)
expect(serializeInner(root)).toBe(`1`)
n.value++
await nextTick()
expect(serializeInner(root)).toBe(`3`)
})
// TODO remove in 3.3
test('provide/inject refs (compat)', async () => {
const n = ref(0)
const np = computed(() => n.value + 1)
const Parent = defineComponent({
provide() {
return {
n,
np
}
},
render: () => h(Child)
})
const Child = defineComponent({
inject: ['n', 'np'],
render(this: any) {
return this.n.value + this.np.value
}
})
const app = createApp(Parent)
const root = nodeOps.createElement('div')
app.mount(root)
expect(serializeInner(root)).toBe(`1`)
n.value++
await nextTick()
expect(serializeInner(root)).toBe(`3`)
expect(`injected property "n" is a ref`).toHaveBeenWarned()
expect(`injected property "np" is a ref`).toHaveBeenWarned()
})
test('provide accessing data in extends', () => {
const Base = defineComponent({
data() {