| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- import { nodeOps, render } from '@vue/runtime-test'
- import { defineComponent, h, ref } from '../src'
- describe('api: expose', () => {
- test('via setup context', () => {
- const Child = defineComponent({
- render() {},
- setup(_, { expose }) {
- expose({
- foo: ref(1),
- 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()
- })
- 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)
- })
- })
|