项目作者: Larney11

项目描述 :
Mock http requests and responses using fetch API (or isomorphic-fetch). Straight forward functions makes it simple to create customizable and legible unit tests.
高级语言: JavaScript
项目地址: git://github.com/Larney11/mock-fetch-api.git
创建时间: 2016-07-07T10:24:22Z
项目社区:https://github.com/Larney11/mock-fetch-api

开源协议:Other

下载


mock-fetch-api

Mock http requests and responses using fetch API (or isomorphic-fetch). Straight forward functions makes it simple to create customizable and legible unit tests.

Installation

  1. npm install --save-dev mock-fetch-api

Usage

  1. var MockFetch = require('mock-fetch-api');

Functions

when()

The when() function sets the required method and URL.

  1. when(method, URL)
  2. when('GET', 'http://mydomain.com')

withExpectedHeader()

The withExpectedHeader() function sets the required headers.

  1. withExpectedHeader(Header-Field-Name, Header-Field-Type)
  2. 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.

  1. otherwiseRespondWith(status, statusText)
  2. 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.

  1. respondWith(status, data)
  2. respondWith(401, '{"data":[{"text":"Hello"},{"text":"Goodbye"}]}')

failNextCall()

The failNextCall() function forces the fetch to reject.

  1. failNextCall()

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

The following examples are unit tests using Jest.

  1. pit("can set a condition which is returned by fetch", () => {
  2. var MockFetch = require('../MockFetch.js');
  3. MockFetch.when('GET', 'http://mydomain.com').respondWith(200, '"Hello World"');
  4. return fetch('GET', 'http://mydomain.com').then((response) => {
  5. return response.json();
  6. }).then((data) => {
  7. expect(data).toBe('Hello World');
  8. });
  9. });
  10. pit("only responds when matched correctly", () => {
  11. var MockFetch = require('mock-fetch-api');
  12. MockFetch.when('GET', 'http://mydomain.com').respondWith(200, '"Hello World"');
  13. return fetch('http://mydomain.com', { method: 'PUT'}).then((response) => {
  14. expect(response.status).toBe(404);
  15. expect(response.statusText).toBe('Not Found');
  16. });
  17. });
  18. pit("also checks for an expected header value", () => {
  19. var MockFetch = require('../MockFetch.js');
  20. MockFetch.when('GET', 'http://mydomain.com')
  21. .withExpectedHeader('X-AuthToken','1234')
  22. .otherwiseRespondWith(401, "Not Authorized")
  23. .respondWith(200, '"Hello World"');
  24. return fetch('http://mydomain.com', { method: 'GET', headers: new Headers({
  25. 'X-AuthToken':'1234'
  26. })}).then((response) => {
  27. expect(response.status).toBe(200);
  28. });
  29. });
  30. pit("fails when expected header is not set", () => {
  31. var MockFetch = require('../MockFetch.js');
  32. MockFetch.when('GET', 'http://mydomain.com')
  33. .withExpectedHeader({'X-AuthToken':'1234'}).otherwiseRespondWith(401, "Not Authorized")
  34. .respondWith(200, '"Hello World"');
  35. return fetch('http://mydomain.com', { method: 'GET'}).then((response) => {
  36. expect(response.status).toBe(401);
  37. expect(response.statusText).toBe('Not Authorized');
  38. });
  39. });
  40. pit("can check for multiple expected headers", () => {
  41. var MockFetch = require('../MockFetch.js');
  42. MockFetch.when('GET', 'http://mydomain.com')
  43. .withExpectedHeader('X-AuthToken','1234').otherwiseRespondWith(401, "Not Authorized")
  44. .withExpectedHeader('Content-Type', 'application/json').otherwiseRespondWith(404, "Not Found")
  45. .respondWith(200, '"Hello World"');
  46. return fetch('http://mydomain.com', { method: 'GET', headers: new Headers({
  47. 'X-AuthToken':'1234',
  48. 'Content-Type': 'application/json'
  49. })}).then((response) => {
  50. expect(response.status).toBe(200);
  51. });
  52. });
  53. pit("rejects the promise when simulating a failed network connection", () => {
  54. var MockFetch = require('../MockFetch.js');
  55. MockFetch.when('GET', 'http://mydomain.com')
  56. .respondWith(200, '"Hello World"');
  57. MockFetch.failNextCall();
  58. return fetch('http://mydomain.com').then((response) => {
  59. expect(false).toBe(true);
  60. }, (error) => {
  61. expect(true).toBe(true);
  62. });
  63. });