Untitled

mail@pastecode.io avatar
unknown
javascript
a year ago
4.1 kB
1
Indexable
Never
describe('getUrl', () => {
  afterEach(() => {
    jest.clearAllMocks();
  });

  // ... Existing test cases ...

  it('should handle missing client_required_params', async () => {
    // Arrange
    const reqBody = {
      url1: {
        url_name: 'valid_url1',
        url_data: {}, // Missing required data
      },
    };
    const ctx = createMockContext(reqBody);
    // Mock the 'generateUrl' function to avoid unnecessary calls

    // Act
    const result = await moduleToTest.getUrl(ctx);

    // Assert
    expect(result).toEqual({
      data: {
        url_details: [
          {
            url_name: 'valid_url1',
            status: moduleToTest.constants.STATUS.FAILED,
            error: 'Missing required reqBody for valid_url1: required_param1',
          },
        ],
        response_status: moduleToTest.constants.STATUS.FAILED,
      },
      status: HTTPStatus.OK,
    });
  });

  it('should handle invalid url_data format', async () => {
    // Arrange
    const reqBody = {
      url1: {
        url_name: 'valid_url1',
        url_data: 'invalid_data', // Invalid format, should be an object
      },
    };
    const ctx = createMockContext(reqBody);
    // Mock the 'generateUrl' function to avoid unnecessary calls

    // Act
    const result = await moduleToTest.getUrl(ctx);

    // Assert
    expect(result).toEqual({
      data: {
        url_details: [
          {
            url_name: 'valid_url1',
            status: moduleToTest.constants.STATUS.FAILED,
            error: 'url_data must be an object',
          },
        ],
        response_status: moduleToTest.constants.STATUS.FAILED,
      },
      status: HTTPStatus.OK,
    });
  });

  it('should handle unsupported url_name', async () => {
    // Arrange
    const reqBody = {
      url1: {
        url_name: 'unsupported_url',
        url_data: { key: 'value' },
      },
    };
    const ctx = createMockContext(reqBody);
    // Mock the 'generateUrl' function to avoid unnecessary calls

    // Act
    const result = await moduleToTest.getUrl(ctx);

    // Assert
    expect(result).toEqual({
      data: {
        url_details: [
          {
            url_name: 'unsupported_url',
            status: moduleToTest.constants.STATUS.FAILED,
            error: 'Unsupported url_name :unsupported_url',
          },
        ],
        response_status: moduleToTest.constants.STATUS.FAILED,
      },
      status: HTTPStatus.OK,
    });
  });

  it('should handle partial success when some URLs are successful and others are not', async () => {
    // Arrange
    const reqBody = {
      url1: {
        url_name: 'valid_url1',
        url_data: { key: 'value' },
      },
      url2: {
        url_name: 'invalid_url',
        url_data: { key: 'value' },
      },
    };
    const ctx = createMockContext(reqBody);
    jest.spyOn(moduleToTest, 'generateUrl').mockResolvedValueOnce('http://example.com');
    // Mock other required functions as needed

    // Act
    const result = await moduleToTest.getUrl(ctx);

    // Assert
    expect(result).toEqual({
      data: {
        url_details: [
          { url_name: 'valid_url1', status: moduleToTest.constants.STATUS.SUCCESS, url: 'http://example.com' },
          {
            url_name: 'invalid_url',
            status: moduleToTest.constants.STATUS.FAILED,
            error: 'Unsupported url_name :invalid_url',
          },
        ],
        response_status: moduleToTest.constants.STATUS_PARTIAL.PARTIAL_SUCCESS,
      },
      status: HTTPStatus.OK,
    });
  });

  it('should handle downstreamCalls error', async () => {
    // Arrange
    const reqBody = {
      url1: {
        url_name: 'valid_url1',
        url_data: { key: 'value' },
      },
    };
    const ctx = createMockContext(reqBody);
    jest.spyOn(moduleToTest.helpers, 'downstreamCalls').mockRejectedValueOnce(new Error('Downstream error'));
    // Mock other required functions as needed

    // Act and Assert
    await expect(moduleToTest.getUrl(ctx)).rejects.toThrowError(ZodeError);
  });

  // Add more test cases as needed to improve code coverage and handle different scenarios.

});