Skip to content
Snippets Groups Projects
error.middleware.test.ts 3.35 KiB
Newer Older
import { handleError } from '@/utils/error-report/errorReporting';
import { store } from '@/redux/store';
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 handle error with message when action is rejected', async () => {
    const action = {
      type: 'action/rejected',
      payload: 'Error message',
      meta: {
        requestId: '421',
        rejectedWithValue: true,
        requestStatus: 'rejected',
      },
      error: {
        code: 'Error 2',
      },
    const { getState } = store;
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-expect-error
    await errorMiddlewareListener(action, { getState });
    expect(handleError).toHaveBeenCalledWith({ code: 'Error 2' }, getState());
  it('should handle error without message 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',
      },
    const { getState } = store;
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-expect-error
    await errorMiddlewareListener(action, { getState });
    expect(handleError).toHaveBeenCalledWith({ code: 'Error 3' }, getState());
  it('should not handle error when action is not rejected', async () => {
    const action = {
      type: 'action/loading',
      payload: null,
      meta: {
        requestId: '421',
        requestStatus: 'fulfilled',
      },
    };
    const { getState } = store;
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-expect-error
    await errorMiddlewareListener(action, { getState });
    expect(handleError).not.toHaveBeenCalled();
  it('should handle error with unknown error message 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',
      },
    const { getState } = store;
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-expect-error
    await errorMiddlewareListener(action, { getState });
    expect(handleError).toHaveBeenCalledWith(
      { code: 'ERROR', message: 'Error message' },
      getState(),
    );
  it('should handle error 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',
        message: 'xyz',
    const { getState } = store;
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-expect-error
    await errorMiddlewareListener(action, { getState });
    expect(handleError).toHaveBeenCalledWith({ code: 'ERROR', message: 'xyz' }, getState());