Niedzielski has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/394476 )

Change subject: Chore: add fetch wrapper tests
......................................................................

Chore: add fetch wrapper tests

Bug: T177681
Change-Id: If6702dfca8557127f889810145ae7adbe44d71e0
---
M package-lock.json
M package.json
A src/common/http/fetch-mock.test.ts
A src/common/http/fetch.test.ts
M src/common/http/fetch.ts
5 files changed, 100 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/marvin refs/changes/76/394476/1

diff --git a/package-lock.json b/package-lock.json
index 46592ab..2ee06b6 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -80,6 +80,12 @@
         "@types/webpack": "3.0.9"
       }
     },
+    "@types/fetch-mock": {
+      "version": "5.12.2",
+      "resolved": 
"https://registry.npmjs.org/@types/fetch-mock/-/fetch-mock-5.12.2.tgz";,
+      "integrity": 
"sha512-5YopFLn3TCnzQ1Z49xeKuOOcNGLlSNiWnEhcGA7uI6aSpZvb7vZHnpP0bbf2RNQ0P9sSgiHvr6IKj9nsTQR3sA==",
+      "dev": true
+    },
     "@types/history": {
       "version": "4.6.1",
       "resolved": 
"https://registry.npmjs.org/@types/history/-/history-4.6.1.tgz";,
@@ -2510,6 +2516,34 @@
         "websocket-driver": "0.7.0"
       }
     },
+    "fetch-mock": {
+      "version": "5.13.1",
+      "resolved": 
"https://registry.npmjs.org/fetch-mock/-/fetch-mock-5.13.1.tgz";,
+      "integrity": 
"sha512-eWUo2KI4sRGnRu8tKELCBfasALM5BfvrCxdI7J02j3eUM9mf+uYzJkURA0PSn/29JVapVrYFm+z+9XijXu1PdA==",
+      "dev": true,
+      "requires": {
+        "glob-to-regexp": "0.3.0",
+        "node-fetch": "1.7.3",
+        "path-to-regexp": "1.7.0"
+      },
+      "dependencies": {
+        "isarray": {
+          "version": "0.0.1",
+          "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz";,
+          "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=",
+          "dev": true
+        },
+        "path-to-regexp": {
+          "version": "1.7.0",
+          "resolved": 
"https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-1.7.0.tgz";,
+          "integrity": "sha1-Wf3g9DW62suhA6hOnTvGTpa5k30=",
+          "dev": true,
+          "requires": {
+            "isarray": "0.0.1"
+          }
+        }
+      }
+    },
     "figures": {
       "version": "2.0.0",
       "resolved": "https://registry.npmjs.org/figures/-/figures-2.0.0.tgz";,
@@ -2828,6 +2862,12 @@
         "is-glob": "2.0.1"
       }
     },
