componentAttrs.spec.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import {
  2. createComponent,
  3. getCurrentInstance,
  4. nextTick,
  5. ref,
  6. setInheritAttrs,
  7. setText,
  8. template,
  9. watchEffect,
  10. } from '../src'
  11. import { makeRender } from './_utils'
  12. const define = makeRender<any>()
  13. describe.todo('attribute fallthrough', () => {
  14. it('should allow attrs to fallthrough', async () => {
  15. const t0 = template('<div>')
  16. const { component: Child } = define({
  17. props: ['foo'],
  18. render() {
  19. const instance = getCurrentInstance()!
  20. const n0 = t0() as Element
  21. setInheritAttrs()
  22. watchEffect(() => setText(n0, instance.props.foo))
  23. return n0
  24. },
  25. })
  26. const foo = ref(1)
  27. const id = ref('a')
  28. const { host } = define({
  29. setup() {
  30. return { foo, id }
  31. },
  32. render(_ctx: Record<string, any>) {
  33. return createComponent(
  34. Child,
  35. [
  36. {
  37. foo: () => _ctx.foo,
  38. id: () => _ctx.id,
  39. },
  40. ],
  41. null,
  42. true,
  43. )
  44. },
  45. }).render()
  46. expect(host.innerHTML).toBe('<div id="a">1</div>')
  47. foo.value++
  48. await nextTick()
  49. expect(host.innerHTML).toBe('<div id="a">2</div>')
  50. id.value = 'b'
  51. await nextTick()
  52. expect(host.innerHTML).toBe('<div id="b">2</div>')
  53. })
  54. it('should not fallthrough if explicitly pass inheritAttrs: false', async () => {
  55. const t0 = template('<div>')
  56. const { component: Child } = define({
  57. props: ['foo'],
  58. inheritAttrs: false,
  59. render() {
  60. const instance = getCurrentInstance()!
  61. const n0 = t0() as Element
  62. setInheritAttrs()
  63. watchEffect(() => setText(n0, instance.props.foo))
  64. return n0
  65. },
  66. })
  67. const foo = ref(1)
  68. const id = ref('a')
  69. const { host } = define({
  70. setup() {
  71. return { foo, id }
  72. },
  73. render(_ctx: Record<string, any>) {
  74. return createComponent(
  75. Child,
  76. [
  77. {
  78. foo: () => _ctx.foo,
  79. id: () => _ctx.id,
  80. },
  81. ],
  82. null,
  83. true,
  84. )
  85. },
  86. }).render()
  87. expect(host.innerHTML).toBe('<div>1</div>')
  88. foo.value++
  89. await nextTick()
  90. expect(host.innerHTML).toBe('<div>2</div>')
  91. id.value = 'b'
  92. await nextTick()
  93. expect(host.innerHTML).toBe('<div>2</div>')
  94. })
  95. it('should pass through attrs in nested single root components', async () => {
  96. const t0 = template('<div>')
  97. const { component: Grandson } = define({
  98. props: ['custom-attr'],
  99. render() {
  100. const instance = getCurrentInstance()!
  101. const n0 = t0() as Element
  102. setInheritAttrs()
  103. watchEffect(() => setText(n0, instance.attrs.foo))
  104. return n0
  105. },
  106. })
  107. const { component: Child } = define({
  108. render() {
  109. const n0 = createComponent(
  110. Grandson,
  111. [
  112. {
  113. 'custom-attr': () => 'custom-attr',
  114. },
  115. ],
  116. null,
  117. true,
  118. )
  119. return n0
  120. },
  121. })
  122. const foo = ref(1)
  123. const id = ref('a')
  124. const { host } = define({
  125. setup() {
  126. return { foo, id }
  127. },
  128. render(_ctx: Record<string, any>) {
  129. return createComponent(
  130. Child,
  131. [
  132. {
  133. foo: () => _ctx.foo,
  134. id: () => _ctx.id,
  135. },
  136. ],
  137. null,
  138. true,
  139. )
  140. },
  141. }).render()
  142. expect(host.innerHTML).toBe('<div foo="1" id="a">1</div>')
  143. foo.value++
  144. await nextTick()
  145. expect(host.innerHTML).toBe('<div foo="2" id="a">2</div>')
  146. id.value = 'b'
  147. await nextTick()
  148. expect(host.innerHTML).toBe('<div foo="2" id="b">2</div>')
  149. })
  150. })