el.spec.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 () {
  30. const h = this.$createElement
  31. return h('p', { staticAttrs: { id: 'app' }}, [
  32. h('span', {}, [this.message])
  33. ])
  34. },
  35. data: { message: 'hello world' }
  36. })
  37. expect(vm.$el.tagName).toBe('P')
  38. expect(vm.$el.textContent).toBe(vm.message)
  39. })
  40. it('svg element', () => {
  41. const ns = 'http://www.w3.org/2000/svg'
  42. const el = document.createElementNS(ns, 'svg')
  43. const text = document.createElementNS(ns, 'text')
  44. text.setAttribute(':x', 'x')
  45. text.setAttribute(':y', 'y')
  46. text.setAttribute(':fill', 'color')
  47. text.textContent = '{{text}}'
  48. el.appendChild(text)
  49. const vm = new Vue({
  50. el,
  51. data: {
  52. x: 64, y: 128, color: 'red', 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. })
  61. it('warn cannot find element', () => {
  62. new Vue({ el: '#non-existent' })
  63. expect('Cannot find element: #non-existent').toHaveBeenWarned()
  64. })
  65. })