apiSetup.spec.ts 5.5 KB

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