watch.spec.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import Vue from 'vue'
  2. import testObjectOption from '../../../helpers/test-object-option'
  3. import { finished } from 'stream';
  4. describe('Options watch', () => {
  5. let spy
  6. beforeEach(() => {
  7. spy = jasmine.createSpy('watch')
  8. })
  9. testObjectOption('watch')
  10. it('basic usage', done => {
  11. const vm = new Vue({
  12. data: {
  13. a: 1
  14. },
  15. watch: {
  16. a: spy
  17. }
  18. })
  19. expect(spy).not.toHaveBeenCalled()
  20. vm.a = 2
  21. expect(spy).not.toHaveBeenCalled()
  22. waitForUpdate(() => {
  23. expect(spy).toHaveBeenCalledWith(2, 1)
  24. }).then(done)
  25. })
  26. it('string method name', done => {
  27. const vm = new Vue({
  28. data: {
  29. a: 1
  30. },
  31. watch: {
  32. a: 'onChange'
  33. },
  34. methods: {
  35. onChange: spy
  36. }
  37. })
  38. expect(spy).not.toHaveBeenCalled()
  39. vm.a = 2
  40. expect(spy).not.toHaveBeenCalled()
  41. waitForUpdate(() => {
  42. expect(spy).toHaveBeenCalledWith(2, 1)
  43. }).then(done)
  44. })
  45. it('multiple cbs (after option merge)', done => {
  46. const spy1 = jasmine.createSpy('watch')
  47. const Test = Vue.extend({
  48. watch: {
  49. a: spy1
  50. }
  51. })
  52. const vm = new Test({
  53. data: { a: 1 },
  54. watch: {
  55. a: spy
  56. }
  57. })
  58. vm.a = 2
  59. waitForUpdate(() => {
  60. expect(spy1).toHaveBeenCalledWith(2, 1)
  61. expect(spy).toHaveBeenCalledWith(2, 1)
  62. }).then(done)
  63. })
  64. it('with option: immediate', done => {
  65. const vm = new Vue({
  66. data: { a: 1 },
  67. watch: {
  68. a: {
  69. handler: spy,
  70. immediate: true
  71. }
  72. }
  73. })
  74. expect(spy).toHaveBeenCalledWith(1)
  75. vm.a = 2
  76. waitForUpdate(() => {
  77. expect(spy).toHaveBeenCalledWith(2, 1)
  78. }).then(done)
  79. })
  80. it('with option: deep', done => {
  81. const vm = new Vue({
  82. data: { a: { b: 1 }},
  83. watch: {
  84. a: {
  85. handler: spy,
  86. deep: true
  87. }
  88. }
  89. })
  90. const oldA = vm.a
  91. expect(spy).not.toHaveBeenCalled()
  92. vm.a.b = 2
  93. expect(spy).not.toHaveBeenCalled()
  94. waitForUpdate(() => {
  95. expect(spy).toHaveBeenCalledWith(vm.a, vm.a)
  96. vm.a = { b: 3 }
  97. }).then(() => {
  98. expect(spy).toHaveBeenCalledWith(vm.a, oldA)
  99. }).then(done)
  100. })
  101. it('correctly merges multiple extends', done => {
  102. const spy2 = jasmine.createSpy('A')
  103. const spy3 = jasmine.createSpy('B')
  104. const A = Vue.extend({
  105. data: function () {
  106. return {
  107. a: 0,
  108. b: 0
  109. }
  110. },
  111. watch: {
  112. b: spy
  113. }
  114. })
  115. const B = Vue.extend({
  116. extends: A,
  117. watch: {
  118. a: spy2
  119. }
  120. })
  121. const C = Vue.extend({
  122. extends: B,
  123. watch: {
  124. a: spy3
  125. }
  126. })
  127. const vm = new C()
  128. vm.a = 1
  129. waitForUpdate(() => {
  130. expect(spy).not.toHaveBeenCalled()
  131. expect(spy2).toHaveBeenCalledWith(1, 0)
  132. expect(spy3).toHaveBeenCalledWith(1, 0)
  133. }).then(done)
  134. })
  135. it('should support watching unicode paths', done => {
  136. const vm = new Vue({
  137. data: {
  138. 数据: 1
  139. },
  140. watch: {
  141. 数据: spy
  142. }
  143. })
  144. expect(spy).not.toHaveBeenCalled()
  145. vm['数据'] = 2
  146. expect(spy).not.toHaveBeenCalled()
  147. waitForUpdate(() => {
  148. expect(spy).toHaveBeenCalledWith(2, 1)
  149. }).then(done)
  150. })
  151. it('should not warn proper usage', () => {
  152. const vm = new Vue({
  153. data: {
  154. foo: { _bar: 1 }, // element has such watchers...
  155. prop1: 123
  156. },
  157. watch: {
  158. 'foo._bar': () => {},
  159. prop1 () {}
  160. }
  161. })
  162. expect(`Failed watching path`).not.toHaveBeenWarned()
  163. })
  164. })