<commit_before># mock-fetch-api
<<<<<<< HEAD
This is a mock fetch API created for unit testing communication between a server

## Installation
    npm install --save-dev mock-fetch-api

## Usage
    var MockFetch = require('mock-fetch-api');

### Functions

#### when()
The when() function sets the required method and URL.  
```js
when(method, URL)    
when('GET', 'http://mydomain.com')    
```

#### withExpectedHeader()
The withExpectedHeader() function sets the required headers.  
```js
withExpectedHeader(Header-Field-Name, Header-Field-Type)  
withExpectedHeader('Content-Type', 'application/json')  
```


#### otherwiseRespondWith()
The otherwiseRespondWith() function sets the Response if the header does not match the header set in the withExpectedHeader() function  
```js
otherwiseRespondWith(status, statusText)  
otherwiseRespondWith(401, 'not authorised')  
```

#### respondWith()
The respondWith() function sets the Response if all the requirements specified with the when() and withExpectedHeader() functions correspond with what is passed to the fetch().  
```js
respondWith(status, data)  
respondWith(401, '{"data":[{"text":"Hello"},{"text":"Goodbye"}]}')  
```

#### failNextCall()
The failNextCall() function forces the fetch to reject().  
```js
failNextCall()
```

## Examples
<strong>Check out the '__tests__' directory to view all examples. </strong> https://github.com/Larney11/mock-fetch-api/blob/master/__tests__/mock-fetch-api-test.js  

The following examples are unit tests using Jest.  

