transclude_spec.js 4.1 KB

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