el.spec.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import Vue from 'vue'
  2. describe('Options el', () => {
  3. it('basic usage', () => {
  4. const el = document.createElement('div')
  5. el.innerHTML = '<span>{{message}}</span>'
  6. const vm = new Vue({
  7. el,
  8. data: { message: 'hello world' }
  9. })
  10. expect(vm.$el.tagName).toBe('DIV')
  11. expect(vm.$el.textContent).toBe(vm.message)
  12. })
  13. it('should be replaced when use together with `template` option', () => {
  14. const el = document.createElement('div')
  15. el.innerHTML = '<span>{{message}}</span>'
  16. const vm = new Vue({
  17. el,
  18. template: '<p id="app"><span>{{message}}</span></p>',
  19. data: { message: 'hello world' }
  20. })
  21. expect(vm.$el.tagName).toBe('P')
  22. expect(vm.$el.textContent).toBe(vm.message)
  23. })
  24. it('should be replaced when use together with `render` option', () => {
  25. const el = document.createElement('div')
  26. el.innerHTML = '<span>{{message}}</span>'
  27. const vm = new Vue({
  28. el,
  29. render (h) {
  30. return h('p', { staticAttrs: { id: 'app' }}, [
  31. h('span', {}, [this.message])
  32. ])
  33. },
  34. data: { message: 'hello world' }
  35. })
  36. expect(vm.$el.tagName).toBe('P')
  37. expect(vm.$el.textContent).toBe(vm.message)
  38. })
  39. it('svg element', () => {
  40. const parent = document.createElement('div')
  41. parent.innerHTML =
  42. '<svg>' +
  43. '<text :x="x" :y="y" :fill="color">{{ text }}</text>' +
  44. '<g><clipPath><foo></foo></clipPath></g>' +
  45. '</svg>'
  46. const vm = new Vue({
  47. el: parent.childNodes[0],
  48. data: {
  49. x: 64,
  50. y: 128,
  51. color: 'red',
  52. text: 'svg text'
  53. }
  54. })
  55. expect(vm.$el.tagName).toBe('svg')
  56. expect(vm.$el.childNodes[0].getAttribute('x')).toBe(vm.x.toString())
  57. expect(vm.$el.childNodes[0].getAttribute('y')).toBe(vm.y.toString())
  58. expect(vm.$el.childNodes[0].getAttribute('fill')).toBe(vm.color)
  59. expect(vm.$el.childNodes[0].textContent).toBe(vm.text)
  60. // nested, non-explicitly listed SVG elements
  61. expect(vm.$el.childNodes[1].childNodes[0].namespaceURI).toContain('svg')
  62. expect(vm.$el.childNodes[1].childNodes[0].childNodes[0].namespaceURI).toContain('svg')
  63. })
  64. // https://w3c.github.io/DOM-Parsing/#dfn-serializing-an-attribute-value
  65. it('properly decode attribute values when parsing templates from DOM', () => {
  66. const el = document.createElement('div')
  67. el.innerHTML = '<a href="/a?foo=bar&baz=qux" name="<abc>" single=\'"hi"\'></a>'
  68. const vm = new Vue({ el })
  69. expect(vm.$el.children[0].getAttribute('href')).toBe('/a?foo=bar&baz=qux')
  70. expect(vm.$el.children[0].getAttribute('name')).toBe('<abc>')
  71. expect(vm.$el.children[0].getAttribute('single')).toBe('"hi"')
  72. })
  73. it('decode attribute value newlines when parsing templates from DOM in IE', () => {
  74. const el = document.createElement('div')
  75. el.innerHTML = `<a :style="{\ncolor:'red'\n}"></a>`
  76. const vm = new Vue({ el })
  77. expect(vm.$el.children[0].style.color).toBe('red')
  78. })
  79. it('warn cannot find element', () => {
  80. new Vue({ el: '#non-existent' })
  81. expect('Cannot find element: #non-existent').toHaveBeenWarned()
  82. })
  83. })