useDismissAlert.spec.tsx 3.3 KB

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