partial_spec.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. var Vue = require('src')
  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 inside v-for', function () {
  47. new Vue({
  48. el: el,
  49. template: '<div v-for="id in list"><partial v-bind:name="\'test-\' + id"></partial></div>',
  50. data: {
  51. list: ['a', 'b']
  52. },
  53. partials: {
  54. 'test-a': 'a {{id}}',
  55. 'test-b': 'b {{id}}'
  56. }
  57. })
  58. expect(el.textContent).toBe('a ab b')
  59. })
  60. it('caching', function () {
  61. var calls = 0
  62. var compile = compiler.compile
  63. compiler.compile = function () {
  64. calls++
  65. return compile.apply(this, arguments)
  66. }
  67. // Note: caching only works on components, not native Vue
  68. var Comp = Vue.extend({
  69. template:
  70. '<partial name="cache-test"></partial> ' +
  71. '<partial name="cache-test"></partial>',
  72. partials: {
  73. 'cache-test': 'cache-test {{msg}}'
  74. }
  75. })
  76. new Comp({
  77. el: el,
  78. data: {
  79. msg: 'hi'
  80. }
  81. })
  82. expect(el.textContent).toBe('cache-test hi cache-test hi')
  83. // one call for instance, and one for partial
  84. expect(calls).toBe(2)
  85. // cleanup
  86. compiler.compile = compile
  87. })
  88. it('teardown', function () {
  89. var vm = new Vue({
  90. el: el,
  91. template: '<partial :name="\'test-\' + id"></partial>',
  92. data: {
  93. id: 'a'
  94. },
  95. partials: {
  96. 'test-a': 'a {{id}}'
  97. }
  98. })
  99. expect(vm._directives.length).toBe(2)
  100. expect(vm._watchers.length).toBe(2)
  101. var partialDir
  102. vm._directives.some(function (d) {
  103. if (d.name === 'partial') {
  104. partialDir = d
  105. return true
  106. }
  107. })
  108. partialDir._teardown()
  109. // the text-directive should've been removed.
  110. expect(vm._directives.length).toBe(1)
  111. expect(vm._directives[0].name).toBe('partial')
  112. expect(vm._watchers.length).toBe(0)
  113. })
  114. })