transclude_spec.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 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: v-component
  59. options.template = '<div v-component="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. })
  72. it('direct fragment instance', function () {
  73. var frag = document.createDocumentFragment()
  74. frag.appendChild(el)
  75. var res = transclude(frag, options)
  76. expect(res).toBe(frag)
  77. expect(res.childNodes.length).toBe(3)
  78. expect(res.childNodes[0].nodeType).toBe(3)
  79. expect(res.childNodes[1]).toBe(el)
  80. expect(res.childNodes[2].nodeType).toBe(3)
  81. })
  82. it('template element', function () {
  83. var tpl = document.createElement('template')
  84. tpl.innerHTML = '<div>123</div>'
  85. var res = transclude(tpl, options)
  86. expect(res instanceof DocumentFragment).toBe(true)
  87. expect(res.childNodes.length).toBe(3)
  88. expect(res.childNodes[0].nodeType).toBe(3)
  89. expect(res.childNodes[1].textContent).toBe('123')
  90. expect(res.childNodes[2].nodeType).toBe(3)
  91. })
  92. it('replacer attr should overwrite container attr of same name, except class should be merged', function () {
  93. el.setAttribute('class', 'test')
  94. el.setAttribute('title', 'parent')
  95. options.template = '<div class="other" title="child"></div>'
  96. options.replace = true
  97. options._asComponent = true
  98. var res = transclude(el, options)
  99. expect(res.getAttribute('class')).toBe('other test')
  100. expect(res.getAttribute('title')).toBe('child')
  101. })
  102. it('class merge for svg elements', function () {
  103. el.setAttribute('class', 'test')
  104. options.template = '<circle class="other"></circle>'
  105. options.replace = true
  106. options._asComponent = true
  107. var res = transclude(el, options)
  108. expect(res.namespaceURI).toBe('http://www.w3.org/2000/svg')
  109. expect(res.getAttribute('class')).toBe('other test')
  110. })
  111. })
  112. }