Enabled fetch-mock with tests.
This patch adds the fetch-mock library, demonstrating how it may be used to properly mock out API calls. An important change that was made was the way that 'isomorphic-fetch' was included. Since fetch-mock makes the assumption that only the window.fetch object should be overridden, we cannot invoke it via a variable. Instead, I've simply imported the library, which during import will override global.fetch if necessary. Change-Id: Ibd88b61595b4492f375294a8a08031a1a001a10c
This commit is contained in:
@@ -36,6 +36,7 @@
|
|||||||
"babel-register": "^6.9.0",
|
"babel-register": "^6.9.0",
|
||||||
"eslint": "^2.4.0",
|
"eslint": "^2.4.0",
|
||||||
"eslint-config-openstack": "2.0.0",
|
"eslint-config-openstack": "2.0.0",
|
||||||
|
"fetch-mock": "^5.0.5",
|
||||||
"istanbul": "^1.0.0-alpha.2",
|
"istanbul": "^1.0.0-alpha.2",
|
||||||
"jasmine": "^2.4.1",
|
"jasmine": "^2.4.1",
|
||||||
"karma": "^1.1.1",
|
"karma": "^1.1.1",
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
import 'babel-polyfill';
|
import 'babel-polyfill';
|
||||||
import fetch from 'isomorphic-fetch';
|
import 'isomorphic-fetch';
|
||||||
import log from 'loglevel';
|
import log from 'loglevel';
|
||||||
|
|
||||||
log.setLevel('INFO');
|
log.setLevel('INFO');
|
||||||
|
|
||||||
export default class Test {
|
export default class Test {
|
||||||
getUrl(url) {
|
getUrl (url) {
|
||||||
return fetch(url)
|
return fetch(url)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
log.info(response.status);
|
|
||||||
return response;
|
return response;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,32 @@
|
|||||||
import Test from "../../src/index.js";
|
import Test from "../../src/index.js";
|
||||||
|
|
||||||
|
const FetchMock = require('fetch-mock');
|
||||||
|
|
||||||
describe("Simple test", () => {
|
describe("Simple test", () => {
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
FetchMock.reset();
|
||||||
|
});
|
||||||
|
|
||||||
it("should export a class", () => {
|
it("should export a class", () => {
|
||||||
let t = new Test();
|
let t = new Test();
|
||||||
expect(t).toBeDefined();
|
expect(t).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should retrieve URL's", (done) => {
|
||||||
|
FetchMock.get("http://example.com/", {
|
||||||
|
status: 200,
|
||||||
|
body: "This is a test"
|
||||||
|
});
|
||||||
|
|
||||||
|
let t = new Test();
|
||||||
|
t.getUrl("http://example.com/")
|
||||||
|
.then((response) => {
|
||||||
|
return response.text();
|
||||||
|
})
|
||||||
|
.then((body) => {
|
||||||
|
expect(body).toEqual("This is a test");
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user