123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- import {reactHooks} from 'sentry-test/reactTestingLibrary';
- import {setMockDate} from 'sentry-test/utils';
- import localStorage from 'sentry/utils/localStorage';
- import useDismissAlert from 'sentry/utils/useDismissAlert';
- jest.mock('sentry/utils/localStorage');
- const mockSetItem = jest.mocked(localStorage.setItem);
- const mockGetItem = jest.mocked(localStorage.getItem);
- const key = 'test_123';
- const now = new Date('2020-01-01');
- describe('useDismissAlert', () => {
- beforeEach(() => {
- setMockDate(now);
- mockSetItem.mockReset();
- mockGetItem.mockReset();
- });
- it('should return a stable ref for the dismiss() function', () => {
- const {result, rerender} = reactHooks.renderHook(useDismissAlert, {
- initialProps: {key},
- });
- const initialRef = result.current.dismiss;
- rerender({key});
- expect(result.current.dismiss).toEqual(initialRef);
- });
- it('should not be dismissed if there is no value in localstorage', () => {
- mockGetItem.mockReturnValue(null);
- const hook = reactHooks.renderHook(useDismissAlert, {
- initialProps: {key},
- });
- const {result} = hook;
- expect(result.current.isDismissed).toBeFalsy();
- });
- it('should be dismissed if there is any value in localstorage and no expiration', () => {
- mockGetItem.mockReturnValue(JSON.stringify('some value'));
- const {result} = reactHooks.renderHook(useDismissAlert, {
- initialProps: {key},
- });
- expect(result.current.isDismissed).toBeTruthy();
- });
- it('should set the current timestamp into localstorage when an alert is dismissed', async () => {
- const {result, waitFor} = reactHooks.renderHook(useDismissAlert, {
- initialProps: {key},
- });
- reactHooks.act(() => {
- result.current.dismiss();
- });
- await waitFor(() =>
- expect(mockSetItem).toHaveBeenCalledWith(
- key,
- JSON.stringify(now.getTime().toString())
- )
- );
- });
- it('should be dismissed if the timestamp in localStorage is older than the expiration', () => {
- const today = new Date('2020-01-01');
- setMockDate(today);
- // Dismissed on christmas
- const christmas = new Date('2019-12-25').getTime();
- mockGetItem.mockReturnValue(JSON.stringify(christmas));
- // Expires after 2 days
- const {result} = reactHooks.renderHook(useDismissAlert, {
- initialProps: {key, expirationDays: 2},
- });
- // Dismissal has expired
- expect(result.current.isDismissed).toBeFalsy();
- });
- it('should not be dismissed if the timestamp in localstorage is more recent than the expiration', () => {
- // Dismissed on christmas
- const christmas = new Date('2019-12-25').getTime();
- mockGetItem.mockReturnValue(JSON.stringify(christmas));
- // Expires after 30 days
- const {result} = reactHooks.renderHook(useDismissAlert, {
- initialProps: {key, expirationDays: 30},
- });
- // Not expired, dismissal is still active
- expect(result.current.isDismissed).toBeTruthy();
- });
- it('should not be dismissed if the value in localstorage is not a number/timestamp', () => {
- mockGetItem.mockReturnValue(JSON.stringify('foobar'));
- const {result} = reactHooks.renderHook(useDismissAlert, {
- initialProps: {key, expirationDays: 30},
- });
- expect(result.current.isDismissed).toBeFalsy();
- });
- });
|