From 4ac10a1c2e86ccc3eebbac3930b0c3621e61a900 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Paix=C3=A3o?= Date: Sun, 19 Nov 2017 17:01:32 -0200 Subject: [PATCH 1/6] Added function whenall() --- mock-fetch-api.js | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) 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 = { From 3a4de4aba54a8a8bcffd7dc8b4b0af80de9c5f14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Paix=C3=A3o?= Date: Sun, 19 Nov 2017 17:01:59 -0200 Subject: [PATCH 2/6] Installed module lodash.foreach --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index ce3061e..ba1042d 100644 --- a/package.json +++ b/package.json @@ -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" } } From f39819fda40a31b43e78b0359b985b4de353875c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Paix=C3=A3o?= Date: Sun, 19 Nov 2017 17:02:35 -0200 Subject: [PATCH 3/6] Added documentation of whenAll() --- README.md | 143 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) diff --git a/README.md b/README.md index f6cd0a1..a4a725f 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() from 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); + }); + }); + +}); +``` From 0d2bdc59c03023fe8f6263519a100843cc42b6fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Paix=C3=A3o?= Date: Sun, 19 Nov 2017 17:03:07 -0200 Subject: [PATCH 4/6] Added test of whenAll() --- tests/mock-fetch-api-test.js | 110 ++++++++++++++++++++++++++++++++++- 1 file changed, 109 insertions(+), 1 deletion(-) 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); + }); + }); + +}); From 038b6202603ddd0a8646906983f7e98dbbb5d0bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Paix=C3=A3o?= Date: Sun, 19 Nov 2017 17:07:38 -0200 Subject: [PATCH 5/6] Fix 'from' to 'in' --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a4a725f..87e726c 100644 --- a/README.md +++ b/README.md @@ -176,7 +176,7 @@ pit("rejects the promise when simulating a failed network connection", () => { }); ``` -### Example using whenAll() from beforeAll() +### Example using whenAll() in beforeAll() ```js describe('MockFetch test using function whenAll()', () => { From 2a5353fba15b9cfe2af2aa41ba4c4b3bd83287e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Paix=C3=A3o?= Date: Sun, 19 Nov 2017 17:14:41 -0200 Subject: [PATCH 6/6] Version MINOR to 1 and PACTH to 0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ba1042d..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": {