transclude_spec.js 4.4 KB

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