```js

pit("can set a condition which is returned by fetch", () => {
  var MockFetch  = require('../MockFetch.js');

  MockFetch.when('GET', 'http://mydomain.com').respondWith(200, '"Hello World"');

  return fetch('GET', 'http://mydomain.com').then((response) => {
     return response.json();

  }).then((data) => {
     expect(data).toBe('Hello World');
  });
});


pit("only responds when matched correctly", () => {
  var MockFetch  = require('mock-fetch-api');

  MockFetch.when('GET', 'http://mydomain.com').respondWith(200, '"Hello World"');

  return fetch('http://mydomain.com', { method: 'PUT'}).then((response) => {

  expect(response.status).toBe(404);
  expect(response.statusText).toBe('Not Found');
  });
});    


pit("also checks for an expected header value", () => {
   var MockFetch  = require('../MockFetch.js');

   MockFetch.when('GET', 'http://mydomain.com')
      .withExpectedHeader('X-AuthToken','<PASSWORD>')
      .otherwiseRespondWith(401, "Not Authorized")
      .respondWith(200, '"Hello World"');

   return fetch('http://mydomain.com', { method: 'GET', headers: new Headers({
      'X-AuthToken':'<PASSWORD>'
   })}).then((response) => {
      expect(response.status).toBe(200);
   });
});


pit("fails when expected header is not set", () => {
   var MockFetch  = require('../MockFetch.js');

   MockFetch.when('GET', 'http://mydomain.com')
      .withExpectedHeader({'X-AuthToken':'<PASSWORD>'}).otherwiseRespondWith(401, "Not Authorized")
      .respondWith(200, '"Hello World"');

   return fetch('http://mydomain.com', { method: 'GET'}).then((response) => {

      expect(response.status).toBe(401);
      expect(response.statusText).toBe('Not Authorized');
   });
});


pit("can check for multiple expected headers", () => {
   var MockFetch  = require('../MockFetch.js');

   MockFetch.when('GET', 'http://mydomain.com')
      .withExpectedHeader('X-AuthToken','1<PASSWORD>').otherwiseRespondWith(401, "Not Authorized")
      .withExpectedHeader('Content-Type', 'application/json').otherwiseRespondWith(404, "Not Found")
      .respondWith(200, '"Hello World"');

   return fetch('http://mydomain.com', { method: 'GET', headers: new Headers({
      'X-AuthToken':'<PASSWORD>',
      'Content-Type': 'application/json'
   })}).then((response) => {

      expect(response.status).toBe(200);
   });
});


pit("rejects the promise when simulating a failed network connection", () => {
   var MockFetch  = require('../MockFetch.js');

   MockFetch.when('GET', 'http://mydomain.com')
      .respondWith(200, '"Hello World"');

   MockFetch.failNextCall();
   return fetch('http://mydomain.com').then((response) => {
      expect(false).toBe(true);
   }, (error) => {
      expect(true).toBe(true);
   });
});
```
=======
gjgj
>>>>>>> 960029188f4489b5b53e199ea4b6e4ca84f3a184<commit_msg>README.md updated<commit_after># mock-fetch-api
Mock http requests and responses using fetch API (or [isomorphic-fetch](https://www.npmjs.com/package/isomorphic-fetch)). Straight forward functions makes it simple to create customizable and legible unit tests.


## Installation
    npm install --save-dev mock-fetch-api

## Usage
    var MockFetch = require('mock-fetch-api');

### Functions

#### when()
The when() function sets the required method and URL.  
```js
when(method, URL)    
when('GET', 'http://mydomain.com')    
```

#### withExpectedHeader()
The withExpectedHeader() function sets the required headers.  
```js
withExpectedHeader(Header-Field-Name, Header-Field-Type)  
withExpectedHeader('Content-Type', 'application/json')  
```


#### otherwiseRespondWith()
The otherwiseRespondWith() function sets the response if the header specified with the  withExpectedHeader() function does not correspond with the header passed to the fetch() function.  
```js
otherwiseRespondWith(status, statusText)  
otherwiseRespondWith(401, 'not authorised')  
```

#### respondWith()
The respondWith() function sets the response if all the requirements specified with the when() and withExpectedHeader() functions correspond with what is passed to the fetch() function.  
```js
respondWith(status, data)  
respondWith(401, '{"data":[{"text":"Hello"},{"text":"Goodbye"}]}')  
```

#### failNextCall()
The failNextCall() function forces the fetch to reject.  
```js
failNextCall()
```

## Examples
<strong>Check out the '__tests__' directory to view all examples. </strong> https://github.com/Larney11/mock-fetch-api/blob/master/tests/mock-fetch-api-test.js  

The following examples are unit tests using Jest.  

```js

pit("can set a condition which is returned by fetch", () => {
  var MockFetch  = require('../MockFetch.js');

  MockFetch.when('GET', 'http://mydomain.com').respondWith(200, '"Hello World"');

  return fetch('GET', 'http://mydomain.com').then((response) => {
     return response.json();

  }).then((data) => {
     expect(data).toBe('Hello World');
  });
});


pit("only responds when matched correctly", () => {
  var MockFetch  = require('mock-fetch-api');

  MockFetch.when('GET', 'http://mydomain.com').respondWith(200, '"Hello World"');

  return fetch('http://mydomain.com', { method: 'PUT'}).then((response) => {

  expect(response.status).toBe(404);
  expect(response.statusText).toBe('Not Found');
  });
});    


pit("also checks for an expected header value", () => {
   var MockFetch  = require('../MockFetch.js');

   MockFetch.when('GET', 'http://mydomain.com')
      .withExpectedHeader('X-AuthToken','<PASSWORD>')
      .otherwiseRespondWith(401, "Not Authorized")
      .respondWith(200, '"Hello World"');

   return fetch('http://mydomain.com', { method: 'GET', headers: new Headers({
      'X-AuthToken':'<PASSWORD>'
   })}).then((response) => {
      expect(response.status).toBe(200);
   });
});


pit("fails when expected header is not set", () => {
   var MockFetch  = require('../MockFetch.js');

   MockFetch.when('GET', 'http://mydomain.com')
      .withExpectedHeader({'X-AuthToken':'<PASSWORD>'}).otherwiseRespondWith(401, "Not Authorized")
      .respondWith(200, '"Hello World"');

   return fetch('http://mydomain.com', { method: 'GET'}).then((response) => {

      expect(response.status).toBe(401);
      expect(response.statusText).toBe('Not Authorized');
   });
});


pit("can check for multiple expected headers", () => {
   var MockFetch  = require('../MockFetch.js');

   MockFetch.when('GET', 'http://mydomain.com')
      .withExpectedHeader('X-AuthToken','<PASSWORD>').otherwiseRespondWith(401, "Not Authorized")
      .withExpectedHeader('Content-Type', 'application/json').otherwiseRespondWith(404, "Not Found")
      .respondWith(200, '"Hello World"');

   return fetch('http://mydomain.com', { method: 'GET', headers: new Headers({
      'X-AuthToken':'<PASSWORD>',
      'Content-Type': 'application/json'
   })}).then((response) => {

      expect(response.status).toBe(200);
   });
});


pit("rejects the promise when simulating a failed network connection", () => {
   var MockFetch  = require('../MockFetch.js');

   MockFetch.when('GET', 'http://mydomain.com')
      .respondWith(200, '"Hello World"');

   MockFetch.failNextCall();
   return fetch('http://mydomain.com').then((response) => {
      expect(false).toBe(true);
   }, (error) => {
      expect(true).toBe(true);
   });
});
```
{'fraction_non_alphanumeric': 0.18600953895071543, 'fraction_numerical': 0.014649102884397002, 'mean_word_length': 4.3118214716525936, 'pattern_counts': {'":': 6, '<': 24, '<?xml version=': 0, '>': 52, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, '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': '3533091', 'n_tokens_mistral': 2811, 'n_tokens_neox': 2610, 'n_words': 611}