diff --git a/README.md b/README.md index f6cd0a1..87e726c 100644 --- a/README.md +++ b/README.md @@ -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 Check out the '__tests__' directory to view all examples. https://github.com/Larney11/mock-fetch-api/blob/master/tests/mock-fetch-api-test.js @@ -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); + }); + }); + +}); +``` diff --git a/mock-fetch-api.js b/mock-fetch-api.js index 3a8ef3b..56b2207 100644 --- a/mock-fetch-api.js +++ b/mock-fetch-api.js @@ -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; @@ -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 = { diff --git a/package.json b/package.json index ce3061e..a4c8ef6 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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" } } diff --git a/tests/mock-fetch-api-test.js b/tests/mock-fetch-api-test.js index b05f9b4..9932c47 100644 --- a/tests/mock-fetch-api-test.js +++ b/tests/mock-fetch-api-test.js @@ -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'); @@ -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); + }); + }); + +});