partial_spec.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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('caching', function () {
  47. var calls = 0
  48. var compile = compiler.compile
  49. compiler.compile = function () {
  50. calls++
  51. return compile.apply(this, arguments)
  52. }
  53. // Note: caching only works on components, not native Vue
  54. var Comp = Vue.extend({
  55. template:
  56. '<partial name="cache-test"></partial> ' +
  57. '<partial name="cache-test"></partial>',
  58. partials: {
  59. 'cache-test': 'cache-test {{msg}}'
  60. }
  61. })
  62. new Comp({
  63. el: el,
  64. data: {
  65. msg: 'hi'
  66. }
  67. })
  68. expect(el.textContent).toBe('cache-test hi cache-test hi')
  69. // one call for instance, and one for partial
  70. expect(calls).toBe(2)
  71. // cleanup
  72. compiler.compile = compile
  73. })
  74. it('teardown', function () {
  75. var vm = new Vue({
  76. el: el,
  77. template: '<partial name="test-{{id}}"></partial>',
  78. data: {
  79. id: 'a'
  80. },
  81. partials: {
  82. 'test-a': 'a {{id}}'
  83. }
  84. })
  85. expect(vm._directives.length).toBe(2)
  86. expect(vm._directives[1].name).toBe('partial')
  87. expect(vm._watchers.length).toBe(2)
  88. vm._directives[1]._teardown()
  89. // the text-directive should've been removed.
  90. expect(vm._directives.length).toBe(1)
  91. expect(vm._directives[0].name).toBe('partial')
  92. expect(vm._watchers.length).toBe(0)
  93. })
  94. })