apiSetupContext.spec.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. props: {},
  107. setup(props: any, { attrs }: any) {
  108. return () => h('div', attrs)
  109. }
  110. }
  111. const root = nodeOps.createElement('div')
  112. render(h(Parent), root)
  113. expect(serializeInner(root)).toMatch(`<div id="foo"></div>`)
  114. // should update even though it's not reactive
  115. toggle.value = false
  116. await nextTick()
  117. expect(serializeInner(root)).toMatch(`<div class="baz"></div>`)
  118. })
  119. it('context.slots', async () => {
  120. const id = ref('foo')
  121. const Parent = {
  122. render: () =>
  123. h(Child, null, {
  124. foo: () => id.value,
  125. bar: () => 'bar'
  126. })
  127. }
  128. const Child = {
  129. setup(props: any, { slots }: any) {
  130. return () => h('div', [...slots.foo(), ...slots.bar()])
  131. }
  132. }
  133. const root = nodeOps.createElement('div')
  134. render(h(Parent), root)
  135. expect(serializeInner(root)).toMatch(`<div>foobar</div>`)
  136. // should update even though it's not reactive
  137. id.value = 'baz'
  138. await nextTick()
  139. expect(serializeInner(root)).toMatch(`<div>bazbar</div>`)
  140. })
  141. it('context.emit', async () => {
  142. const count = ref(0)
  143. const spy = jest.fn()
  144. const Parent = {
  145. render: () =>
  146. h(Child, {
  147. count: count.value,
  148. onInc: (newVal: number) => {
  149. spy()
  150. count.value = newVal
  151. }
  152. })
  153. }
  154. const Child = defineComponent({
  155. props: {
  156. count: {
  157. type: Number,
  158. default: 1
  159. }
  160. },
  161. setup(props, { emit }) {
  162. return () =>
  163. h(
  164. 'div',
  165. {
  166. onClick: () => emit('inc', props.count + 1)
  167. },
  168. props.count
  169. )
  170. }
  171. })
  172. const root = nodeOps.createElement('div')
  173. render(h(Parent), root)
  174. expect(serializeInner(root)).toMatch(`<div>0</div>`)
  175. // emit should trigger parent handler
  176. triggerEvent(root.children[0] as TestElement, 'click')
  177. expect(spy).toHaveBeenCalled()
  178. await nextTick()
  179. expect(serializeInner(root)).toMatch(`<div>1</div>`)
  180. })
  181. })