create-element.spec.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import Vue from 'vue'
  2. import { createEmptyVNode } from 'core/vdom/vnode'
  3. describe('create-element', () => {
  4. it('render vnode with basic reserved tag using createElement', () => {
  5. const vm = new Vue({
  6. data: { msg: 'hello world' }
  7. })
  8. const h = vm.$createElement
  9. const vnode = h('p', {})
  10. expect(vnode.tag).toBe('p')
  11. expect(vnode.data).toEqual({})
  12. expect(vnode.children).toBeUndefined()
  13. expect(vnode.text).toBeUndefined()
  14. expect(vnode.elm).toBeUndefined()
  15. expect(vnode.ns).toBeUndefined()
  16. expect(vnode.context).toEqual(vm)
  17. })
  18. it('render vnode with component using createElement', () => {
  19. const vm = new Vue({
  20. data: { message: 'hello world' },
  21. components: {
  22. 'my-component': {
  23. props: ['msg']
  24. }
  25. }
  26. })
  27. const h = vm.$createElement
  28. const vnode = h('my-component', { props: { msg: vm.message } })
  29. expect(vnode.tag).toMatch(/vue-component-[0-9]+/)
  30. expect(vnode.componentOptions.propsData).toEqual({ msg: vm.message })
  31. expect(vnode.children).toBeUndefined()
  32. expect(vnode.text).toBeUndefined()
  33. expect(vnode.elm).toBeUndefined()
  34. expect(vnode.ns).toBeUndefined()
  35. expect(vnode.context).toEqual(vm)
  36. })
  37. it('render vnode with custom tag using createElement', () => {
  38. const vm = new Vue({
  39. data: { msg: 'hello world' }
  40. })
  41. const h = vm.$createElement
  42. const tag = 'custom-tag'
  43. const vnode = h(tag, {})
  44. expect(vnode.tag).toBe('custom-tag')
  45. expect(vnode.data).toEqual({})
  46. expect(vnode.children).toBeUndefined()
  47. expect(vnode.text).toBeUndefined()
  48. expect(vnode.elm).toBeUndefined()
  49. expect(vnode.ns).toBeUndefined()
  50. expect(vnode.context).toEqual(vm)
  51. expect(vnode.componentOptions).toBeUndefined()
  52. })
  53. it('render empty vnode with falsy tag using createElement', () => {
  54. const vm = new Vue({
  55. data: { msg: 'hello world' }
  56. })
  57. const h = vm.$createElement
  58. const vnode = h(null, {})
  59. expect(vnode).toEqual(createEmptyVNode())
  60. })
  61. it('render vnode with not string tag using createElement', () => {
  62. const vm = new Vue({
  63. data: { msg: 'hello world' }
  64. })
  65. const h = vm.$createElement
  66. const vnode = h(
  67. Vue.extend({
  68. // Component class
  69. props: ['msg']
  70. }),
  71. { props: { msg: vm.message } }
  72. )
  73. expect(vnode.tag).toMatch(/vue-component-[0-9]+/)
  74. expect(vnode.componentOptions.propsData).toEqual({ msg: vm.message })
  75. expect(vnode.children).toBeUndefined()
  76. expect(vnode.text).toBeUndefined()
  77. expect(vnode.elm).toBeUndefined()
  78. expect(vnode.ns).toBeUndefined()
  79. expect(vnode.context).toEqual(vm)
  80. })
  81. it('render vnode with createElement with children', () => {
  82. const vm = new Vue({})
  83. const h = vm.$createElement
  84. const vnode = h('p', void 0, [h('br'), 'hello world', h('br')])
  85. expect(vnode.children[0].tag).toBe('br')
  86. expect(vnode.children[1].text).toBe('hello world')
  87. expect(vnode.children[2].tag).toBe('br')
  88. })
  89. it('render vnode with children, omitting data', () => {
  90. const vm = new Vue({})
  91. const h = vm.$createElement
  92. const vnode = h('p', [h('br'), 'hello world', h('br')])
  93. expect(vnode.children[0].tag).toBe('br')
  94. expect(vnode.children[1].text).toBe('hello world')
  95. expect(vnode.children[2].tag).toBe('br')
  96. })
  97. it('render vnode with children, including boolean and null type', () => {
  98. const vm = new Vue({})
  99. const h = vm.$createElement
  100. const vnode = h('p', [h('br'), true, 123, h('br'), 'abc', null])
  101. expect(vnode.children.length).toBe(4)
  102. expect(vnode.children[0].tag).toBe('br')
  103. expect(vnode.children[1].text).toBe('123')
  104. expect(vnode.children[2].tag).toBe('br')
  105. expect(vnode.children[3].text).toBe('abc')
  106. })
  107. it('render svg elements with correct namespace', () => {
  108. const vm = new Vue({})
  109. const h = vm.$createElement
  110. const vnode = h('svg', [h('a', [h('foo', [h('bar')])])])
  111. expect(vnode.ns).toBe('svg')
  112. // should apply ns to children recursively
  113. expect(vnode.children[0].ns).toBe('svg')
  114. expect(vnode.children[0].children[0].ns).toBe('svg')
  115. expect(vnode.children[0].children[0].children[0].ns).toBe('svg')
  116. })
  117. it('render MathML elements with correct namespace', () => {
  118. const vm = new Vue({})
  119. const h = vm.$createElement
  120. const vnode = h('math', [h('matrix')])
  121. expect(vnode.ns).toBe('math')
  122. // should apply ns to children
  123. expect(vnode.children[0].ns).toBe('math')
  124. // although not explicitly listed, elements nested under <math>
  125. // should not be treated as component
  126. expect(vnode.children[0].componentOptions).toBeUndefined()
  127. })
  128. it('render svg foreignObject with correct namespace', () => {
  129. const vm = new Vue({})
  130. const h = vm.$createElement
  131. const vnode = h('svg', [h('foreignObject', [h('p'), h('svg')])])
  132. expect(vnode.ns).toBe('svg')
  133. expect(vnode.children[0].ns).toBe('svg')
  134. expect(vnode.children[0].children[0].ns).toBeUndefined()
  135. // #7330
  136. expect(vnode.children[0].children[1].ns).toBe('svg')
  137. })
  138. // #6642
  139. it('render svg foreignObject component with correct namespace', () => {
  140. const vm = new Vue({
  141. template: `
  142. <svg>
  143. <test></test>
  144. </svg>
  145. `,
  146. components: {
  147. test: {
  148. template: `
  149. <foreignObject>
  150. <p xmlns="http://www.w3.org/1999/xhtml"></p>
  151. </foreignObject>
  152. `
  153. }
  154. }
  155. }).$mount()
  156. const testComp = vm.$children[0]
  157. expect(testComp.$vnode.ns).toBe('svg')
  158. expect(testComp._vnode.tag).toBe('foreignObject')
  159. expect(testComp._vnode.ns).toBe('svg')
  160. expect(testComp._vnode.children[0].tag).toBe('p')
  161. expect(testComp._vnode.children[0].ns).toBeUndefined()
  162. })
  163. // #6506
  164. it('render SVGAElement in a component correctly', () => {
  165. const vm = new Vue({
  166. template: `
  167. <svg>
  168. <test></test>
  169. </svg>
  170. `,
  171. components: {
  172. test: { render: h => h('a') }
  173. }
  174. }).$mount()
  175. const testComp = vm.$children[0]
  176. expect(testComp.$vnode.ns).toBe('svg')
  177. expect(testComp._vnode.tag).toBe('a')
  178. expect(testComp._vnode.ns).toBe('svg')
  179. })
  180. it('warn observed data objects', () => {
  181. new Vue({
  182. data: {
  183. data: {}
  184. },
  185. render(h) {
  186. return h('div', this.data)
  187. }
  188. }).$mount()
  189. expect('Avoid using observed data object as vnode data').toHaveBeenWarned()
  190. })
  191. it('warn non-primitive key', () => {
  192. new Vue({
  193. render(h) {
  194. return h('div', { key: {} })
  195. }
  196. }).$mount()
  197. expect('Avoid using non-primitive value as key').toHaveBeenWarned()
  198. })
  199. it("doesn't warn boolean key", () => {
  200. new Vue({
  201. render(h) {
  202. return h('div', { key: true })
  203. }
  204. }).$mount()
  205. expect('Avoid using non-primitive value as key').not.toHaveBeenWarned()
  206. })
  207. it("doesn't warn symbol key", () => {
  208. new Vue({
  209. render(h) {
  210. return h('div', { key: Symbol('symbol') })
  211. }
  212. }).$mount()
  213. expect('Avoid using non-primitive value as key').not.toHaveBeenWarned()
  214. })
  215. it('nested child elements should be updated correctly', done => {
  216. const vm = new Vue({
  217. data: { n: 1 },
  218. render(h) {
  219. const list: any[] = []
  220. for (let i = 0; i < this.n; i++) {
  221. list.push(h('span', i))
  222. }
  223. const input = h('input', {
  224. attrs: {
  225. value: 'a',
  226. type: 'text'
  227. }
  228. })
  229. return h('div', [[...list, input]])
  230. }
  231. }).$mount()
  232. expect(vm.$el.innerHTML).toContain('<span>0</span><input')
  233. const el = vm.$el.querySelector('input')
  234. el.value = 'b'
  235. vm.n++
  236. waitForUpdate(() => {
  237. expect(vm.$el.innerHTML).toContain('<span>0</span><span>1</span><input')
  238. expect(vm.$el.querySelector('input')).toBe(el)
  239. expect(vm.$el.querySelector('input').value).toBe('b')
  240. }).then(done)
  241. })
  242. // #7786
  243. it('creates element with vnode reference in :class or :style', () => {
  244. const vm = new Vue({
  245. components: {
  246. foo: {
  247. render(h) {
  248. return h(
  249. 'div',
  250. {
  251. class: {
  252. 'has-vnode': this.$vnode
  253. }
  254. },
  255. 'foo'
  256. )
  257. }
  258. }
  259. },
  260. render: h => h('foo')
  261. }).$mount()
  262. expect(vm.$el.innerHTML).toContain('foo')
  263. expect(vm.$el.classList.contains('has-vnode')).toBe(true)
  264. })
  265. })