2021-09-22 00:55:08 +08:00
|
|
|
import { createApp, nodeOps, render } from '@vue/runtime-test'
|
2020-11-15 01:49:35 +08:00
|
|
|
import { defineComponent, h, ref } from '../src'
|
|
|
|
|
|
|
|
describe('api: expose', () => {
|
|
|
|
test('via setup context', () => {
|
|
|
|
const Child = defineComponent({
|
|
|
|
render() {},
|
|
|
|
setup(_, { expose }) {
|
|
|
|
expose({
|
2021-06-25 09:28:09 +08:00
|
|
|
foo: 1,
|
2020-11-15 01:49:35 +08:00
|
|
|
bar: ref(2)
|
|
|
|
})
|
|
|
|
return {
|
|
|
|
bar: ref(3),
|
|
|
|
baz: ref(4)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const childRef = ref()
|
|
|
|
const Parent = {
|
|
|
|
setup() {
|
|
|
|
return () => h(Child, { ref: childRef })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
render(h(Parent), root)
|
|
|
|
expect(childRef.value).toBeTruthy()
|
|
|
|
expect(childRef.value.foo).toBe(1)
|
|
|
|
expect(childRef.value.bar).toBe(2)
|
|
|
|
expect(childRef.value.baz).toBeUndefined()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('via options', () => {
|
|
|
|
const Child = defineComponent({
|
|
|
|
render() {},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
foo: 1
|
|
|
|
}
|
|
|
|
},
|
|
|
|
setup() {
|
|
|
|
return {
|
|
|
|
bar: ref(2),
|
|
|
|
baz: ref(3)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
expose: ['foo', 'bar']
|
|
|
|
})
|
|
|
|
|
|
|
|
const childRef = ref()
|
|
|
|
const Parent = {
|
|
|
|
setup() {
|
|
|
|
return () => h(Child, { ref: childRef })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
render(h(Parent), root)
|
|
|
|
expect(childRef.value).toBeTruthy()
|
|
|
|
expect(childRef.value.foo).toBe(1)
|
|
|
|
expect(childRef.value.bar).toBe(2)
|
|
|
|
expect(childRef.value.baz).toBeUndefined()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('options + context', () => {
|
|
|
|
const Child = defineComponent({
|
|
|
|
render() {},
|
|
|
|
expose: ['foo'],
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
foo: 1
|
|
|
|
}
|
|
|
|
},
|
|
|
|
setup(_, { expose }) {
|
|
|
|
expose({
|
|
|
|
bar: ref(2)
|
|
|
|
})
|
|
|
|
return {
|
|
|
|
bar: ref(3),
|
|
|
|
baz: ref(4)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const childRef = ref()
|
|
|
|
const Parent = {
|
|
|
|
setup() {
|
|
|
|
return () => h(Child, { ref: childRef })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
render(h(Parent), root)
|
|
|
|
expect(childRef.value).toBeTruthy()
|
|
|
|
expect(childRef.value.foo).toBe(1)
|
|
|
|
expect(childRef.value.bar).toBe(2)
|
|
|
|
expect(childRef.value.baz).toBeUndefined()
|
|
|
|
})
|
2020-11-17 00:28:37 +08:00
|
|
|
|
|
|
|
test('options: empty', () => {
|
|
|
|
const Child = defineComponent({
|
|
|
|
render() {},
|
|
|
|
expose: [],
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
foo: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const childRef = ref()
|
|
|
|
const Parent = {
|
|
|
|
setup() {
|
|
|
|
return () => h(Child, { ref: childRef })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
render(h(Parent), root)
|
|
|
|
expect(childRef.value).toBeTruthy()
|
|
|
|
expect('foo' in childRef.value).toBe(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('options: empty + setup context', () => {
|
|
|
|
const Child = defineComponent({
|
|
|
|
render() {},
|
|
|
|
expose: [],
|
|
|
|
setup(_, { expose }) {
|
|
|
|
expose({
|
|
|
|
foo: 1
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const childRef = ref()
|
|
|
|
const Parent = {
|
|
|
|
setup() {
|
|
|
|
return () => h(Child, { ref: childRef })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
render(h(Parent), root)
|
|
|
|
expect(childRef.value).toBeTruthy()
|
|
|
|
expect(childRef.value.foo).toBe(1)
|
|
|
|
})
|
2021-02-07 21:39:52 +08:00
|
|
|
|
|
|
|
test('with $parent/$root', () => {
|
|
|
|
const Child = defineComponent({
|
|
|
|
render() {
|
|
|
|
expect((this.$parent! as any).foo).toBe(1)
|
|
|
|
expect((this.$parent! as any).bar).toBe(undefined)
|
|
|
|
expect((this.$root! as any).foo).toBe(1)
|
|
|
|
expect((this.$root! as any).bar).toBe(undefined)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const Parent = defineComponent({
|
|
|
|
expose: [],
|
|
|
|
setup(_, { expose }) {
|
|
|
|
expose({
|
|
|
|
foo: 1
|
|
|
|
})
|
|
|
|
return {
|
|
|
|
bar: 2
|
|
|
|
}
|
|
|
|
},
|
|
|
|
render() {
|
|
|
|
return h(Child)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
render(h(Parent), root)
|
|
|
|
})
|
2021-06-25 09:28:09 +08:00
|
|
|
|
2021-09-22 00:55:08 +08:00
|
|
|
test('with mount', () => {
|
|
|
|
const Component = defineComponent({
|
|
|
|
setup(_, { expose }) {
|
|
|
|
expose({
|
|
|
|
foo: 1
|
|
|
|
})
|
|
|
|
return {
|
|
|
|
bar: 2
|
|
|
|
}
|
|
|
|
},
|
|
|
|
render() {
|
|
|
|
return h('div')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
const vm = createApp(Component).mount(root) as any
|
|
|
|
expect(vm.foo).toBe(1)
|
|
|
|
expect(vm.bar).toBe(undefined)
|
|
|
|
})
|
|
|
|
|
2021-06-25 09:28:09 +08:00
|
|
|
test('expose should allow access to built-in instance properties', () => {
|
2021-07-02 19:51:54 +08:00
|
|
|
const GrandChild = defineComponent({
|
|
|
|
render() {
|
|
|
|
return h('div')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const grandChildRef = ref()
|
2021-06-25 09:28:09 +08:00
|
|
|
const Child = defineComponent({
|
|
|
|
render() {
|
|
|
|
return h('div')
|
|
|
|
},
|
|
|
|
setup(_, { expose }) {
|
|
|
|
expose()
|
2021-07-02 19:51:54 +08:00
|
|
|
return () => h(GrandChild, { ref: grandChildRef })
|
2021-06-25 09:28:09 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const childRef = ref()
|
|
|
|
const Parent = {
|
|
|
|
setup() {
|
|
|
|
return () => h(Child, { ref: childRef })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
render(h(Parent), root)
|
|
|
|
expect(childRef.value.$el.tag).toBe('div')
|
2021-07-02 19:51:54 +08:00
|
|
|
expect(grandChildRef.value.$parent).toBe(childRef.value)
|
|
|
|
expect(grandChildRef.value.$parent.$parent).toBe(grandChildRef.value.$root)
|
2021-06-25 09:28:09 +08:00
|
|
|
})
|
2020-11-15 01:49:35 +08:00
|
|
|
})
|