with_spec.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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" v-ref="child"></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. // test two-way
  76. vm.$.child.bb = 'B'
  77. vm.$.child.testt = { a: 'A' }
  78. _.nextTick(function () {
  79. expect(el.firstChild.textContent).toBe('A B')
  80. expect(vm.test.a).toBe('A')
  81. expect(vm.test).toBe(vm.$.child.testt)
  82. expect(vm.b).toBe('B')
  83. done()
  84. })
  85. })
  86. })
  87. })
  88. })
  89. it('teardown', function (done) {
  90. var vm = new Vue({
  91. el: el,
  92. data: {
  93. b: 'B'
  94. },
  95. template: '<div v-component="test" v-with="bb:b"></div>',
  96. components: {
  97. test: {
  98. template: '{{bb}}'
  99. }
  100. }
  101. })
  102. expect(el.firstChild.textContent).toBe('B')
  103. vm.b = 'BB'
  104. _.nextTick(function () {
  105. expect(el.firstChild.textContent).toBe('BB')
  106. vm._children[0]._directives[0].unbind()
  107. vm.b = 'BBB'
  108. _.nextTick(function () {
  109. expect(el.firstChild.textContent).toBe('BB')
  110. done()
  111. })
  112. })
  113. })
  114. it('non-root warning', function () {
  115. var vm = new Vue({
  116. el: el,
  117. template: '<div v-with="test"></div>'
  118. })
  119. expect(_.warn).toHaveBeenCalled()
  120. })
  121. it('no-parent warning', function () {
  122. el.setAttribute('v-with', 'test')
  123. var vm = new Vue({
  124. el: el
  125. })
  126. expect(_.warn).toHaveBeenCalled()
  127. })
  128. it('block instance with replace:true', function () {
  129. var vm = new Vue({
  130. el: el,
  131. template: '<div v-component="test" v-with="b:a" c="{{d}}"></div>',
  132. data: {
  133. a: 'AAA',
  134. d: 'DDD'
  135. },
  136. components: {
  137. test: {
  138. paramAttributes: ['c'],
  139. template: '<p>{{b}}</p><p>{{c}}</p>',
  140. replace: true
  141. }
  142. }
  143. })
  144. expect(el.innerHTML).toBe('<!--v-start--><p>AAA</p><p>DDD</p><!--v-end--><!--v-component-->')
  145. })
  146. it('bind literal values should not trigger setter warning', function (done) {
  147. var vm = new Vue({
  148. el: el,
  149. template: '<div v-component="test" v-with="a:\'test\'"></div>',
  150. components: {
  151. test: {
  152. template: '{{a}}'
  153. }
  154. }
  155. })
  156. expect(el.firstChild.innerHTML).toBe('test')
  157. vm._children[0].a = 'changed'
  158. _.nextTick(function () {
  159. expect(el.firstChild.innerHTML).toBe('changed')
  160. expect(_.warn).not.toHaveBeenCalled()
  161. done()
  162. })
  163. })
  164. it('should warn when binding literal value without childKey', function () {
  165. var vm = new Vue({
  166. el: el,
  167. template: '<div v-component="test" v-with="\'test\'"></div>',
  168. components: {
  169. test: {}
  170. }
  171. })
  172. expect(_.warn).toHaveBeenCalled()
  173. })
  174. })
  175. }