with_spec.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. var _ = require('../../../../src/util')
  2. var Vue = require('../../../../src/vue')
  3. if (_.inBrowser) {
  4. describe('v-with', function () {
  5. var el
  6. beforeEach(function () {
  7. el = document.createElement('div')
  8. spyOn(_, 'warn')
  9. })
  10. it('no arg', function (done) {
  11. var vm = new Vue({
  12. el: el,
  13. data: {
  14. test: {
  15. a: 'A'
  16. }
  17. },
  18. template: '<div v-component="test" v-with="test"></div>',
  19. components: {
  20. test: {
  21. template: '{{a}}'
  22. }
  23. }
  24. })
  25. expect(el.firstChild.textContent).toBe('A')
  26. // swap nested prop
  27. vm.test.a = 'B'
  28. _.nextTick(function () {
  29. expect(el.firstChild.textContent).toBe('B')
  30. // swap passed down prop
  31. vm.test = { a: 'C' }
  32. _.nextTick(function () {
  33. expect(el.firstChild.textContent).toBe('C')
  34. // swap root $data
  35. vm.$data = { test: { a: 'D' }}
  36. _.nextTick(function () {
  37. expect(el.firstChild.textContent).toBe('D')
  38. done()
  39. })
  40. })
  41. })
  42. })
  43. it('with arg', function (done) {
  44. var vm = new Vue({
  45. el: el,
  46. data: {
  47. b: 'B',
  48. test: {
  49. a: 'A'
  50. }
  51. },
  52. template: '<div v-component="test" v-with="testt:test,bb:b"></div>',
  53. components: {
  54. test: {
  55. template: '{{testt.a}} {{bb}}'
  56. }
  57. }
  58. })
  59. expect(el.firstChild.textContent).toBe('A B')
  60. vm.test.a = 'AA'
  61. vm.b = 'BB'
  62. _.nextTick(function () {
  63. expect(el.firstChild.textContent).toBe('AA BB')
  64. vm.test = { a: 'AAA' }
  65. _.nextTick(function () {
  66. expect(el.firstChild.textContent).toBe('AAA BB')
  67. vm.$data = {
  68. b: 'BBB',
  69. test: {
  70. a: 'AAAA'
  71. }
  72. }
  73. _.nextTick(function () {
  74. expect(el.firstChild.textContent).toBe('AAAA BBB')
  75. done()
  76. })
  77. })
  78. })
  79. })
  80. it('teardown', function (done) {
  81. var vm = new Vue({
  82. el: el,
  83. data: {
  84. b: 'B'
  85. },
  86. template: '<div v-component="test" v-with="bb:b"></div>',
  87. components: {
  88. test: {
  89. template: '{{bb}}'
  90. }
  91. }
  92. })
  93. expect(el.firstChild.textContent).toBe('B')
  94. vm.b = 'BB'
  95. _.nextTick(function () {
  96. expect(el.firstChild.textContent).toBe('BB')
  97. vm._children[0]._directives[0].unbind()
  98. vm.b = 'BBB'
  99. _.nextTick(function () {
  100. expect(el.firstChild.textContent).toBe('BB')
  101. done()
  102. })
  103. })
  104. })
  105. it('non-root warning', function () {
  106. var vm = new Vue({
  107. el: el,
  108. template: '<div v-with="test"></div>'
  109. })
  110. expect(_.warn).toHaveBeenCalled()
  111. })
  112. it('no-parent warning', function () {
  113. el.setAttribute('v-with', 'test')
  114. var vm = new Vue({
  115. el: el
  116. })
  117. expect(_.warn).toHaveBeenCalled()
  118. })
  119. })
  120. }