watch.spec.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import Vue from 'vue'
  2. describe('Options watch', () => {
  3. let spy
  4. beforeEach(() => {
  5. spy = jasmine.createSpy('watch')
  6. })
  7. it('basic usage', done => {
  8. const vm = new Vue({
  9. data: {
  10. a: 1
  11. },
  12. watch: {
  13. a: spy
  14. }
  15. })
  16. expect(spy).not.toHaveBeenCalled()
  17. vm.a = 2
  18. expect(spy).not.toHaveBeenCalled()
  19. waitForUpdate(() => {
  20. expect(spy).toHaveBeenCalledWith(2, 1)
  21. }).then(done)
  22. })
  23. it('string method name', done => {
  24. const vm = new Vue({
  25. data: {
  26. a: 1
  27. },
  28. watch: {
  29. a: 'onChange'
  30. },
  31. methods: {
  32. onChange: spy
  33. }
  34. })
  35. expect(spy).not.toHaveBeenCalled()
  36. vm.a = 2
  37. expect(spy).not.toHaveBeenCalled()
  38. waitForUpdate(() => {
  39. expect(spy).toHaveBeenCalledWith(2, 1)
  40. }).then(done)
  41. })
  42. it('multiple cbs (after option merge)', done => {
  43. const spy1 = jasmine.createSpy('watch')
  44. const Test = Vue.extend({
  45. watch: {
  46. a: spy1
  47. }
  48. })
  49. const vm = new Test({
  50. data: { a: 1 },
  51. watch: {
  52. a: spy
  53. }
  54. })
  55. vm.a = 2
  56. waitForUpdate(() => {
  57. expect(spy1).toHaveBeenCalledWith(2, 1)
  58. expect(spy).toHaveBeenCalledWith(2, 1)
  59. }).then(done)
  60. })
  61. it('with option: immediate', done => {
  62. const vm = new Vue({
  63. data: { a: 1 },
  64. watch: {
  65. a: {
  66. handler: spy,
  67. immediate: true
  68. }
  69. }
  70. })
  71. expect(spy).toHaveBeenCalledWith(1)
  72. vm.a = 2
  73. waitForUpdate(() => {
  74. expect(spy).toHaveBeenCalledWith(2, 1)
  75. }).then(done)
  76. })
  77. it('with option: deep', done => {
  78. const vm = new Vue({
  79. data: { a: { b: 1 }},
  80. watch: {
  81. a: {
  82. handler: spy,
  83. deep: true
  84. }
  85. }
  86. })
  87. const oldA = vm.a
  88. expect(spy).not.toHaveBeenCalled()
  89. vm.a.b = 2
  90. expect(spy).not.toHaveBeenCalled()
  91. waitForUpdate(() => {
  92. expect(spy).toHaveBeenCalledWith(vm.a, vm.a)
  93. vm.a = { b: 3 }
  94. }).then(() => {
  95. expect(spy).toHaveBeenCalledWith(vm.a, oldA)
  96. }).then(done)
  97. })
  98. })