transclude_spec.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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(hasWarned(_, 'Invalid template option')).toBe(true)
  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(3)
  44. expect(res.childNodes[1]).toBe(el)
  45. expect(res.childNodes[2].nodeType).toBe(3)
  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(3)
  54. expect(res.childNodes[1].textContent).toBe('123')
  55. expect(res.childNodes[2].nodeType).toBe(3)
  56. })
  57. it('replacer attr should overwrite container attr of same name, except class should be merged', function () {
  58. el.setAttribute('class', 'test')
  59. el.setAttribute('title', 'parent')
  60. options.template = '<div class="other" title="child"></div>'
  61. options.replace = true
  62. options._asComponent = true
  63. var res = transclude(el, options)
  64. expect(res.getAttribute('class')).toBe('other test')
  65. expect(res.getAttribute('title')).toBe('child')
  66. })
  67. })
  68. }