apiTemplateRef.spec.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { ref, nodeOps, h, render, nextTick, Ref } from '@vue/runtime-test'
  2. // reference: https://vue-composition-api-rfc.netlify.com/api.html#template-refs
  3. describe('api: template refs', () => {
  4. it('string ref mount', () => {
  5. const root = nodeOps.createElement('div')
  6. const el = ref(null)
  7. const Comp = {
  8. setup() {
  9. return {
  10. refKey: el
  11. }
  12. },
  13. render() {
  14. return h('div', { ref: 'refKey' })
  15. }
  16. }
  17. render(h(Comp), root)
  18. expect(el.value).toBe(root.children[0])
  19. })
  20. it('string ref update', async () => {
  21. const root = nodeOps.createElement('div')
  22. const fooEl = ref(null)
  23. const barEl = ref(null)
  24. const refKey = ref('foo')
  25. const Comp = {
  26. setup() {
  27. return {
  28. foo: fooEl,
  29. bar: barEl
  30. }
  31. },
  32. render() {
  33. return h('div', { ref: refKey.value })
  34. }
  35. }
  36. render(h(Comp), root)
  37. expect(fooEl.value).toBe(root.children[0])
  38. expect(barEl.value).toBe(null)
  39. refKey.value = 'bar'
  40. await nextTick()
  41. expect(fooEl.value).toBe(null)
  42. expect(barEl.value).toBe(root.children[0])
  43. })
  44. it('string ref unmount', async () => {
  45. const root = nodeOps.createElement('div')
  46. const el = ref(null)
  47. const toggle = ref(true)
  48. const Comp = {
  49. setup() {
  50. return {
  51. refKey: el
  52. }
  53. },
  54. render() {
  55. return toggle.value ? h('div', { ref: 'refKey' }) : null
  56. }
  57. }
  58. render(h(Comp), root)
  59. expect(el.value).toBe(root.children[0])
  60. toggle.value = false
  61. await nextTick()
  62. expect(el.value).toBe(null)
  63. })
  64. it('render function ref mount', () => {
  65. const root = nodeOps.createElement('div')
  66. const el = ref(null)
  67. const Comp = {
  68. setup() {
  69. return () => h('div', { ref: el })
  70. }
  71. }
  72. render(h(Comp), root)
  73. expect(el.value).toBe(root.children[0])
  74. })
  75. it('render function ref update', async () => {
  76. const root = nodeOps.createElement('div')
  77. const refs = {
  78. foo: ref(null),
  79. bar: ref(null)
  80. }
  81. const refKey: Ref<keyof typeof refs> = ref('foo')
  82. const Comp = {
  83. setup() {
  84. return () => h('div', { ref: refs[refKey.value] })
  85. }
  86. }
  87. render(h(Comp), root)
  88. expect(refs.foo.value).toBe(root.children[0])
  89. expect(refs.bar.value).toBe(null)
  90. refKey.value = 'bar'
  91. await nextTick()
  92. expect(refs.foo.value).toBe(null)
  93. expect(refs.bar.value).toBe(root.children[0])
  94. })
  95. it('render function ref unmount', async () => {
  96. const root = nodeOps.createElement('div')
  97. const el = ref(null)
  98. const toggle = ref(true)
  99. const Comp = {
  100. setup() {
  101. return () => (toggle.value ? h('div', { ref: el }) : null)
  102. }
  103. }
  104. render(h(Comp), root)
  105. expect(el.value).toBe(root.children[0])
  106. toggle.value = false
  107. await nextTick()
  108. expect(el.value).toBe(null)
  109. })
  110. })