controller_observer.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. QUnit.test( "controller observer tests - observe", assert => {
  2. App.Ticket.refresh([{
  3. id: 1,
  4. title: 'ticket',
  5. state_id: 1,
  6. customer_id: 33,
  7. organization_id: 1,
  8. owner_id: 1,
  9. preferences: { a: 1, b: 2 },
  10. }])
  11. var observer1 = new App.ControllerObserver({
  12. object_id: 1,
  13. template: 'version',
  14. observe: {
  15. title: true,
  16. preferences: true,
  17. },
  18. })
  19. var ticket = App.Ticket.find(1)
  20. assert.equal(false, observer1.hasChanged(ticket))
  21. // track title changes
  22. ticket.title = 'title 2'
  23. assert.equal(true, observer1.hasChanged(ticket))
  24. assert.equal(false, observer1.hasChanged(ticket))
  25. ticket.title = undefined
  26. assert.equal(true, observer1.hasChanged(ticket))
  27. assert.equal(false, observer1.hasChanged(ticket))
  28. ticket.title = 'title 3'
  29. assert.equal(true, observer1.hasChanged(ticket))
  30. assert.equal(false, observer1.hasChanged(ticket))
  31. // track no owner_id changes
  32. ticket.owner_id = 2
  33. assert.equal(false, observer1.hasChanged(ticket))
  34. assert.equal(false, observer1.hasChanged(ticket))
  35. // track preferences changes
  36. ticket.preferences['a'] = 3
  37. assert.equal(true, observer1.hasChanged(ticket))
  38. assert.equal(false, observer1.hasChanged(ticket))
  39. ticket.preferences['c'] = 3
  40. assert.equal(true, observer1.hasChanged(ticket))
  41. assert.equal(false, observer1.hasChanged(ticket))
  42. // track no new_attribute1 changes
  43. ticket.new_attribute1 = 'na 3'
  44. assert.equal(false, observer1.hasChanged(ticket))
  45. assert.equal(false, observer1.hasChanged(ticket))
  46. ticket.new_attribute2 = function() { console.log(1) }
  47. assert.equal(false, observer1.hasChanged(ticket))
  48. assert.equal(false, observer1.hasChanged(ticket))
  49. ticket.new_attribute2 = function() { console.log(2) }
  50. assert.equal(false, observer1.hasChanged(ticket))
  51. assert.equal(false, observer1.hasChanged(ticket))
  52. // track title changes
  53. ticket.title = function() { console.log(1) }
  54. assert.equal(true, observer1.hasChanged(ticket))
  55. assert.equal(false, observer1.hasChanged(ticket))
  56. ticket.title = function() { console.log(2) }
  57. assert.equal(false, observer1.hasChanged(ticket))
  58. assert.equal(false, observer1.hasChanged(ticket))
  59. ticket.title = 1
  60. assert.equal(true, observer1.hasChanged(ticket))
  61. assert.equal(false, observer1.hasChanged(ticket))
  62. });
  63. QUnit.test( "controller observer tests - observeNot", assert => {
  64. App.Ticket.refresh([{
  65. id: 2,
  66. title: 'ticket',
  67. state_id: 1,
  68. customer_id: 33,
  69. organization_id: 1,
  70. owner_id: 1,
  71. preferences: { a: 1, b: 2 },
  72. }])
  73. var observer1 = new App.ControllerObserver({
  74. object_id: 2,
  75. template: 'version',
  76. observeNot: {
  77. title: true,
  78. preferences: true,
  79. },
  80. })
  81. var ticket = App.Ticket.find(2)
  82. assert.equal(false, observer1.hasChanged(ticket))
  83. // track no title changes
  84. ticket.title = 'title 2'
  85. assert.equal(false, observer1.hasChanged(ticket))
  86. assert.equal(false, observer1.hasChanged(ticket))
  87. // track owner_id changes
  88. ticket.owner_id = 2
  89. assert.equal(true, observer1.hasChanged(ticket))
  90. assert.equal(false, observer1.hasChanged(ticket))
  91. ticket.owner_id = undefined
  92. assert.equal(true, observer1.hasChanged(ticket))
  93. assert.equal(false, observer1.hasChanged(ticket))
  94. ticket.owner_id = 3
  95. assert.equal(true, observer1.hasChanged(ticket))
  96. assert.equal(false, observer1.hasChanged(ticket))
  97. // track no preferences changes
  98. ticket.preferences['a'] = 3
  99. assert.equal(false, observer1.hasChanged(ticket))
  100. assert.equal(false, observer1.hasChanged(ticket))
  101. ticket.preferences['c'] = 3
  102. assert.equal(false, observer1.hasChanged(ticket))
  103. assert.equal(false, observer1.hasChanged(ticket))
  104. // track preferences2 changes
  105. ticket.preferences2 = {}
  106. assert.equal(true, observer1.hasChanged(ticket))
  107. assert.equal(false, observer1.hasChanged(ticket))
  108. ticket.preferences2['a'] = 3
  109. assert.equal(true, observer1.hasChanged(ticket))
  110. assert.equal(false, observer1.hasChanged(ticket))
  111. ticket.preferences2['a'] = 2
  112. assert.equal(true, observer1.hasChanged(ticket))
  113. assert.equal(false, observer1.hasChanged(ticket))
  114. ticket.preferences2['c'] = 3
  115. assert.equal(true, observer1.hasChanged(ticket))
  116. assert.equal(false, observer1.hasChanged(ticket))
  117. // track new_attribute1 changes
  118. ticket.new_attribute1 = 'na 3'
  119. assert.equal(true, observer1.hasChanged(ticket))
  120. assert.equal(false, observer1.hasChanged(ticket))
  121. // track no new_attribute2 changes (because of function content)
  122. ticket.new_attribute2 = function() { console.log(1) }
  123. assert.equal(false, observer1.hasChanged(ticket))
  124. assert.equal(false, observer1.hasChanged(ticket))
  125. ticket.new_attribute2 = function() { console.log(2) }
  126. assert.equal(false, observer1.hasChanged(ticket))
  127. assert.equal(false, observer1.hasChanged(ticket))
  128. // track owner_id changes (pnly if content has no function content)
  129. ticket.owner_id = function() { console.log(1) }
  130. assert.equal(true, observer1.hasChanged(ticket))
  131. assert.equal(false, observer1.hasChanged(ticket))
  132. ticket.owner_id = function() { console.log(2) }
  133. assert.equal(false, observer1.hasChanged(ticket))
  134. assert.equal(false, observer1.hasChanged(ticket))
  135. ticket.owner_id = 1
  136. assert.equal(true, observer1.hasChanged(ticket))
  137. assert.equal(false, observer1.hasChanged(ticket))
  138. });