import { AxiosError } from 'axios';

import {
  isArticlesAccessDeniedError,
  isGroupsAccessDeniedError,
} from 'src/hooks/useGeneralError.helpers';
import { ResponseCode } from 'src/types/services';

const createAxiosError = ({
  status = 403,
  code = ResponseCode.ACCESS_DENIED,
  url = '/groups/123',
}: {
  status?: number;
  code?: string;
  url?: string;
}) =>
  ({
    config: { url },
    response: {
      status,
      data: { code },
    },
  }) as AxiosError<{ code?: string }>;

describe('isArticlesAccessDeniedError', () => {
  test('returns true for articles endpoints with ACCESS_DENIED 403', () => {
    expect(
      isArticlesAccessDeniedError(createAxiosError({ url: '/articles/123' })),
    ).toBe(true);
    expect(
      isArticlesAccessDeniedError(createAxiosError({ url: 'articles/123' })),
    ).toBe(true);
  });

  test('returns false for non-articles urls', () => {
    expect(
      isArticlesAccessDeniedError(createAxiosError({ url: '/groups/123' })),
    ).toBe(false);
  });

  test('returns false for non-403 status', () => {
    expect(
      isArticlesAccessDeniedError(
        createAxiosError({ status: 500, url: '/articles/123' }),
      ),
    ).toBe(false);
  });

  test('returns false for non ACCESS_DENIED code', () => {
    expect(
      isArticlesAccessDeniedError(
        createAxiosError({
          code: ResponseCode.TOKEN_EXPIRED,
          url: '/articles/123',
        }),
      ),
    ).toBe(false);
  });
});

describe('isGroupsAccessDeniedError', () => {
  test('returns true for groups endpoints with ACCESS_DENIED 403', () => {
    expect(
      isGroupsAccessDeniedError(createAxiosError({ url: '/groups/123' })),
    ).toBe(true);
    expect(
      isGroupsAccessDeniedError(createAxiosError({ url: 'groups/123' })),
    ).toBe(true);
  });

  test('returns false for non-groups urls', () => {
    expect(
      isGroupsAccessDeniedError(createAxiosError({ url: '/users/123' })),
    ).toBe(false);
  });

  test('returns false for non-403 status', () => {
    expect(
      isGroupsAccessDeniedError(
        createAxiosError({ status: 500, url: '/groups/123' }),
      ),
    ).toBe(false);
  });

  test('returns false for non ACCESS_DENIED code', () => {
    expect(
      isGroupsAccessDeniedError(
        createAxiosError({
          code: ResponseCode.TOKEN_EXPIRED,
          url: '/groups/123',
        }),
      ),
    ).toBe(false);
  });
});
