<commit_before>/* eslint-env jasmine */

const nock = require('nock')

const makeRequest = require('../../../src/common/makeRequest')

describe('makeRequest', function () {
  describe('when a sucessful request is made', function () {
    describe('and valid data is returned', function () {
      it('should resolve with that data', function (done) {
        // Intercept request and return the stub data
        nock('https://data.police.uk/api')
          .get('/foobar')
          .reply(200, [{data: 'some-date'}])
        makeRequest('/foobar')
          .then((data) => { expect(data).toEqual([{data: 'some-date'}]); done() })
          .catch(() => done.fail(new Error('Promise should not be rejected')))
      })
    })
    describe('and invalid data is returned', function () {
      it('should reject with an error', function (done) {
        // Intercept request and return the stub data
        nock('https://data.police.uk/api')
          .get('/foobar')
          .reply(200, '<h1>Welcome to my site</h1>')
        makeRequest('/foobar')
          .then(() => done.fail(new Error('Promise should not be rejected')))
          .catch((error) => { expect(error).toEqual('Invalid JSON'); done() })
      })
    })
  })
  describe('when an unsuccessful request is made', function () {
    describe('and a non-200 status code is received', function () {
      it('should reject with an error', function (done) {
        nock('https://data.police.uk/api')
          .get('/foobar')
          .reply(500)
        makeRequest('/foobar')
          .then(() => done.fail(new Error('Promise should not be rejected')))
          .catch((error) => { expect(error).toEqual('Non-200 status code received (500)'); done() })
      })
    })
  })
})<commit_msg>Add test for when error is received<commit_after>/* eslint-env jasmine */

const nock = require('nock')

const makeRequest = require('../../../src/common/makeRequest')

describe('makeRequest', function () {
  describe('when a sucessful request is made', function () {
    describe('and valid data is returned', function () {
      it('should resolve with that data', function (done) {
        // Intercept request and return the stub data
        nock('https://data.police.uk/api')
          .get('/foobar')
          .reply(200, [{data: 'some-date'}])
        makeRequest('/foobar')
          .then((data) => { expect(data).toEqual([{data: 'some-date'}]); done() })
          .catch(() => done.fail(new Error('Promise should not be rejected')))
      })
    })
    describe('and invalid data is returned', function () {
      it('should reject with an error', function (done) {
        // Intercept request and return the stub data
        nock('https://data.police.uk/api')
          .get('/foobar')
          .reply(200, '<h1>Welcome to my site</h1>')
        makeRequest('/foobar')
          .then(() => done.fail(new Error('Promise should not be rejected')))
          .catch((error) => { expect(error).toEqual('Invalid JSON'); done() })
      })
    })
  })
  describe('when an unsuccessful request is made', function () {
    describe('and a non-200 status code is received', function () {
      it('should reject with an error', function (done) {
        nock('https://data.police.uk/api')
          .get('/foobar')
          .reply(500)
        makeRequest('/foobar')
          .then(() => done.fail(new Error('Promise should not be rejected')))
          .catch((error) => { expect(error).toEqual('Non-200 status code received (500)'); done() })
      })
    })
    describe('and an error is received', function () {
      it('should reject with an error', function (done) {
        nock('https://data.police.uk/api')
          .get('/foobar')
          .replyWithError({code: 'SOMETHING_BAD', message: 'Something bad happened'})
        makeRequest('/foobar')
          .then(() => done.fail(new Error('Promise should not be rejected')))
          .catch((error) => { expect(error).toEqual({code: 'SOMETHING_BAD', message: 'Something bad happened'}); done() })
      })
    })
  })
})
{'fraction_non_alphanumeric': 0.17645607107601186, 'fraction_numerical': 0.009871668311944718, 'mean_word_length': 2.6058718861209966, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 7, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5611304', 'n_tokens_mistral': 1190, 'n_tokens_neox': 1143, 'n_words': 311}