transclude_spec.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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('block instance', function () {
  39. var frag = document.createDocumentFragment()
  40. frag.appendChild(el)
  41. var res = transclude(frag, options)
  42. expect(res).toBe(frag)
  43. expect(res.childNodes.length).toBe(3)
  44. expect(res.childNodes[0].nodeType).toBe(3)
  45. expect(res.childNodes[1]).toBe(el)
  46. expect(res.childNodes[2].nodeType).toBe(3)
  47. })
  48. it('template element', function () {
  49. var tpl = document.createElement('template')
  50. tpl.innerHTML = '<div>123</div>'
  51. var res = transclude(tpl, options)
  52. expect(res instanceof DocumentFragment).toBe(true)
  53. expect(res.childNodes.length).toBe(3)
  54. expect(res.childNodes[0].nodeType).toBe(3)
  55. expect(res.childNodes[1].textContent).toBe('123')
  56. expect(res.childNodes[2].nodeType).toBe(3)
  57. })
  58. it('replacer attr should overwrite container attr of same name, except class should be merged', function () {
  59. el.setAttribute('class', 'test')
  60. el.setAttribute('title', 'parent')
  61. options.template = '<div class="other" title="child"></div>'
  62. options.replace = true
  63. options._asComponent = true
  64. var res = transclude(el, options)
  65. expect(res.getAttribute('class')).toBe('other test')
  66. expect(res.getAttribute('title')).toBe('child')
  67. })
  68. })
  69. }