<commit_before>/* eslint-disable import/no-extraneous-dependencies */
/* eslint-env mocha */
import sinon from 'sinon';
import { expect } from 'chai';
import Fetch from '../src/Fetch';

describe('Fetch', () => {
  let stubFetch;

  const textPromise = Promise.resolve({
    status: 200,
    headers: {
      get: () => 'text/plain',
    },
    text: () => Promise.resolve(),
  });

  const jsonPromise = Promise.resolve({
    status: 200,
    headers: {
      get: () => 'application/json',
    },
    json: () => Promise.resolve(),
  });

  afterEach(() => {
    if (stubFetch) {
      stubFetch.restore();
    }
  });

  it('should return a promise', () => {
    global.fetch = () => Promise.resolve({
      status: 200,
      headers: {
        get: () => '',
      },
      text: () => Promise.resolve(''),
    });

    const result = Fetch.get('/');

    expect(result).to.be.instanceof(Promise);
  });

  it('should return text when asked', () => {
    global.fetch = () => Promise.resolve({
      status: 200,
      headers: {
        get: () => 'text/plain',
      },
      text: () => Promise.resolve('Test Mocha'),
    });

    return Fetch.get('/')
      .then(data => expect(data).to.eql('Test Mocha'));
  });

  it('should return json when asked', () => {
    global.fetch = () => Promise.resolve({
      status: 200,
      headers: {
        get: () => 'application/json',
      },
      json: () => Promise.resolve({
        result: 'Test Mocha',
      }),
    });

    return Fetch.get('/')
      .then(data => expect(data).to.eql({
        result: 'Test Mocha',
      }));
  });

  it('should return error when 400 or more', () => {
    global.fetch = () => Promise.resolve({
      status: 400,
      headers: {
        get: () => 'text/plain',
      },
      text: () => Promise.resolve('Test Mocha Error'),
    });

    return Fetch.get('/')
      .then(() => expect(false).to.be.true)
      .catch((data) => {
        expect(data.content).to.eql('Test Mocha Error');
        expect(String(data)).to.eql('Test Mocha Error');
      });
  });

  it('should return jsonError when 400 or more', () => {
    global.fetch = () => Promise.resolve({
      status: 500,
      headers: {
        get: () => 'application/json',
      },
      json: () => Promise.resolve({
        error: 'Test Mocha Error',
      }),
    });

    return Fetch.get('/')
      .then(() => expect(false).to.be.true)
      .catch((data) => {
        expect(data.content).to.eql({ error: 'Test Mocha Error' });
        expect(String(data)).to.eql('{"error":"Test Mocha Error"}');
      });
  });

  it('should return error when text fail', () => {
    global.fetch = () => Promise.resolve({
      status: 200,
      headers: {
        get: () => 'text/plain',
      },
      text: () => Promise.reject(new Error('Mocha Text Error')),
    });

    return Fetch.get('/')
      .then(() => expect(false).to.be.true)
      .catch(data => expect(String(data)).to.eql('Error: Mocha Text Error'));
  });

  it('should return error when json fail', () => {
    global.fetch = () => Promise.resolve({
      status: 200,
      headers: {
        get: () => 'application/json',
      },
      json: () => Promise.reject(new Error('Mocha JSON Error')),
    });

    return Fetch.get('/')
      .then(() => expect(false).to.be.true)
      .catch(data => expect(String(data)).to.eql('Error: Mocha JSON Error'));
  });

  it('should pass header', () => {
    stubFetch = sinon.stub(global, 'fetch').callsFake(() => textPromise);

    return Fetch.url('/').header('custom', 'test').get()
      .then(() => expect(stubFetch.calledWith('/', {
        headers: {
          custom: 'test',
        },
        method: 'GET',
      })).to.be.true);
  });

  it('should pass auth', () => {
    stubFetch = sinon.stub(global, 'fetch').callsFake(() => textPromise);

    return Fetch.url('/').auth('token').get()
      .then(() => expect(stubFetch.calledWith('/', {
        headers: {
          Authorization: 'token',
        },
        method: 'GET',
      })).to.be.true);
  });

  it('should pass contentType for json', () => {
    stubFetch = sinon.stub(global, 'fetch', () => jsonPromise);
<commit_msg>Update Fetch.test.js<commit_after>/* eslint-disable import/no-extraneous-dependencies */
/* eslint-env mocha */
import sinon from 'sinon';
import { expect } from 'chai';
import Fetch from '../src/Fetch';

describe('Fetch', () => {
  let stubFetch;

  const textPromise = Promise.resolve({
    status: 200,
    headers: {
      get: () => 'text/plain',
    },
    text: () => Promise.resolve(),
  });

  const jsonPromise = Promise.resolve({
    status: 200,
    headers: {
      get: () => 'application/json',
    },
    json: () => Promise.resolve(),
  });

  afterEach(() => {
    if (stubFetch) {
      stubFetch.restore();
    }
  });

  it('should return a promise', () => {
    global.fetch = () => Promise.resolve({
      status: 200,
      headers: {
        get: () => '',
      },
      text: () => Promise.resolve(''),
    });

    const result = Fetch.get('/');

    expect(result).to.be.instanceof(Promise);
  });

  it('should return text when asked', () => {
    global.fetch = () => Promise.resolve({
      status: 200,
      headers: {
        get: () => 'text/plain',
      },
      text: () => Promise.resolve('Test Mocha'),
    });

    return Fetch.get('/')
      .then(data => expect(data).to.eql('Test Mocha'));
  });

  it('should return json when asked', () => {
    global.fetch = () => Promise.resolve({
      status: 200,
      headers: {
        get: () => 'application/json',
      },
      json: () => Promise.resolve({
        result: 'Test Mocha',
      }),
    });

    return Fetch.get('/')
      .then(data => expect(data).to.eql({
        result: 'Test Mocha',
      }));
  });

  it('should return error when 400 or more', () => {
    global.fetch = () => Promise.resolve({
      status: 400,
      headers: {
        get: () => 'text/plain',
      },
      text: () => Promise.resolve('Test Mocha Error'),
    });

    return Fetch.get('/')
      .then(() => expect(false).to.be.true)
      .catch((data) => {
        expect(data.content).to.eql('Test Mocha Error');
        expect(String(data)).to.eql('Test Mocha Error');
      });
  });

  it('should return jsonError when 400 or more', () => {
    global.fetch = () => Promise.resolve({
      status: 500,
      headers: {
        get: () => 'application/json',
      },
      json: () => Promise.resolve({
        error: 'Test Mocha Error',
      }),
    });

    return Fetch.get('/')
      .then(() => expect(false).to.be.true)
      .catch((data) => {
        expect(data.content).to.eql({ error: 'Test Mocha Error' });
        expect(String(data)).to.eql('{"error":"Test Mocha Error"}');
      });
  });

  it('should return error when text fail', () => {
    global.fetch = () => Promise.resolve({
      status: 200,
      headers: {
        get: () => 'text/plain',
      },
      text: () => Promise.reject(new Error('Mocha Text Error')),
    });

    return Fetch.get('/')
      .then(() => expect(false).to.be.true)
      .catch(data => expect(String(data)).to.eql('Error: Mocha Text Error'));
  });

  it('should return error when json fail', () => {
    global.fetch = () => Promise.resolve({
      status: 200,
      headers: {
        get: () => 'application/json',
      },
      json: () => Promise.reject(new Error('Mocha JSON Error')),
    });

    return Fetch.get('/')
      .then(() => expect(false).to.be.true)
      .catch(data => expect(String(data)).to.eql('Error: Mocha JSON Error'));
  });

  it('should pass header', () => {
    stubFetch = sinon.stub(global, 'fetch').callsFake(() => textPromise);

    return Fetch.url('/').header('custom', 'test').get()
      .then(() => expect(stubFetch.calledWith('/', {
        headers: {
          custom: 'test',
        },
        method: 'GET',
      })).to.be.true);
  });

  it('should pass auth', () => {
    stubFetch = sinon.stub(global, 'fetch').callsFake(() => textPromise);

    return Fetch.url('/').auth('token').get()
      .then(() => expect(stubFetch.calledWith('/', {
        headers: {
          Authorization: 'token',
        },
        method: 'GET',
      })).to.be.true);
  });

  it('should pass contentType for json', () => {
    stubFetch = sinon.stub(global, 'fetch').callsFake(() => jsonPromise);

{'fraction_non_alphanumeric': 0.21443101591480196, 'fraction_numerical': 0.00789757089864784, 'mean_word_length': 2.285377358490566, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 107, 'https://': 0, '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': '1828341', 'n_tokens_mistral': 2751, 'n_tokens_neox': 2635, 'n_words': 529}