apiSetupContext.spec.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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('context.attrs', async () => {
  69. const toggle = ref(true)
  70. const Parent = {
  71. render: () => h(Child, toggle.value ? { id: 'foo' } : { class: 'baz' })
  72. }
  73. const Child = {
  74. // explicit empty props declaration
  75. // puts everything received in attrs
  76. props: {},
  77. setup(props: any, { attrs }: any) {
  78. return () => h('div', attrs)
  79. }
  80. }
  81. const root = nodeOps.createElement('div')
  82. render(h(Parent), root)
  83. expect(serializeInner(root)).toMatch(`<div id="foo"></div>`)
  84. // should update even though it's not reactive
  85. toggle.value = false
  86. await nextTick()
  87. expect(serializeInner(root)).toMatch(`<div class="baz"></div>`)
  88. })
  89. it('context.slots', async () => {
  90. const id = ref('foo')
  91. const Parent = {
  92. render: () =>
  93. h(Child, null, {
  94. foo: () => id.value,
  95. bar: () => 'bar'
  96. })
  97. }
  98. const Child = {
  99. setup(props: any, { slots }: any) {
  100. return () => h('div', [...slots.foo(), ...slots.bar()])
  101. }
  102. }
  103. const root = nodeOps.createElement('div')
  104. render(h(Parent), root)
  105. expect(serializeInner(root)).toMatch(`<div>foobar</div>`)
  106. // should update even though it's not reactive
  107. id.value = 'baz'
  108. await nextTick()
  109. expect(serializeInner(root)).toMatch(`<div>bazbar</div>`)
  110. })
  111. it('context.emit', async () => {
  112. const count = ref(0)
  113. const spy = jest.fn()
  114. const Parent = {
  115. render: () =>
  116. h(Child, {
  117. count: count.value,
  118. onInc: (newVal: number) => {
  119. spy()
  120. count.value = newVal
  121. }
  122. })
  123. }
  124. const Child = createComponent({
  125. props: {
  126. count: {
  127. type: Number,
  128. default: 1
  129. }
  130. },
  131. setup(props, { emit }) {
  132. return () =>
  133. h(
  134. 'div',
  135. {
  136. onClick: () => emit('inc', props.count + 1)
  137. },
  138. props.count
  139. )
  140. }
  141. })
  142. const root = nodeOps.createElement('div')
  143. render(h(Parent), root)
  144. expect(serializeInner(root)).toMatch(`<div>0</div>`)
  145. // emit should trigger parent handler
  146. triggerEvent(root.children[0] as TestElement, 'click')
  147. expect(spy).toHaveBeenCalled()
  148. await nextTick()
  149. expect(serializeInner(root)).toMatch(`<div>1</div>`)
  150. })
  151. })