partial_spec.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. var Vue = require('../../../../src/vue')
  2. var _ = require('../../../../src/util')
  3. var compiler = require('../../../../src/compiler')
  4. describe('Partial', function () {
  5. var el
  6. beforeEach(function () {
  7. el = document.createElement('div')
  8. })
  9. it('static', function (done) {
  10. var vm = new Vue({
  11. el: el,
  12. template: '<partial name="yo"></partial>',
  13. data: {
  14. msg: 'hello'
  15. },
  16. partials: {
  17. yo: '{{msg}}'
  18. }
  19. })
  20. expect(el.textContent).toBe('hello')
  21. vm.msg = 'what'
  22. _.nextTick(function () {
  23. expect(el.textContent).toBe('what')
  24. done()
  25. })
  26. })
  27. it('dynamic', function (done) {
  28. var vm = new Vue({
  29. el: el,
  30. template: '<partial name="test-{{id}}"></partial>',
  31. data: {
  32. id: 'a'
  33. },
  34. partials: {
  35. 'test-a': 'a {{id}}',
  36. 'test-b': 'b {{id}}'
  37. }
  38. })
  39. expect(el.textContent).toBe('a a')
  40. vm.id = 'b'
  41. _.nextTick(function () {
  42. expect(el.textContent).toBe('b b')
  43. done()
  44. })
  45. })
  46. it('dynamic (new syntax)', function (done) {
  47. var vm = new Vue({
  48. el: el,
  49. template: '<partial bind-name="\'test-\' + id"></partial>',
  50. data: {
  51. id: 'a'
  52. },
  53. partials: {
  54. 'test-a': 'a {{id}}',
  55. 'test-b': 'b {{id}}'
  56. }
  57. })
  58. expect(el.textContent).toBe('a a')
  59. vm.id = 'b'
  60. _.nextTick(function () {
  61. expect(el.textContent).toBe('b b')
  62. done()
  63. })
  64. })
  65. it('caching', function () {
  66. var calls = 0
  67. var compile = compiler.compile
  68. compiler.compile = function () {
  69. calls++
  70. return compile.apply(this, arguments)
  71. }
  72. // Note: caching only works on components, not native Vue
  73. var Comp = Vue.extend({
  74. template:
  75. '<partial name="cache-test"></partial> ' +
  76. '<partial name="cache-test"></partial>',
  77. partials: {
  78. 'cache-test': 'cache-test {{msg}}'
  79. }
  80. })
  81. new Comp({
  82. el: el,
  83. data: {
  84. msg: 'hi'
  85. }
  86. })
  87. expect(el.textContent).toBe('cache-test hi cache-test hi')
  88. // one call for instance, and one for partial
  89. expect(calls).toBe(2)
  90. // cleanup
  91. compiler.compile = compile
  92. })
  93. it('teardown', function () {
  94. var vm = new Vue({
  95. el: el,
  96. template: '<partial name="test-{{id}}"></partial>',
  97. data: {
  98. id: 'a'
  99. },
  100. partials: {
  101. 'test-a': 'a {{id}}'
  102. }
  103. })
  104. expect(vm._directives.length).toBe(2)
  105. expect(vm._watchers.length).toBe(2)
  106. var partialDir
  107. vm._directives.some(function (d) {
  108. if (d.name === 'partial') {
  109. partialDir = d
  110. return true
  111. }
  112. })
  113. partialDir._teardown()
  114. // the text-directive should've been removed.
  115. expect(vm._directives.length).toBe(1)
  116. expect(vm._directives[0].name).toBe('partial')
  117. expect(vm._watchers.length).toBe(0)
  118. })
  119. })