apiSetupContext.spec.ts 5.0 KB

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