utils.spec.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import {
  2. backfillMissingProvidersWithFallback,
  3. decideDefault,
  4. getUserDefaultValues,
  5. } from 'sentry/views/settings/account/notifications/utils';
  6. describe('Notification Settings Utils', () => {
  7. describe('getUserDefaultValues', () => {
  8. describe('when notificationsSettings are empty', () => {
  9. it('should return fallback values', () => {
  10. expect(getUserDefaultValues('deploy', {})).toEqual({
  11. email: 'committed_only',
  12. slack: 'never',
  13. msteams: 'never',
  14. });
  15. });
  16. });
  17. describe('when notificationsSettings are not empty', () => {
  18. it('should return the parent-independent notificationSettings', () => {
  19. expect(
  20. getUserDefaultValues('alerts', {
  21. alerts: {
  22. user: {
  23. me: {
  24. email: 'never',
  25. slack: 'never',
  26. },
  27. },
  28. },
  29. })
  30. ).toEqual({
  31. email: 'never',
  32. slack: 'never',
  33. });
  34. });
  35. });
  36. });
  37. describe('decideDefault', () => {
  38. describe('when there are no parent-specific settings', () => {
  39. describe('when the parent-independent settings match', () => {
  40. it('should return always when the settings are always', () => {
  41. expect(
  42. decideDefault('alerts', {
  43. alerts: {
  44. user: {
  45. me: {
  46. email: 'always',
  47. slack: 'always',
  48. },
  49. },
  50. },
  51. })
  52. ).toEqual('always');
  53. });
  54. it('should return never when the settings are never', () => {
  55. expect(
  56. decideDefault('alerts', {
  57. alerts: {
  58. user: {
  59. me: {
  60. email: 'never',
  61. slack: 'never',
  62. },
  63. },
  64. },
  65. })
  66. ).toEqual('never');
  67. });
  68. });
  69. describe('when the parent-independent settings do not match', () => {
  70. it('should return the always when the other value is never', () => {
  71. expect(
  72. decideDefault('alerts', {
  73. alerts: {
  74. user: {
  75. me: {
  76. email: 'always',
  77. slack: 'never',
  78. },
  79. },
  80. },
  81. })
  82. ).toEqual('always');
  83. });
  84. });
  85. });
  86. describe('when there are parent-specific settings', () => {
  87. describe('when the parent-specific settings are "below" the parent-independent settings', () => {
  88. it('should "prioritize" always over never', () => {
  89. expect(
  90. decideDefault('alerts', {
  91. alerts: {
  92. user: {
  93. me: {
  94. email: 'never',
  95. slack: 'never',
  96. },
  97. },
  98. project: {
  99. 1: {
  100. email: 'always',
  101. slack: 'always',
  102. },
  103. },
  104. },
  105. })
  106. ).toEqual('always');
  107. });
  108. it('should "prioritize" sometimes over always', () => {
  109. expect(
  110. decideDefault('alerts', {
  111. alerts: {
  112. user: {
  113. me: {
  114. email: 'never',
  115. slack: 'never',
  116. },
  117. },
  118. project: {
  119. 1: {
  120. email: 'subscribe_only',
  121. slack: 'subscribe_only',
  122. },
  123. },
  124. },
  125. })
  126. ).toEqual('subscribe_only');
  127. });
  128. });
  129. describe('when the parent-specific settings are "above" the parent-independent settings', () => {
  130. it('should return the parent-specific settings', () => {
  131. expect(
  132. decideDefault('alerts', {
  133. alerts: {
  134. user: {
  135. me: {
  136. email: 'always',
  137. slack: 'always',
  138. },
  139. },
  140. project: {
  141. 1: {
  142. email: 'never',
  143. slack: 'never',
  144. },
  145. },
  146. },
  147. })
  148. ).toEqual('always');
  149. });
  150. });
  151. });
  152. });
  153. describe('backfillMissingProvidersWithFallback', () => {
  154. describe('when scopeType is user', () => {
  155. it('should add missing provider with the fallback value', () => {
  156. expect(
  157. backfillMissingProvidersWithFallback({}, ['email'], 'sometimes', 'user')
  158. ).toEqual({email: 'sometimes', slack: 'never', msteams: 'never'});
  159. });
  160. it('should turn on all providers with the fallback value', () => {
  161. expect(
  162. backfillMissingProvidersWithFallback(
  163. {email: 'never', slack: 'never', msteams: 'never'},
  164. ['email', 'slack', 'msteams'],
  165. 'sometimes',
  166. 'user'
  167. )
  168. ).toEqual({email: 'sometimes', slack: 'sometimes', msteams: 'sometimes'});
  169. });
  170. it('should move the existing setting when providers are swapped', () => {
  171. expect(
  172. backfillMissingProvidersWithFallback(
  173. {email: 'always', slack: 'never', msteams: 'never'},
  174. ['slack', 'msteams'],
  175. '',
  176. 'user'
  177. )
  178. ).toEqual({email: 'never', slack: 'always', msteams: 'always'});
  179. });
  180. it('should turn off all providers when providers is empty', () => {
  181. expect(
  182. backfillMissingProvidersWithFallback(
  183. {email: 'always', slack: 'always', msteams: 'always'},
  184. [],
  185. '',
  186. 'user'
  187. )
  188. ).toEqual({email: 'never', slack: 'never', msteams: 'never'});
  189. });
  190. });
  191. describe('when scopeType is organization', () => {
  192. it('should retain OFF organization scope preference when provider list changes', () => {
  193. expect(
  194. backfillMissingProvidersWithFallback(
  195. {email: 'never', slack: 'never', msteams: 'never'},
  196. ['slack'],
  197. 'sometimes',
  198. 'organization'
  199. )
  200. ).toEqual({email: 'never', slack: 'never', msteams: 'never'});
  201. });
  202. });
  203. });
  204. });