+    "glob-to-regexp": {
+      "version": "0.3.0",
+      "resolved": 
"https://registry.npmjs.org/glob-to-regexp/-/glob-to-regexp-0.3.0.tgz";,
+      "integrity": "sha1-jFoUlNIGbFcMw7/kSWF1rMTVAqs=",
+      "dev": true
+    },
     "global-dirs": {
       "version": "0.1.0",
       "resolved": 
"https://registry.npmjs.org/global-dirs/-/global-dirs-0.1.0.tgz";,
diff --git a/package.json b/package.json
index b4c35fb..783bb9b 100644
--- a/package.json
+++ b/package.json
@@ -60,6 +60,7 @@
     "@types/copy-webpack-plugin": "4.0.1",
     "@types/express": "4.0.39",
     "@types/extract-text-webpack-plugin": "3.0.0",
+    "@types/fetch-mock": "5.12.2",
     "@types/history": "4.6.1",
     "@types/mocha": "2.2.44",
     "@types/node": "8.0.47",
@@ -79,6 +80,7 @@
     "eslint-plugin-json": "1.2.0",
     "eslint-plugin-prettier": "2.3.1",
     "extract-text-webpack-plugin": "3.0.2",
+    "fetch-mock": "5.13.1",
     "history": "4.7.2",
     "husky": "0.14.3",
     "ignore-loader": "0.1.2",
diff --git a/src/common/http/fetch-mock.test.ts 
b/src/common/http/fetch-mock.test.ts
new file mode 100644
index 0000000..020eada
--- /dev/null
+++ b/src/common/http/fetch-mock.test.ts
@@ -0,0 +1,9 @@
+import * as Mock from "fetch-mock";
+
+interface FetchMockExtended extends Mock.FetchMockStatic {
+  sandbox(): Mock.FetchMockStatic & typeof fetch;
+}
+
+// todo: remove any usage:
+//       https://github.com/DefinitelyTyped/DefinitelyTyped/issues/20421.
+export const FetchMock: FetchMockExtended = Mock as any;
diff --git a/src/common/http/fetch.test.ts b/src/common/http/fetch.test.ts
new file mode 100644
index 0000000..48a97d2
--- /dev/null
+++ b/src/common/http/fetch.test.ts
@@ -0,0 +1,46 @@
+import * as assert from "assert";
+import { FetchMock } from "./fetch-mock.test";
+import { ClientError, RedirectError, ServerError, fetch } from "./fetch";
+
+describe("fetch", () => {
+  describe(".fetch()", () => {
+    it("returns a response when successful", done => {
+      const fetcher = FetchMock.sandbox().mock("url", 200);
+      fetch("url", undefined, fetcher).then(response => {
+        assert.deepEqual(response.status, 200);
+        done();
+      });
+    });
+
+    it("throws a RedirectError when redirected", done => {
+      const fetcher = FetchMock.sandbox().mock("url", {
+        status: 300,
+        headers: { location: "destination" }
+      });
+      fetch("url", undefined, fetcher).catch(error => {
+        assert.ok(error instanceof RedirectError);
+        assert.deepEqual(error.status, 300);
+        assert.deepEqual(error.url, "destination");
+        done();
+      });
+    });
+
+    it("throws a ClientError when the client fails", done => {
+      const fetcher = FetchMock.sandbox().mock("url", 400);
+      fetch("url", undefined, fetcher).catch(error => {
+        assert.ok(error instanceof ClientError);
+        assert.deepEqual(error.status, 400);
+        done();
+      });
+    });
+
+    it("throws a ServerError when the server fails", done => {
+      const fetcher = FetchMock.sandbox().mock("url", 500);
+      fetch("url", undefined, fetcher).catch(error => {
+        assert.ok(error instanceof ServerError);
+        assert.deepEqual(error.status, 500);
+        done();
+      });
+    });
+  });
+});
diff --git a/src/common/http/fetch.ts b/src/common/http/fetch.ts
index 8fa568d..ab0d694 100644
--- a/src/common/http/fetch.ts
+++ b/src/common/http/fetch.ts
@@ -32,12 +32,13 @@
  */
 export function fetch(
   input: RequestInfo,
-  init?: RequestInit
+  init?: RequestInit,
+  fetcher: typeof unfetch = unfetch
 ): Promise<Response> {
   // Setting the redirect mode to "error" doesn't appear to yield the status
   // code so "manual" is used instead.
   const redirect = server ? "manual" : undefined;
-  return unfetch(input, { redirect, ...init }).then(response => {
+  return fetcher(input, { redirect, ...init }).then(response => {
     if (server && response.status >= 300 && response.status <= 399) {
       const url = response.headers.get("location");
       throw new RedirectError(response.status, url as string);

-- 
To view, visit https://gerrit.wikimedia.org/r/394476
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If6702dfca8557127f889810145ae7adbe44d71e0
Gerrit-PatchSet: 1
Gerrit-Project: marvin
Gerrit-Branch: master
Gerrit-Owner: Niedzielski <[email protected]>
Gerrit-Reviewer: Sniedzielski <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to