create-element.spec.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import Vue from 'vue'
  2. import { createElement } from 'core/vdom/create-element'
  3. import { emptyVNode } from 'core/vdom/vnode'
  4. import { bind } from 'shared/util'
  5. describe('create-element', () => {
  6. it('render vnode with basic reserved tag using createElement', () => {
  7. const vm = new Vue({
  8. data: { msg: 'hello world' }
  9. })
  10. const h = bind(createElement, vm)
  11. const vnode = h('p', {})
  12. expect(vnode.tag).toBe('p')
  13. expect(vnode.data).toEqual({})
  14. expect(vnode.children).toBeUndefined()
  15. expect(vnode.text).toBeUndefined()
  16. expect(vnode.elm).toBeUndefined()
  17. expect(vnode.ns).toBeUndefined()
  18. expect(vnode.context).toEqual(vm)
  19. })
  20. it('render vnode with component using createElement', () => {
  21. const vm = new Vue({
  22. data: { message: 'hello world' },
  23. components: {
  24. 'my-component': {
  25. props: ['msg']
  26. }
  27. }
  28. })
  29. const h = bind(createElement, vm)
  30. const vnode = h('my-component', { props: { msg: vm.message }})
  31. expect(vnode.tag).toMatch(/vue-component-[0-9]+/)
  32. expect(vnode.componentOptions.propsData).toEqual({ msg: vm.message })
  33. expect(vnode.children).toBeUndefined()
  34. expect(vnode.text).toBeUndefined()
  35. expect(vnode.elm).toBeUndefined()
  36. expect(vnode.ns).toBeUndefined()
  37. expect(vnode.context).toEqual(vm)
  38. })
  39. it('render vnode with custom tag using createElement', () => {
  40. const vm = new Vue({
  41. data: { msg: 'hello world' }
  42. })
  43. const h = bind(createElement, vm)
  44. const tag = 'custom-tag'
  45. const vnode = h(tag, {})
  46. expect(vnode.tag).toBe('custom-tag')
  47. expect(vnode.data).toEqual({})
  48. expect(vnode.children).toBeUndefined()
  49. expect(vnode.text).toBeUndefined()
  50. expect(vnode.elm).toBeUndefined()
  51. expect(vnode.ns).toBeUndefined()
  52. expect(vnode.context).toEqual(vm)
  53. expect(vnode.componentOptions).toBeUndefined()
  54. })
  55. it('render empty vnode with falsy tag using createElement', () => {
  56. const vm = new Vue({
  57. data: { msg: 'hello world' }
  58. })
  59. const h = bind(createElement, vm)
  60. const vnode = h(null, {})
  61. expect(vnode).toEqual(emptyVNode())
  62. })
  63. it('render vnode with not string tag using createElement', () => {
  64. const vm = new Vue({
  65. data: { msg: 'hello world' }
  66. })
  67. const h = bind(createElement, vm)
  68. const vnode = h(Vue.extend({ // Component class
  69. props: ['msg']
  70. }), { props: { msg: vm.message }})
  71. expect(vnode.tag).toMatch(/vue-component-[0-9]+/)
  72. expect(vnode.componentOptions.propsData).toEqual({ msg: vm.message })
  73. expect(vnode.children).toBeUndefined()
  74. expect(vnode.text).toBeUndefined()
  75. expect(vnode.elm).toBeUndefined()
  76. expect(vnode.ns).toBeUndefined()
  77. expect(vnode.context).toEqual(vm)
  78. })
  79. it('render vnode with createElement with children', () => {
  80. const vm = new Vue({})
  81. const h = bind(createElement, vm)
  82. const vnode = h('p', void 0, [h('br'), 'hello world', h('br')])
  83. expect(vnode.children[0].tag).toBe('br')
  84. expect(vnode.children[1].text).toBe('hello world')
  85. expect(vnode.children[2].tag).toBe('br')
  86. })
  87. it('render vnode with children, omitting data', () => {
  88. const vm = new Vue({})
  89. const h = bind(createElement, vm)
  90. const vnode = h('p', [h('br'), 'hello world', h('br')])
  91. expect(vnode.children[0].tag).toBe('br')
  92. expect(vnode.children[1].text).toBe('hello world')
  93. expect(vnode.children[2].tag).toBe('br')
  94. })
  95. it('render svg elements with correct namespace', () => {
  96. const vm = new Vue({})
  97. const h = bind(createElement, vm)
  98. const vnode = h('svg', [h('a', [h('foo', [h('bar')])])])
  99. expect(vnode.ns).toBe('svg')
  100. // should apply ns to children recursively
  101. expect(vnode.children[0].ns).toBe('svg')
  102. expect(vnode.children[0].children[0].ns).toBe('svg')
  103. expect(vnode.children[0].children[0].children[0].ns).toBe('svg')
  104. })
  105. it('render MathML elements with correct namespace', () => {
  106. const vm = new Vue({})
  107. const h = bind(createElement, vm)
  108. const vnode = h('math', [h('matrix')])
  109. expect(vnode.ns).toBe('math')
  110. // should apply ns to children
  111. expect(vnode.children[0].ns).toBe('math')
  112. // although not explicitly listed, elements nested under <math>
  113. // should not be treated as component
  114. expect(vnode.children[0].componentOptions).toBeUndefined()
  115. })
  116. it('warn observed data objects', () => {
  117. new Vue({
  118. data: {
  119. data: {}
  120. },
  121. render (h) {
  122. return h('div', this.data)
  123. }
  124. }).$mount()
  125. expect('Avoid using observed data object as vnode data').toHaveBeenWarned()
  126. })
  127. })