apiSetupContext.spec.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import { ref, reactive } from '@vue/reactivity'
  2. import {
  3. renderToString,
  4. h,
  5. nodeOps,
  6. render,
  7. serializeInner,
  8. nextTick,
  9. watch,
  10. createComponent,
  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 = createComponent({
  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 = createComponent({
  51. setup(props: { count: number }) {
  52. watch(() => {
  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('setup props should resolve the correct types from props object', async () => {
  69. const count = ref(0)
  70. let dummy
  71. const Parent = {
  72. render: () => h(Child, { count: count.value })
  73. }
  74. const Child = createComponent({
  75. props: {
  76. count: Number
  77. },
  78. setup(props) {
  79. watch(() => {
  80. dummy = props.count
  81. })
  82. return () => h('div', props.count)
  83. }
  84. })
  85. const root = nodeOps.createElement('div')
  86. render(h(Parent), root)
  87. expect(serializeInner(root)).toMatch(`<div>0</div>`)
  88. expect(dummy).toBe(0)
  89. // props should be reactive
  90. count.value++
  91. await nextTick()
  92. expect(serializeInner(root)).toMatch(`<div>1</div>`)
  93. expect(dummy).toBe(1)
  94. })
  95. it('context.attrs', async () => {
  96. const toggle = ref(true)
  97. const Parent = {
  98. render: () => h(Child, toggle.value ? { id: 'foo' } : { class: 'baz' })
  99. }
  100. const Child = {
  101. // explicit empty props declaration
  102. // puts everything received in attrs
  103. // disable implicit fallthrough
  104. inheritAttrs: false,
  105. props: {},
  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. it('context.slots', async () => {
  119. const id = ref('foo')
  120. const Parent = {
  121. render: () =>
  122. h(Child, null, {
  123. foo: () => id.value,
  124. bar: () => 'bar'
  125. })
  126. }
  127. const Child = {
  128. setup(props: any, { slots }: any) {
  129. return () => h('div', [...slots.foo(), ...slots.bar()])
  130. }
  131. }
  132. const root = nodeOps.createElement('div')
  133. render(h(Parent), root)
  134. expect(serializeInner(root)).toMatch(`<div>foobar</div>`)
  135. // should update even though it's not reactive
  136. id.value = 'baz'
  137. await nextTick()
  138. expect(serializeInner(root)).toMatch(`<div>bazbar</div>`)
  139. })
  140. it('context.emit', async () => {
  141. const count = ref(0)
  142. const spy = jest.fn()
  143. const Parent = {
  144. render: () =>
  145. h(Child, {
  146. count: count.value,
  147. onInc: (newVal: number) => {
  148. spy()
  149. count.value = newVal
  150. }
  151. })
  152. }
  153. const Child = createComponent({
  154. props: {
  155. count: {
  156. type: Number,
  157. default: 1
  158. }
  159. },
  160. setup(props, { emit }) {
  161. return () =>
  162. h(
  163. 'div',
  164. {
  165. onClick: () => emit('inc', props.count + 1)
  166. },
  167. props.count
  168. )
  169. }
  170. })
  171. const root = nodeOps.createElement('div')
  172. render(h(Parent), root)
  173. expect(serializeInner(root)).toMatch(`<div>0</div>`)
  174. // emit should trigger parent handler
  175. triggerEvent(root.children[0] as TestElement, 'click')
  176. expect(spy).toHaveBeenCalled()
  177. await nextTick()
  178. expect(serializeInner(root)).toMatch(`<div>1</div>`)
  179. })
  180. })