apiTemplateRef.spec.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import {
  2. ref,
  3. nodeOps,
  4. h,
  5. render,
  6. nextTick,
  7. Ref,
  8. createComponent
  9. } from '@vue/runtime-test'
  10. // reference: https://vue-composition-api-rfc.netlify.com/api.html#template-refs
  11. describe('api: template refs', () => {
  12. it('string ref mount', () => {
  13. const root = nodeOps.createElement('div')
  14. const el = ref(null)
  15. const Comp = {
  16. setup() {
  17. return {
  18. refKey: el
  19. }
  20. },
  21. render() {
  22. return h('div', { ref: 'refKey' })
  23. }
  24. }
  25. render(h(Comp), root)
  26. expect(el.value).toBe(root.children[0])
  27. })
  28. it('string ref update', async () => {
  29. const root = nodeOps.createElement('div')
  30. const fooEl = ref(null)
  31. const barEl = ref(null)
  32. const refKey = ref('foo')
  33. const Comp = {
  34. setup() {
  35. return {
  36. foo: fooEl,
  37. bar: barEl
  38. }
  39. },
  40. render() {
  41. return h('div', { ref: refKey.value })
  42. }
  43. }
  44. render(h(Comp), root)
  45. expect(fooEl.value).toBe(root.children[0])
  46. expect(barEl.value).toBe(null)
  47. refKey.value = 'bar'
  48. await nextTick()
  49. expect(fooEl.value).toBe(null)
  50. expect(barEl.value).toBe(root.children[0])
  51. })
  52. it('string ref unmount', async () => {
  53. const root = nodeOps.createElement('div')
  54. const el = ref(null)
  55. const toggle = ref(true)
  56. const Comp = {
  57. setup() {
  58. return {
  59. refKey: el
  60. }
  61. },
  62. render() {
  63. return toggle.value ? h('div', { ref: 'refKey' }) : null
  64. }
  65. }
  66. render(h(Comp), root)
  67. expect(el.value).toBe(root.children[0])
  68. toggle.value = false
  69. await nextTick()
  70. expect(el.value).toBe(null)
  71. })
  72. it('function ref mount', () => {
  73. const root = nodeOps.createElement('div')
  74. const fn = jest.fn()
  75. const Comp = createComponent(() => () => h('div', { ref: fn }))
  76. render(h(Comp), root)
  77. expect(fn.mock.calls[0][0]).toBe(root.children[0])
  78. })
  79. it('function ref update', async () => {
  80. const root = nodeOps.createElement('div')
  81. const fn1 = jest.fn()
  82. const fn2 = jest.fn()
  83. const fn = ref(fn1)
  84. const Comp = createComponent(() => () => h('div', { ref: fn.value }))
  85. render(h(Comp), root)
  86. expect(fn1.mock.calls).toHaveLength(1)
  87. expect(fn1.mock.calls[0][0]).toBe(root.children[0])
  88. expect(fn2.mock.calls).toHaveLength(0)
  89. fn.value = fn2
  90. await nextTick()
  91. expect(fn1.mock.calls).toHaveLength(1)
  92. expect(fn2.mock.calls).toHaveLength(1)
  93. expect(fn2.mock.calls[0][0]).toBe(root.children[0])
  94. })
  95. it('function ref unmount', async () => {
  96. const root = nodeOps.createElement('div')
  97. const fn = jest.fn()
  98. const toggle = ref(true)
  99. const Comp = createComponent(() => () =>
  100. toggle.value ? h('div', { ref: fn }) : null
  101. )
  102. render(h(Comp), root)
  103. expect(fn.mock.calls[0][0]).toBe(root.children[0])
  104. toggle.value = false
  105. await nextTick()
  106. expect(fn.mock.calls[1][0]).toBe(null)
  107. })
  108. it('render function ref mount', () => {
  109. const root = nodeOps.createElement('div')
  110. const el = ref(null)
  111. const Comp = {
  112. setup() {
  113. return () => h('div', { ref: el })
  114. }
  115. }
  116. render(h(Comp), root)
  117. expect(el.value).toBe(root.children[0])
  118. })
  119. it('render function ref update', async () => {
  120. const root = nodeOps.createElement('div')
  121. const refs = {
  122. foo: ref(null),
  123. bar: ref(null)
  124. }
  125. const refKey: Ref<keyof typeof refs> = ref('foo')
  126. const Comp = {
  127. setup() {
  128. return () => h('div', { ref: refs[refKey.value] })
  129. }
  130. }
  131. render(h(Comp), root)
  132. expect(refs.foo.value).toBe(root.children[0])
  133. expect(refs.bar.value).toBe(null)
  134. refKey.value = 'bar'
  135. await nextTick()
  136. expect(refs.foo.value).toBe(null)
  137. expect(refs.bar.value).toBe(root.children[0])
  138. })
  139. it('render function ref unmount', async () => {
  140. const root = nodeOps.createElement('div')
  141. const el = ref(null)
  142. const toggle = ref(true)
  143. const Comp = {
  144. setup() {
  145. return () => (toggle.value ? h('div', { ref: el }) : null)
  146. }
  147. }
  148. render(h(Comp), root)
  149. expect(el.value).toBe(root.children[0])
  150. toggle.value = false
  151. await nextTick()
  152. expect(el.value).toBe(null)
  153. })
  154. })