el.spec.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 = '<svg><text :x="x" :y="y" :fill="color">{{ text }}</text></svg>'
  42. const vm = new Vue({
  43. el: parent.childNodes[0],
  44. data: {
  45. x: 64,
  46. y: 128,
  47. color: 'red',
  48. text: 'svg text'
  49. }
  50. })
  51. expect(vm.$el.tagName).toBe('svg')
  52. expect(vm.$el.childNodes[0].getAttribute('x')).toBe(vm.x.toString())
  53. expect(vm.$el.childNodes[0].getAttribute('y')).toBe(vm.y.toString())
  54. expect(vm.$el.childNodes[0].getAttribute('fill')).toBe(vm.color)
  55. expect(vm.$el.childNodes[0].textContent).toBe(vm.text)
  56. })
  57. it('warn cannot find element', () => {
  58. new Vue({ el: '#non-existent' })
  59. expect('Cannot find element: #non-existent').toHaveBeenWarned()
  60. })
  61. })