Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,39 @@ The failNextCall() function forces the fetch to reject.
failNextCall()
```

#### whenAll()
The whenAll() function sets multiple configured in array with objects.
```js
var when1 = {
url: 'http://mydomain.com/login',
method: 'POST',
headers: {
'X-AuthToken': 1234,
BANANA: 8757,
otherwiseRespondWith: {
status: 401,
statusText: 'Not Authorized'
}
},
response: {
status: 200,
statusText: 'Success'
}
};

// Default method is GET
var when2 = {
url: 'http://mydomain.com/home',
response: {
status: 200,
statusText: 'Success'
},
};

MockFetch.router([when1, when2]);
```


## 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

Expand Down Expand Up @@ -142,3 +175,113 @@ pit("rejects the promise when simulating a failed network connection", () => {
});
});
```

### Example using whenAll() in beforeAll()

```js
describe('MockFetch test using function whenAll()', () => {
var MockFetch = null;

beforeAll(() => {
MockFetch = require('mock-fetch-api');

var when = {
url: 'http://mydomain.com/home',
response: {
status: 200,
statusText: 'Success!',
}
};

var when2 = {
url: 'http://otherdomain.org',
method: 'POST',
headers: {
'X-AuthToken': 1234,
BANANA: 8757,
otherwiseRespondWith: { // Last Item!
status: 401,
statusText: 'Not Authorized'
}
},
response: {
status: 200,
statusText: 'Great!'
}
};

var when3 = {
url: 'http://anydomain.com.br',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9',
otherwiseRespondWith: { // Last Item!
status: 404,
statusText: 'Not Found'
}
},
response: {
status: 200,
statusText: 'YEAH!'
}
};

MockFetch.whenAll([when, when2, when3]);
});


pit("Should mydomain.com return a status 200", () => {
return fetch('http://mydomain.com/home').then((response) => {
expect(response.status).toBe(200);
});
});


pit("Should mydomain.com return a status 200 when connection is failed", () => {
MockFetch.failNextCall();

return fetch('http://mydomain.com/home').then((response) => {
expect(false).toBe(true);
}, (error) => {
expect(true).toBe(true);
});
});


pit("Should otherdomain.org return a status 200 when login authorized", () => {
var headers = new Headers({
'X-AuthToken': 1234,
BANANA: 8757
});

return fetch('http://otherdomain.org', { method: 'POST', headers: headers}).then((response) => {
expect(response.status).toBe(200);
});
});


pit("Should otherdomain.org return a status 401 when login not authorized", () => {
var headers = new Headers({
'X-AuthToken': 1234,
BANANA: 8758 // Password wrong
});

return fetch('http://otherdomain.org', { method: 'POST', headers: headers}).then((response) => {
expect(response.status).toBe(401);
});
});


pit("Should anydomain.com.br return a status 200 when request is json", () => {
var headers = new Headers({
'Content-Type': 'application/json',
'X-CSRF-Token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9'
});

return fetch('http://anydomain.com.br', {headers: headers}).then((response) => {
expect(response.status).toBe(200);
});
});

});
```
32 changes: 31 additions & 1 deletion mock-fetch-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ PERFORMANCE OF THIS SOFTWARE.
require('es6-promise').polyfill();
require('isomorphic-fetch');
var extend = require('object-extend');
var forEach = require('lodash.foreach');

var conditions = [];
var failNextCall = false;
Expand Down Expand Up @@ -95,11 +96,40 @@ global.fetch = function(uri, options) {
}));

});
}
};


var handlerRequest = function(request, fnWhen) {
var headers = (request.headers) ? request.headers : {};
var method = (request.method) ? request.method : 'GET';
var uri = request.url;
var when = fnWhen(method, uri);

forEach(headers, function(value, key) {
if (key !== 'otherwiseRespondWith') {
when.withExpectedHeader(key, value);
} else {
when.otherwiseRespondWith(value.status, value.statusText);
}
});

return when;
};


module.exports = {

whenAll: function(all = []) {

var parent = this;
var requests = all;

requests.forEach(function(req) {
handlerRequest(req, parent.when)
.respondWith(req.status, req.statusText);
});
},

when: function(method, uri) {

var condition = {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mock-fetch-api",
"version": "1.0.6",
"version": "1.1.0",
"description": "Mock http requests and responses using fetch API (or isomorphic-fetch). Straight forward functions makes it simple to create customizable and legible unit tests.",
"main": "mock-fetch-api.js",
"repository": {
Expand All @@ -26,6 +26,7 @@
"dependencies": {
"es6-promise": "^3.2.1",
"isomorphic-fetch": "^2.2.1",
"lodash.foreach": "^4.5.0",
"object-extend": "^0.5.0"
}
}
110 changes: 109 additions & 1 deletion tests/mock-fetch-api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ PERFORMANCE OF THIS SOFTWARE.

jest.autoMockOff();

describe('MockFetch test', () => {
describe('MockFetch test using function when()', () => {

pit("can set a condition which is returned by fetch", () => {
var MockFetch = require('../mock-fetch-api.js');
Expand Down Expand Up @@ -190,3 +190,111 @@ describe('MockFetch test', () => {
});

});



describe('MockFetch test using function whenAll()', () => {
var MockFetch = null;

beforeAll(() => {
MockFetch = require('mock-fetch-api');

var when = {
url: 'http://mydomain.com/home',
response: {
status: 200,
statusText: 'Success!',
}
};

var when2 = {
url: 'http://otherdomain.org',
method: 'POST',
headers: {
'X-AuthToken': 1234,
BANANA: 8757,
otherwiseRespondWith: {
status: 401,
statusText: 'Not Authorized'
}
},
response: {
status: 200,
statusText: 'Great!'
}
};

var when3 = {
url: 'http://anydomain.com.br',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9',
otherwiseRespondWith: {
status: 404,
statusText: 'Not Found'
}
},
response: {
status: 200,
statusText: 'YEAH!'
}
};

MockFetch.whenAll([when, when2, when3]);
});


pit("Should mydomain.com return a status 200", () => {
return fetch('http://mydomain.com/home').then((response) => {
expect(response.status).toBe(200);
});
});


pit("Should mydomain.com return a status 200 when connection is failed", () => {
MockFetch.failNextCall();

return fetch('http://mydomain.com/home').then((response) => {
expect(false).toBe(true);
}, (error) => {
expect(true).toBe(true);
});
});


pit("Should otherdomain.org return a status 200 when login authorized", () => {
var headers = new Headers({
'X-AuthToken': 1234,
BANANA: 8757
});

return fetch('http://otherdomain.org', { method: 'POST', headers: headers}).then((response) => {
expect(response.status).toBe(200);
});
});


pit("Should otherdomain.org return a status 401 when login not authorized", () => {
var headers = new Headers({
'X-AuthToken': 1234,
BANANA: 8758 // Password wrong
});

return fetch('http://otherdomain.org', { method: 'POST', headers: headers}).then((response) => {
expect(response.status).toBe(401);
});
});


pit("Should anydomain.com.br return a status 200 when request is json", () => {
var headers = new Headers({
'Content-Type': 'application/json',
'X-CSRF-Token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9'
});

return fetch('http://anydomain.com.br', {headers: headers}).then((response) => {
expect(response.status).toBe(200);
});
});

});