observer.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. var Observer = require('../../src/observer/observer')
  2. var u = undefined
  3. Observer.pathDelimiter = '.'
  4. describe('Observer', function () {
  5. var spy
  6. beforeEach(function () {
  7. spy = jasmine.createSpy('observer')
  8. })
  9. it('get', function () {
  10. Observer.emitGet = true
  11. var obj = {
  12. a: 1,
  13. b: {
  14. c: 2
  15. }
  16. }
  17. var ob = Observer.create(obj)
  18. ob.on('get', spy)
  19. var t = obj.a
  20. expect(spy).toHaveBeenCalledWith('a', u, u)
  21. expect(spy.callCount).toEqual(1)
  22. t = obj.b.c
  23. expect(spy).toHaveBeenCalledWith('b', u, u)
  24. expect(spy).toHaveBeenCalledWith('b.c', u, u)
  25. expect(spy.callCount).toEqual(3)
  26. Observer.emitGet = false
  27. })
  28. it('set', function () {
  29. var obj = {
  30. a: 1,
  31. b: {
  32. c: 2
  33. }
  34. }
  35. var ob = Observer.create(obj)
  36. ob.on('set', spy)
  37. obj.a = 3
  38. expect(spy).toHaveBeenCalledWith('a', 3, u)
  39. expect(spy.callCount).toEqual(1)
  40. obj.b.c = 4
  41. expect(spy).toHaveBeenCalledWith('b.c', 4, u)
  42. expect(spy.callCount).toEqual(2)
  43. var newB = { c: 5 }
  44. obj.b = newB
  45. expect(spy).toHaveBeenCalledWith('b', newB, u)
  46. expect(spy.callCount).toEqual(3)
  47. // same value set should not emit events
  48. obj.a = 3
  49. expect(spy.callCount).toEqual(3)
  50. })
  51. it('array get', function () {
  52. Observer.emitGet = true
  53. var obj = {
  54. arr: [{a:1}, {a:2}]
  55. }
  56. var ob = Observer.create(obj)
  57. ob.on('get', spy)
  58. var t = obj.arr[0].a
  59. expect(spy).toHaveBeenCalledWith('arr', u, u)
  60. expect(spy).toHaveBeenCalledWith('arr.0.a', u, u)
  61. expect(spy.callCount).toEqual(2)
  62. Observer.emitGet = false
  63. })
  64. it('array set', function () {
  65. var obj = {
  66. arr: [{a:1}, {a:2}]
  67. }
  68. var ob = Observer.create(obj)
  69. ob.on('set', spy)
  70. obj.arr[0].a = 2
  71. expect(spy).toHaveBeenCalledWith('arr.0.a', 2, u)
  72. // set events after mutation
  73. obj.arr.reverse()
  74. obj.arr[0].a = 3
  75. expect(spy).toHaveBeenCalledWith('arr.0.a', 3, u)
  76. })
  77. it('array mutate', function () {
  78. var arr = [{a:1}, {a:2}]
  79. var ob = Observer.create(arr)
  80. ob.on('mutate', spy)
  81. arr.push({a:3})
  82. expect(spy.mostRecentCall.args[0]).toEqual('')
  83. expect(spy.mostRecentCall.args[1]).toEqual(arr)
  84. var mutation = spy.mostRecentCall.args[2]
  85. expect(mutation).toBeDefined()
  86. expect(mutation.method).toEqual('push')
  87. expect(mutation.index).toEqual(2)
  88. expect(mutation.inserted.length).toEqual(1)
  89. expect(mutation.inserted[0]).toEqual(arr[2])
  90. })
  91. it('array set after mutate', function () {
  92. var arr = [{a:1}, {a:2}]
  93. var ob = Observer.create(arr)
  94. ob.on('set', spy)
  95. arr.push({a:3})
  96. arr[2].a = 4
  97. expect(spy).toHaveBeenCalledWith('2.a', 4, u)
  98. })
  99. it('object.$add', function () {
  100. // body...
  101. })
  102. it('object.$delete', function () {
  103. // body...
  104. })
  105. it('array.$set', function () {
  106. // body...
  107. })
  108. it('array.$remove', function () {
  109. // body...
  110. })
  111. })