alertStore.spec.tsx 2.3 KB

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