apiSetupContext.spec.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import { reactive, ref } from '@vue/reactivity'
  2. import {
  3. type TestElement,
  4. defineComponent,
  5. h,
  6. nextTick,
  7. nodeOps,
  8. render,
  9. renderToString,
  10. serializeInner,
  11. triggerEvent,
  12. watchEffect,
  13. } from '@vue/runtime-test'
  14. describe('api: setup context', () => {
  15. it('should expose return values to template render context', () => {
  16. const Comp = defineComponent({
  17. setup() {
  18. return {
  19. // ref should auto-unwrap
  20. ref: ref('foo'),
  21. // object exposed as-is
  22. object: reactive({ msg: 'bar' }),
  23. // primitive value exposed as-is
  24. value: 'baz',
  25. }
  26. },
  27. render() {
  28. return `${this.ref} ${this.object.msg} ${this.value}`
  29. },
  30. })
  31. expect(renderToString(h(Comp))).toMatch(`foo bar baz`)
  32. })
  33. it('should support returning render function', () => {
  34. const Comp = {
  35. setup() {
  36. return () => {
  37. return h('div', 'hello')
  38. }
  39. },
  40. }
  41. expect(renderToString(h(Comp))).toMatch(`hello`)
  42. })
  43. it('props', async () => {
  44. const count = ref(0)
  45. let dummy
  46. const Parent = {
  47. render: () => h(Child, { count: count.value }),
  48. }
  49. const Child = defineComponent({
  50. props: { count: Number },
  51. setup(props) {
  52. watchEffect(() => {
  53. dummy = props.count
  54. })
  55. return () => h('div', props.count)
  56. },
  57. })
  58. const root = nodeOps.createElement('div')
  59. render(h(Parent), root)
  60. expect(serializeInner(root)).toMatch(`<div>0</div>`)
  61. expect(dummy).toBe(0)
  62. // props should be reactive
  63. count.value++
  64. await nextTick()
  65. expect(serializeInner(root)).toMatch(`<div>1</div>`)
  66. expect(dummy).toBe(1)
  67. })
  68. it('context.attrs', async () => {
  69. const toggle = ref(true)
  70. const Parent = {
  71. render: () => h(Child, toggle.value ? { id: 'foo' } : { class: 'baz' }),
  72. }
  73. const Child = {
  74. // explicit empty props declaration
  75. // puts everything received in attrs
  76. // disable implicit fallthrough
  77. inheritAttrs: false,
  78. setup(props: any, { attrs }: any) {
  79. return () => h('div', attrs)
  80. },
  81. }
  82. const root = nodeOps.createElement('div')
  83. render(h(Parent), root)
  84. expect(serializeInner(root)).toMatch(`<div id="foo"></div>`)
  85. // should update even though it's not reactive
  86. toggle.value = false
  87. await nextTick()
  88. expect(serializeInner(root)).toMatch(`<div class="baz"></div>`)
  89. })
  90. // #4161
  91. it('context.attrs in child component slots', async () => {
  92. const toggle = ref(true)
  93. const Parent = {
  94. render: () => h(Child, toggle.value ? { id: 'foo' } : { class: 'baz' }),
  95. }
  96. const Wrapper = {
  97. render(this: any) {
  98. return this.$slots.default()
  99. },
  100. }
  101. const Child = {
  102. inheritAttrs: false,
  103. setup(_: any, { attrs }: any) {
  104. return () => {
  105. const vnode = h(Wrapper, null, {
  106. default: () => [h('div', attrs)],
  107. _: 1, // mark stable slots
  108. })
  109. vnode.dynamicChildren = [] // force optimized mode
  110. return vnode
  111. }
  112. },
  113. }
  114. const root = nodeOps.createElement('div')
  115. render(h(Parent), root)
  116. expect(serializeInner(root)).toMatch(`<div id="foo"></div>`)
  117. // should update even though it's not reactive
  118. toggle.value = false
  119. await nextTick()
  120. expect(serializeInner(root)).toMatch(`<div class="baz"></div>`)
  121. })
  122. it('context.slots', async () => {
  123. const id = ref('foo')
  124. const Parent = {
  125. render: () =>
  126. h(Child, null, {
  127. foo: () => id.value,
  128. bar: () => 'bar',
  129. }),
  130. }
  131. const Child = {
  132. setup(props: any, { slots }: any) {
  133. return () => h('div', [...slots.foo(), ...slots.bar()])
  134. },
  135. }
  136. const root = nodeOps.createElement('div')
  137. render(h(Parent), root)
  138. expect(serializeInner(root)).toMatch(`<div>foobar</div>`)
  139. // should update even though it's not reactive
  140. id.value = 'baz'
  141. await nextTick()
  142. expect(serializeInner(root)).toMatch(`<div>bazbar</div>`)
  143. })
  144. it('context.emit', async () => {
  145. const count = ref(0)
  146. const spy = vi.fn()
  147. const Parent = {
  148. render: () =>
  149. h(Child, {
  150. count: count.value,
  151. onInc: (newVal: number) => {
  152. spy()
  153. count.value = newVal
  154. },
  155. }),
  156. }
  157. const Child = defineComponent({
  158. props: {
  159. count: {
  160. type: Number,
  161. default: 1,
  162. },
  163. },
  164. setup(props, { emit }) {
  165. return () =>
  166. h(
  167. 'div',
  168. {
  169. onClick: () => emit('inc', props.count + 1),
  170. },
  171. props.count,
  172. )
  173. },
  174. })
  175. const root = nodeOps.createElement('div')
  176. render(h(Parent), root)
  177. expect(serializeInner(root)).toMatch(`<div>0</div>`)
  178. // emit should trigger parent handler
  179. triggerEvent(root.children[0] as TestElement, 'click')
  180. expect(spy).toHaveBeenCalled()
  181. await nextTick()
  182. expect(serializeInner(root)).toMatch(`<div>1</div>`)
  183. })
  184. })