el.spec.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 togther 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 togther 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', function () {
  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('warn cannot find element', () => {
  74. new Vue({ el: '#non-existent' })
  75. expect('Cannot find element: #non-existent').toHaveBeenWarned()
  76. })
  77. })