Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
error.middleware.test.ts 2.54 KiB
import { handleError } from '@/utils/error-report/errorReporting';
import { errorMiddlewareListener } from './error.middleware';

jest.mock('../../utils/error-report/errorReporting', () => ({
  handleError: jest.fn(),
}));

describe('errorMiddlewareListener', () => {
  // eslint-disable-next-line no-console

  beforeEach(() => {
    jest.clearAllMocks();
  });

  it('should show toast with error message when action is rejected with value', async () => {
    const action = {
      type: 'action/rejected',
      payload: 'Error message',
      meta: {
        requestId: '421',
        rejectedWithValue: true,
        requestStatus: 'rejected',
      },
      error: {
        code: 'Error 2',
      },
    };
    await errorMiddlewareListener(action);
    expect(handleError).toHaveBeenCalledWith({ code: 'Error 2' });
  });

  it('should show toast with unknown error when action is rejected without value', async () => {
    const action = {
      type: 'action/rejected',
      payload: null,
      meta: {
        requestId: '421',
        rejectedWithValue: true,
        requestStatus: 'rejected',
      },
      error: {
        code: 'Error 3',
      },
    };
    await errorMiddlewareListener(action);
    expect(handleError).toHaveBeenCalledWith({ code: 'Error 3' });
  });

  it('should not show toast when action is not rejected', async () => {
    const action = {
      type: 'action/loading',
      payload: null,
      meta: {
        requestId: '421',
        requestStatus: 'fulfilled',
      },
    };
    await errorMiddlewareListener(action);
    expect(handleError).not.toHaveBeenCalled();
  });

  it('should show toast with unknown error when action payload is not a string', async () => {
    const action = {
      type: 'action/rejected',
      payload: {},
      meta: {
        requestId: '421',
        rejectedWithValue: true,
        requestStatus: 'rejected',
      },
      error: {
        code: 'ERROR',
        message: 'Error message',
      },
    };
    await errorMiddlewareListener(action);
    expect(handleError).toHaveBeenCalledWith({ code: 'ERROR', message: 'Error message' });
  });

  it('should show toast with custom message when action payload is a string', async () => {
    const action = {
      type: 'action/rejected',
      payload: 'Failed to fetch',
      meta: {
        requestId: '421',
        rejectedWithValue: true,
        requestStatus: 'rejected',
      },
      error: {
        code: 'ERROR',
      },
    };
    await errorMiddlewareListener(action);
    expect(handleError).toHaveBeenCalledWith({ code: 'ERROR' });
  });
});