create-element.spec.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import Vue from 'vue'
  2. import { renderState } from 'core/instance/render'
  3. import { createElement } from 'core/vdom/create-element'
  4. import { emptyVNode } from 'core/vdom/vnode'
  5. import { bind } from 'shared/util'
  6. describe('create-element', () => {
  7. afterEach(() => {
  8. renderState.activeInstance = null
  9. })
  10. it('render vnode with basic reserved tag using createElement', () => {
  11. const vm = new Vue({
  12. data: { msg: 'hello world' }
  13. })
  14. const h = bind(createElement, vm)
  15. renderState.activeInstance = vm
  16. const vnode = h('p', {})
  17. expect(vnode.tag).toBe('p')
  18. expect(vnode.data).toEqual({})
  19. expect(vnode.children).toBeUndefined()
  20. expect(vnode.text).toBeUndefined()
  21. expect(vnode.elm).toBeUndefined()
  22. expect(vnode.ns).toBeUndefined()
  23. expect(vnode.context).toEqual(vm)
  24. })
  25. it('render vnode with component using createElement', () => {
  26. const vm = new Vue({
  27. data: { message: 'hello world' },
  28. components: {
  29. 'my-component': {
  30. props: ['msg']
  31. }
  32. }
  33. })
  34. const h = bind(createElement, vm)
  35. renderState.activeInstance = vm
  36. const vnode = h('my-component', { props: { msg: vm.message }})
  37. expect(vnode.tag).toMatch(/vue-component-[0-9]+/)
  38. expect(vnode.componentOptions.propsData).toEqual({ msg: vm.message })
  39. expect(vnode.children).toBeUndefined()
  40. expect(vnode.text).toBeUndefined()
  41. expect(vnode.elm).toBeUndefined()
  42. expect(vnode.ns).toBeUndefined()
  43. expect(vnode.context).toEqual(vm)
  44. })
  45. it('render vnode with custom tag using createElement', () => {
  46. const vm = new Vue({
  47. data: { msg: 'hello world' }
  48. })
  49. const h = bind(createElement, vm)
  50. const tag = 'custom-tag'
  51. renderState.activeInstance = vm
  52. const vnode = h(tag, {})
  53. expect(vnode.tag).toBe('custom-tag')
  54. expect(vnode.data).toEqual({})
  55. expect(vnode.children).toBeUndefined()
  56. expect(vnode.text).toBeUndefined()
  57. expect(vnode.elm).toBeUndefined()
  58. expect(vnode.ns).toBeUndefined()
  59. expect(vnode.context).toEqual(vm)
  60. expect(vnode.componentOptions).toBeUndefined()
  61. expect(`Unknown custom element: <${tag}> - did you`).toHaveBeenWarned()
  62. })
  63. it('render empty vnode with falsy tag using createElement', () => {
  64. const vm = new Vue({
  65. data: { msg: 'hello world' }
  66. })
  67. const h = bind(createElement, vm)
  68. renderState.activeInstance = vm
  69. const vnode = h(null, {})
  70. expect(vnode).toEqual(emptyVNode())
  71. })
  72. it('render vnode with not string tag using createElement', () => {
  73. const vm = new Vue({
  74. data: { msg: 'hello world' }
  75. })
  76. const h = bind(createElement, vm)
  77. renderState.activeInstance = vm
  78. const vnode = h(Vue.extend({ // Component class
  79. props: ['msg']
  80. }), { props: { msg: vm.message }})
  81. expect(vnode.tag).toMatch(/vue-component-[0-9]+/)
  82. expect(vnode.componentOptions.propsData).toEqual({ msg: vm.message })
  83. expect(vnode.children).toBeUndefined()
  84. expect(vnode.text).toBeUndefined()
  85. expect(vnode.elm).toBeUndefined()
  86. expect(vnode.ns).toBeUndefined()
  87. expect(vnode.context).toEqual(vm)
  88. })
  89. it('warn message that createElement cannot called when using createElement', () => {
  90. const vm = new Vue({
  91. data: { msg: 'hello world' }
  92. })
  93. const h = bind(createElement, vm)
  94. h('p', {})
  95. expect('createElement cannot be called outside of component').toHaveBeenWarned()
  96. })
  97. it('render vnode with createElement with children', () => {
  98. const vm = new Vue({})
  99. const h = bind(createElement, vm)
  100. renderState.activeInstance = vm
  101. const vnode = h('p', void 0, [h('br'), 'hello world', h('br')])
  102. expect(vnode.children[0].tag).toBe('br')
  103. expect(vnode.children[1].text).toBe('hello world')
  104. expect(vnode.children[2].tag).toBe('br')
  105. })
  106. it('render vnode with children, omitting data', () => {
  107. const vm = new Vue({})
  108. const h = bind(createElement, vm)
  109. renderState.activeInstance = vm
  110. const vnode = h('p', [h('br'), 'hello world', h('br')])
  111. expect(vnode.children[0].tag).toBe('br')
  112. expect(vnode.children[1].text).toBe('hello world')
  113. expect(vnode.children[2].tag).toBe('br')
  114. })
  115. it('warn message when using non-thunk children for component', () => {
  116. const vm = new Vue({
  117. data: { message: 'hello world' },
  118. components: {
  119. 'my-component': {
  120. props: ['msg']
  121. }
  122. }
  123. })
  124. const h = bind(createElement, vm)
  125. renderState.activeInstance = vm
  126. const vnode = h('my-component', { props: { msg: vm.message }}, [h('br'), 'hello world', h('br')])
  127. expect(vnode.componentOptions.children[0].tag).toBe('br')
  128. expect(vnode.componentOptions.children[1]).toBe('hello world')
  129. expect(vnode.componentOptions.children[2].tag).toBe('br')
  130. expect('A component\'s children should be a function').toHaveBeenWarned()
  131. })
  132. it('render svg elements with correct namespace', () => {
  133. const vm = new Vue({})
  134. const h = bind(createElement, vm)
  135. renderState.activeInstance = vm
  136. const vnode = h('svg', [h('a')])
  137. expect(vnode.ns).toBe('svg')
  138. // should apply ns to children
  139. expect(vnode.children[0].ns).toBe('svg')
  140. })
  141. it('render MathML elements with correct namespace', () => {
  142. const vm = new Vue({})
  143. const h = bind(createElement, vm)
  144. renderState.activeInstance = vm
  145. const vnode = h('math', [h('matrix')])
  146. expect(vnode.ns).toBe('math')
  147. // should apply ns to children
  148. expect(vnode.children[0].ns).toBe('math')
  149. // although not explicitly listed, elements nested under <math>
  150. // should not be treated as component
  151. expect(vnode.children[0].componentOptions).toBeUndefined()
  152. })
  153. })