{"size": 1270, "ext": "js", "max_stars_repo_path": "server/services/tests/circuitbreaker.test.js", "max_stars_repo_name": "ritwik-wednesday/pdf-test-api", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/services/tests/circuitbreaker.test.js", "max_issues_repo_name": "ritwik-wednesday/pdf-test-api", "max_issues_repo_issues_event_min_datetime": "2022-03-14T10:21:07.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-14T10:21:07.000Z", "max_forks_repo_path": "server/services/tests/circuitbreaker.test.js", "max_forks_repo_name": "ritwik-wednesday/pdf-test-api", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 35.2777777778, "max_line_length": 102, "alphanum_fraction": 0.6472440945}
import { newCircuitBreaker } from '../circuitbreaker';

describe('newCircuitBreaker', () => {
  const fallbackMessage = 'Some fallback message';
  it('should return the response from the API', async () => {
    const data = 'this is some API response';
    const somefunc = async () => ({
      data
    });
    const testme = 'testme';
    const breaker = newCircuitBreaker(somefunc, fallbackMessage);
    const res = await breaker.fire(testme);
    expect(res.data).toBe(data);
  });
  it('should return the fallback message if the API throws an error', async () => {
    const customError = 'This is some error';

    const somefunc = async () => {
      throw new Error(customError);
    };
    const testme = 'testme';
    const breaker = newCircuitBreaker(somefunc, fallbackMessage);
    const res = await breaker.fire(testme);
    expect(res).toBe(`${fallbackMessage}. ${customError}`);
  });
  it('should return the fallback message if the API throws an error without a message.', async () => {
    const somefunc = async () => {
      throw new Error();
    };
    const testme = 'testme';
    const breaker = newCircuitBreaker(somefunc, fallbackMessage);
    const res = await breaker.fire(testme);
    expect(res).toBe(`${fallbackMessage}. Error`);
  });
});
