transclude_spec.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. var transclude = require('../../../../src/compile/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.firstChild.nodeType).toBe(8)
  44. expect(res.lastChild.nodeType).toBe(8)
  45. expect(res.childNodes[1]).toBe(el)
  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.childNodes.length).toBe(3)
  52. expect(res.firstChild.nodeType).toBe(8)
  53. expect(res.lastChild.nodeType).toBe(8)
  54. expect(res.childNodes[1].textContent).toBe('123')
  55. })
  56. it('content transclusion', function () {
  57. el.innerHTML = '<p>hi</p>'
  58. options.template = '<div><content></content></div>'
  59. var res = transclude(el, options)
  60. expect(res.firstChild.tagName).toBe('DIV')
  61. expect(res.firstChild.firstChild.tagName).toBe('P')
  62. expect(res.firstChild.firstChild.textContent).toBe('hi')
  63. })
  64. it('fallback content', function () {
  65. options.template = '<content><p>fallback</p></content>'
  66. var res = transclude(el, options)
  67. expect(res.firstChild.tagName).toBe('P')
  68. expect(res.firstChild.textContent).toBe('fallback')
  69. })
  70. it('content transclusion with replace', function () {
  71. el.innerHTML = '<p>hi</p>'
  72. options.template = '<div><div><content></content></div></div>'
  73. options.replace = true
  74. var res = transclude(el, options)
  75. expect(res).not.toBe(el)
  76. expect(res.firstChild.tagName).toBe('DIV')
  77. expect(res.firstChild.firstChild.tagName).toBe('P')
  78. expect(res.firstChild.firstChild.textContent).toBe('hi')
  79. })
  80. it('block instance content transclusion', function () {
  81. el.innerHTML = '<p>hi</p><span>ho</span>'
  82. options.template = '<div></div><content select="p"></content><content select="span"></content>'
  83. var res = transclude(el, options)
  84. expect(res.firstChild.tagName).toBe('DIV')
  85. expect(res.childNodes[1].tagName).toBe('P')
  86. expect(res.childNodes[2].tagName).toBe('SPAN')
  87. })
  88. })
  89. }