watch.spec.js 2.1 KB

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