el.spec.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. it('warn cannot find element', () => {
  65. new Vue({ el: '#non-existent' })
  66. expect('Cannot find element: #non-existent').toHaveBeenWarned()
  67. })
  68. })