apiSetupContext.spec.ts 5.1 KB

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