transclude_spec.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. var transclude = require('src/compiler').transclude
  2. var Vue = require('src')
  3. var _ = require('src/util')
  4. describe('Transclude', function () {
  5. var el, options
  6. beforeEach(function () {
  7. el = document.createElement('div')
  8. options = _.extend({}, Vue.options)
  9. })
  10. it('normal', function () {
  11. var res = transclude(el, options)
  12. expect(res).toBe(el)
  13. })
  14. it('template', function () {
  15. options.template = '{{hi}}'
  16. var res = transclude(el, options)
  17. expect(res).toBe(el)
  18. expect(res.innerHTML).toBe('{{hi}}')
  19. })
  20. it('template invalid', function () {
  21. options.template = '#non-existent-stuff'
  22. var res = transclude(el, options)
  23. expect(res).toBeUndefined()
  24. expect('Invalid template option').toHaveBeenWarned()
  25. })
  26. it('template replace', function () {
  27. el.className = 'hello'
  28. options.template = '<div>{{hi}}</div>'
  29. options.replace = true
  30. var res = transclude(el, options)
  31. expect(res).not.toBe(el)
  32. expect(res.tagName).toBe('DIV')
  33. expect(res.className).toBe('hello')
  34. expect(res.innerHTML).toBe('{{hi}}')
  35. })
  36. it('template replace -> fragment instance', function () {
  37. var res
  38. options.replace = true
  39. // multiple root
  40. options.template = '<div></div><div></div>'
  41. res = transclude(el, options)
  42. expect(res.nodeType).toBe(11)
  43. // non-element
  44. options.template = '{{hi}}'
  45. res = transclude(el, options)
  46. expect(res.nodeType).toBe(11)
  47. // single component: <component>
  48. options.template = '<component bind-is="hi"></component>'
  49. res = transclude(el, options)
  50. expect(res.nodeType).toBe(11)
  51. // single component: custom element
  52. options.template = '<test></test>'
  53. options.components = { test: {}}
  54. res = transclude(el, options)
  55. expect(res.nodeType).toBe(11)
  56. // single component: is
  57. options.template = '<div is="test"></div>'
  58. res = transclude(el, options)
  59. expect(res.nodeType).toBe(11)
  60. // element directive
  61. options.template = '<el-dir></el-dir>'
  62. options.elementDirectives = { 'el-dir': {}}
  63. res = transclude(el, options)
  64. expect(res.nodeType).toBe(11)
  65. // v-for
  66. options.template = '<div v-for="item in list"></div>'
  67. res = transclude(el, options)
  68. expect(res.nodeType).toBe(11)
  69. // v-if
  70. options.template = '<div v-if="ok"></div>'
  71. res = transclude(el, options)
  72. expect(res.nodeType).toBe(11)
  73. })
  74. it('direct fragment instance', function () {
  75. var frag = document.createDocumentFragment()
  76. frag.appendChild(el)
  77. var res = transclude(frag, options)
  78. expect(res).toBe(frag)
  79. expect(res.childNodes.length).toBe(3)
  80. expect(res.childNodes[0].nodeType).toBe(3)
  81. expect(res.childNodes[1]).toBe(el)
  82. expect(res.childNodes[2].nodeType).toBe(3)
  83. })
  84. it('template element', function () {
  85. var tpl = document.createElement('template')
  86. tpl.innerHTML = '<div>123</div>'
  87. var res = transclude(tpl, options)
  88. expect(res.nodeType).toBe(11)
  89. expect(res.childNodes.length).toBe(3)
  90. expect(res.childNodes[0].nodeType).toBe(3)
  91. expect(res.childNodes[1].textContent).toBe('123')
  92. expect(res.childNodes[2].nodeType).toBe(3)
  93. })
  94. it('replacer attr should overwrite container attr of same name, except class should be merged', function () {
  95. el.setAttribute('class', 'test other')
  96. el.setAttribute('title', 'parent')
  97. options.template = '<div class="other ok" title="child"></div>'
  98. options.replace = true
  99. options._asComponent = true
  100. var res = transclude(el, options)
  101. expect(res.getAttribute('class')).toBe('other ok test')
  102. expect(res.getAttribute('title')).toBe('child')
  103. })
  104. it('class merge for svg elements', function () {
  105. el.setAttribute('class', 'test')
  106. options.template = '<circle class="other"></circle>'
  107. options.replace = true
  108. options._asComponent = true
  109. var res = transclude(el, options)
  110. expect(res.namespaceURI).toBe('http://www.w3.org/2000/svg')
  111. expect(res.getAttribute('class')).toBe('other test')
  112. })
  113. })