useDismissAlert.spec.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import {reactHooks} from 'sentry-test/reactTestingLibrary';
  2. import {setMockDate} from 'sentry-test/utils';
  3. import localStorage from 'sentry/utils/localStorage';
  4. import useDismissAlert from 'sentry/utils/useDismissAlert';
  5. jest.mock('sentry/utils/localStorage');
  6. const mockSetItem = jest.mocked(localStorage.setItem);
  7. const mockGetItem = jest.mocked(localStorage.getItem);
  8. const key = 'test_123';
  9. const now = new Date('2020-01-01');
  10. describe('useDismissAlert', () => {
  11. beforeEach(() => {
  12. setMockDate(now);
  13. mockSetItem.mockReset();
  14. mockGetItem.mockReset();
  15. });
  16. it('should return a stable ref for the dismiss() function', () => {
  17. const {result, rerender} = reactHooks.renderHook(useDismissAlert, {
  18. initialProps: {key},
  19. });
  20. const initialRef = result.current.dismiss;
  21. rerender({key});
  22. expect(result.current.dismiss).toEqual(initialRef);
  23. });
  24. it('should not be dismissed if there is no value in localstorage', () => {
  25. mockGetItem.mockReturnValue(null);
  26. const hook = reactHooks.renderHook(useDismissAlert, {
  27. initialProps: {key},
  28. });
  29. const {result} = hook;
  30. expect(result.current.isDismissed).toBeFalsy();
  31. });
  32. it('should be dismissed if there is any value in localstorage and no expiration', () => {
  33. mockGetItem.mockReturnValue(JSON.stringify('some value'));
  34. const {result} = reactHooks.renderHook(useDismissAlert, {
  35. initialProps: {key},
  36. });
  37. expect(result.current.isDismissed).toBeTruthy();
  38. });
  39. it('should set the current timestamp into localstorage when an alert is dismissed', async () => {
  40. const {result, waitFor} = reactHooks.renderHook(useDismissAlert, {
  41. initialProps: {key},
  42. });
  43. reactHooks.act(() => {
  44. result.current.dismiss();
  45. });
  46. await waitFor(() =>
  47. expect(mockSetItem).toHaveBeenCalledWith(
  48. key,
  49. JSON.stringify(now.getTime().toString())
  50. )
  51. );
  52. });
  53. it('should be dismissed if the timestamp in localStorage is older than the expiration', () => {
  54. const today = new Date('2020-01-01');
  55. setMockDate(today);
  56. // Dismissed on christmas
  57. const christmas = new Date('2019-12-25').getTime();
  58. mockGetItem.mockReturnValue(JSON.stringify(christmas));
  59. // Expires after 2 days
  60. const {result} = reactHooks.renderHook(useDismissAlert, {
  61. initialProps: {key, expirationDays: 2},
  62. });
  63. // Dismissal has expired
  64. expect(result.current.isDismissed).toBeFalsy();
  65. });
  66. it('should not be dismissed if the timestamp in localstorage is more recent than the expiration', () => {
  67. // Dismissed on christmas
  68. const christmas = new Date('2019-12-25').getTime();
  69. mockGetItem.mockReturnValue(JSON.stringify(christmas));
  70. // Expires after 30 days
  71. const {result} = reactHooks.renderHook(useDismissAlert, {
  72. initialProps: {key, expirationDays: 30},
  73. });
  74. // Not expired, dismissal is still active
  75. expect(result.current.isDismissed).toBeTruthy();
  76. });
  77. it('should not be dismissed if the value in localstorage is not a number/timestamp', () => {
  78. mockGetItem.mockReturnValue(JSON.stringify('foobar'));
  79. const {result} = reactHooks.renderHook(useDismissAlert, {
  80. initialProps: {key, expirationDays: 30},
  81. });
  82. expect(result.current.isDismissed).toBeFalsy();
  83. });
  84. });