apiSetupContext.spec.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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('setup props should resolve the correct types from props object', async () => {
  70. const count = ref(0)
  71. let dummy
  72. const Parent = {
  73. render: () => h(Child, { count: count.value })
  74. }
  75. const Child = defineComponent({
  76. props: {
  77. count: Number
  78. },
  79. setup(props) {
  80. watchEffect(() => {
  81. dummy = props.count
  82. })
  83. return () => h('div', props.count)
  84. }
  85. })
  86. const root = nodeOps.createElement('div')
  87. render(h(Parent), root)
  88. expect(serializeInner(root)).toMatch(`<div>0</div>`)
  89. expect(dummy).toBe(0)
  90. // props should be reactive
  91. count.value++
  92. await nextTick()
  93. expect(serializeInner(root)).toMatch(`<div>1</div>`)
  94. expect(dummy).toBe(1)
  95. })
  96. it('context.attrs', async () => {
  97. const toggle = ref(true)
  98. const Parent = {
  99. render: () => h(Child, toggle.value ? { id: 'foo' } : { class: 'baz' })
  100. }
  101. const Child = {
  102. // explicit empty props declaration
  103. // puts everything received in attrs
  104. // disable implicit fallthrough
  105. inheritAttrs: false,
  106. setup(props: any, { attrs }: any) {
  107. return () => h('div', attrs)
  108. }
  109. }
  110. const root = nodeOps.createElement('div')
  111. render(h(Parent), root)
  112. expect(serializeInner(root)).toMatch(`<div id="foo"></div>`)
  113. // should update even though it's not reactive
  114. toggle.value = false
  115. await nextTick()
  116. expect(serializeInner(root)).toMatch(`<div class="baz"></div>`)
  117. })
  118. // #4161
  119. it('context.attrs in child component slots', async () => {
  120. const toggle = ref(true)
  121. const Parent = {
  122. render: () => h(Child, toggle.value ? { id: 'foo' } : { class: 'baz' })
  123. }
  124. const Wrapper = {
  125. render(this: any) {
  126. return this.$slots.default()
  127. }
  128. }
  129. const Child = {
  130. inheritAttrs: false,
  131. setup(_: any, { attrs }: any) {
  132. return () => {
  133. const vnode = h(Wrapper, null, {
  134. default: () => [h('div', attrs)],
  135. _: 1 // mark stable slots
  136. })
  137. vnode.dynamicChildren = [] // force optimized mode
  138. return vnode
  139. }
  140. }
  141. }
  142. const root = nodeOps.createElement('div')
  143. render(h(Parent), root)
  144. expect(serializeInner(root)).toMatch(`<div id="foo"></div>`)
  145. // should update even though it's not reactive
  146. toggle.value = false
  147. await nextTick()
  148. expect(serializeInner(root)).toMatch(`<div class="baz"></div>`)
  149. })
  150. it('context.slots', async () => {
  151. const id = ref('foo')
  152. const Parent = {
  153. render: () =>
  154. h(Child, null, {
  155. foo: () => id.value,
  156. bar: () => 'bar'
  157. })
  158. }
  159. const Child = {
  160. setup(props: any, { slots }: any) {
  161. return () => h('div', [...slots.foo(), ...slots.bar()])
  162. }
  163. }
  164. const root = nodeOps.createElement('div')
  165. render(h(Parent), root)
  166. expect(serializeInner(root)).toMatch(`<div>foobar</div>`)
  167. // should update even though it's not reactive
  168. id.value = 'baz'
  169. await nextTick()
  170. expect(serializeInner(root)).toMatch(`<div>bazbar</div>`)
  171. })
  172. it('context.emit', async () => {
  173. const count = ref(0)
  174. const spy = jest.fn()
  175. const Parent = {
  176. render: () =>
  177. h(Child, {
  178. count: count.value,
  179. onInc: (newVal: number) => {
  180. spy()
  181. count.value = newVal
  182. }
  183. })
  184. }
  185. const Child = defineComponent({
  186. props: {
  187. count: {
  188. type: Number,
  189. default: 1
  190. }
  191. },
  192. setup(props, { emit }) {
  193. return () =>
  194. h(
  195. 'div',
  196. {
  197. onClick: () => emit('inc', props.count + 1)
  198. },
  199. props.count
  200. )
  201. }
  202. })
  203. const root = nodeOps.createElement('div')
  204. render(h(Parent), root)
  205. expect(serializeInner(root)).toMatch(`<div>0</div>`)
  206. // emit should trigger parent handler
  207. triggerEvent(root.children[0] as TestElement, 'click')
  208. expect(spy).toHaveBeenCalled()
  209. await nextTick()
  210. expect(serializeInner(root)).toMatch(`<div>1</div>`)
  211. })
  212. })