alertStore.spec.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import AlertStore from 'sentry/stores/alertStore';
  2. jest.mock('sentry/utils/localStorage');
  3. describe('AlertStore', function () {
  4. beforeEach(function () {
  5. AlertStore.alerts = [];
  6. AlertStore.count = 0;
  7. });
  8. describe('addAlert()', function () {
  9. it('should add a new alert with incrementing key', function () {
  10. AlertStore.addAlert({
  11. message: 'Bzzzzzzp *crash*',
  12. type: 'error',
  13. });
  14. AlertStore.addAlert({
  15. message: 'Everything is super',
  16. type: 'info',
  17. });
  18. expect(AlertStore.alerts).toHaveLength(2);
  19. expect(AlertStore.alerts[0].key).toEqual(0);
  20. expect(AlertStore.alerts[1].key).toEqual(1);
  21. });
  22. it('should not add duplicates when noDuplicates is set', function () {
  23. AlertStore.addAlert({
  24. id: 'unique-key',
  25. message: 'Bzzzzzzp *crash*',
  26. type: 'error',
  27. noDuplicates: true,
  28. });
  29. AlertStore.addAlert({
  30. id: 'unique-key',
  31. message: 'Bzzzzzzp *crash*',
  32. type: 'error',
  33. noDuplicates: true,
  34. });
  35. expect(AlertStore.alerts).toHaveLength(1);
  36. });
  37. });
  38. describe('closeAlert()', function () {
  39. it('should remove alert', function () {
  40. AlertStore.alerts = [
  41. {key: 1, message: 'foo', type: 'error'},
  42. {key: 2, message: 'bar', type: 'error'},
  43. {key: 3, message: 'baz', type: 'error'},
  44. ];
  45. AlertStore.closeAlert(AlertStore.alerts[1]);
  46. expect(AlertStore.alerts).toHaveLength(2);
  47. expect(AlertStore.alerts[0].key).toEqual(1);
  48. expect(AlertStore.alerts[1].key).toEqual(3);
  49. });
  50. it('should persist removal of persistent alerts', function () {
  51. const alert = {
  52. key: 1,
  53. id: 'test',
  54. message: 'this is a test',
  55. type: 'error',
  56. } as const;
  57. AlertStore.closeAlert(alert);
  58. AlertStore.addAlert(alert);
  59. expect(AlertStore.alerts).toHaveLength(0);
  60. });
  61. });
  62. });