create-element.spec.js 5.3 KB

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