transclude_spec.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. var transclude = require('../../../../src/compiler/transclude')
  2. var _ = require('../../../../src/util')
  3. if (_.inBrowser) {
  4. describe('Transclude', function () {
  5. var el, options
  6. beforeEach(function () {
  7. el = document.createElement('div')
  8. options = {}
  9. spyOn(_, 'warn')
  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(_.warn).toHaveBeenCalled()
  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('block instance', function () {
  38. var frag = document.createDocumentFragment()
  39. frag.appendChild(el)
  40. var res = transclude(frag, options)
  41. expect(res).toBe(frag)
  42. expect(res.childNodes.length).toBe(3)
  43. expect(res.childNodes[0].nodeType).toBe(8)
  44. expect(res.childNodes[1]).toBe(el)
  45. expect(res.childNodes[2].nodeType).toBe(8)
  46. })
  47. it('template element', function () {
  48. var tpl = document.createElement('template')
  49. tpl.innerHTML = '<div>123</div>'
  50. var res = transclude(tpl, options)
  51. expect(res instanceof DocumentFragment).toBe(true)
  52. expect(res.childNodes.length).toBe(3)
  53. expect(res.childNodes[0].nodeType).toBe(8)
  54. expect(res.childNodes[1].textContent).toBe('123')
  55. expect(res.childNodes[2].nodeType).toBe(8)
  56. })
  57. it('content transclusion', function () {
  58. el.innerHTML = '<p>hi</p>'
  59. options.template = '<div><content></content></div>'
  60. var res = transclude(el, options)
  61. expect(res.firstChild.tagName).toBe('DIV')
  62. expect(res.firstChild.firstChild.tagName).toBe('P')
  63. expect(res.firstChild.firstChild.textContent).toBe('hi')
  64. })
  65. it('fallback content', function () {
  66. options.template = '<content><p>fallback</p></content>'
  67. var res = transclude(el, options)
  68. expect(res.firstChild.tagName).toBe('P')
  69. expect(res.firstChild.textContent).toBe('fallback')
  70. })
  71. it('fallback content with multiple select', function () {
  72. el.innerHTML = '<p class="b">select b</p>'
  73. options.template = '<content select=".a"><p>fallback a</p></content><content select=".b">fallback b</content>'
  74. var res = transclude(el, options)
  75. expect(res.childNodes.length).toBe(2)
  76. expect(res.firstChild.textContent).toBe('fallback a')
  77. expect(res.lastChild.textContent).toBe('select b')
  78. })
  79. it('content transclusion with replace', function () {
  80. el.innerHTML = '<p>hi</p>'
  81. options.template = '<div><div><content></content></div></div>'
  82. options.replace = true
  83. var res = transclude(el, options)
  84. expect(res).not.toBe(el)
  85. expect(res.firstChild.tagName).toBe('DIV')
  86. expect(res.firstChild.firstChild.tagName).toBe('P')
  87. expect(res.firstChild.firstChild.textContent).toBe('hi')
  88. })
  89. it('block instance content transclusion', function () {
  90. el.innerHTML = '<p>hi</p><span>ho</span>'
  91. options.template = '<div></div><content select="p"></content><content select="span"></content>'
  92. options.replace = true
  93. var res = transclude(el, options)
  94. expect(res.childNodes[1].tagName).toBe('DIV')
  95. expect(res.childNodes[2].tagName).toBe('P')
  96. expect(res.childNodes[3].tagName).toBe('SPAN')
  97. })
  98. it('select should only match children', function () {
  99. el.innerHTML = '<p class="b">select b</p><span><p class="b">nested b</p></span><span><p class="c">nested c</p></span>'
  100. options.template = '<content select=".a"><p>fallback a</p></content><content select=".b">fallback b</content><content select=".c">fallback c</content>'
  101. var res = transclude(el, options)
  102. expect(res.childNodes.length).toBe(3)
  103. expect(res.firstChild.textContent).toBe('fallback a')
  104. expect(res.childNodes[1].textContent).toBe('select b')
  105. expect(res.lastChild.textContent).toBe('fallback c')
  106. })
  107. })
  108. }