{"size": 1625, "ext": "js", "max_stars_repo_path": "server/tests/lib/requests/createRequestHelper.test.js", "max_stars_repo_name": "zamd/selfservice-password-reset", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/tests/lib/requests/createRequestHelper.test.js", "max_issues_repo_name": "zamd/selfservice-password-reset", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/tests/lib/requests/createRequestHelper.test.js", "max_forks_repo_name": "zamd/selfservice-password-reset", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 26.6393442623, "max_line_length": 74, "alphanum_fraction": 0.5421538462}
import nock from 'nock';
import requestHelper from '../../../src/lib/requests/createRequestHelper';

describe('CreateRequestHandler', () => {
  test('should handle network errors correctly', (done) => {
    requestHelper('foo', {})
      .catch((err) => {
        expect(err).toBeDefined();
        expect(err.code).toBeDefined();
        expect(err.code).toBe('ENOTFOUND');
        done();
        nock.cleanAll();
      });
  });

  test('should handle unauthorized errors correctly', (done) => {
    nock('http://test.com')
      .post('/token')
      .reply(401, 'Unauthorized');

    requestHelper('http://test.com/token', {})
      .catch((err) => {
        expect(err).toBeDefined();
        expect(err.status).toBeDefined();
        expect(err.status).toBe(401);
        done();
        nock.cleanAll();
      });
  });

  test('should handle forbidden errors correctly', (done) => {
    nock('http://test.com')
      .post('/token')
      .reply(403, 'Forbidden');

    requestHelper('http://test.com/token', {})
      .catch((err) => {
        expect(err).toBeDefined();
        expect(err.status).toBeDefined();
        expect(err.status).toBe(403);
        done();
        nock.cleanAll();
      });
  });

  test('should return body when request is successful', (done) => {
    nock('http://test.com')
      .post('/token')
      .reply(200, { value: 'foo' });

    requestHelper('http://test.com/token', {})
      .then((data) => {
        expect(data).toBeDefined();
        expect(data.value).toBeDefined();
        expect(data.value).toBe('foo');
        done();
        nock.cleanAll();
      });
  });
});
