transclude_spec.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. })
  43. it('template element', function () {
  44. var tpl = document.createElement('template')
  45. tpl.innerHTML = '<div>123</div>'
  46. var res = transclude(tpl, options)
  47. expect(res instanceof DocumentFragment).toBe(true)
  48. expect(res.childNodes.length).toBe(1)
  49. expect(res.childNodes[0].textContent).toBe('123')
  50. })
  51. it('content transclusion', function () {
  52. el.innerHTML = '<p>hi</p>'
  53. options.template = '<div><content></content></div>'
  54. var res = transclude(el, options)
  55. expect(res.firstChild.tagName).toBe('DIV')
  56. expect(res.firstChild.firstChild.tagName).toBe('P')
  57. expect(res.firstChild.firstChild.textContent).toBe('hi')
  58. })
  59. it('fallback content', function () {
  60. options.template = '<content><p>fallback</p></content>'
  61. var res = transclude(el, options)
  62. expect(res.firstChild.tagName).toBe('P')
  63. expect(res.firstChild.textContent).toBe('fallback')
  64. })
  65. it('content transclusion with replace', function () {
  66. el.innerHTML = '<p>hi</p>'
  67. options.template = '<div><div><content></content></div></div>'
  68. options.replace = true
  69. var res = transclude(el, options)
  70. expect(res).not.toBe(el)
  71. expect(res.firstChild.tagName).toBe('DIV')
  72. expect(res.firstChild.firstChild.tagName).toBe('P')
  73. expect(res.firstChild.firstChild.textContent).toBe('hi')
  74. })
  75. it('block instance content transclusion', function () {
  76. el.innerHTML = '<p>hi</p><span>ho</span>'
  77. options.template = '<div></div><content select="p"></content><content select="span"></content>'
  78. options.replace = true
  79. var res = transclude(el, options)
  80. expect(res.firstChild.tagName).toBe('DIV')
  81. expect(res.childNodes[1].tagName).toBe('P')
  82. expect(res.childNodes[2].tagName).toBe('SPAN')
  83. })
  84. })
  85. }