Skip to content

Commit

Permalink
switch to github fetch
Browse files Browse the repository at this point in the history
Github/fetch has merged PR for UMD distribution:
JakeChampion/fetch#616

Also abort support has been added:
JakeChampion/fetch#592

We can use Github fetch instead of yetch now.
But AbortableFetch is still not supported, see comment
in test.
  • Loading branch information
Thomas Wang committed May 24, 2018
1 parent 26b42fc commit a0bb51f
Show file tree
Hide file tree
Showing 5 changed files with 959 additions and 816 deletions.
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = function(config) {
'bower_components/jquery/dist/jquery.js',
'node_modules/es6-promise/dist/es6-promise.auto.js',
'node_modules/abortcontroller-polyfill/dist/abortcontroller-polyfill-only.js',
'assets/yetch-ponyfill.js',
'node_modules/whatwg-fetch/dist/fetch.umd.js',
'pretender.js',
'test/**/*.js'
],
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@
"dependencies": {
"fake-xml-http-request": "^2.0.0",
"route-recognizer": "^0.3.3",
"yetch": "^0.0.1"
"whatwg-fetch": "^2.0.4"
},
"jspm": {
"shim": {
"pretender": {
"deps": [
"route-recognizer",
"fake-xml-http-request"
"fake-xml-http-request",
"whatwg-fetch"
],
"exports": "Pretender"
}
Expand Down
4 changes: 2 additions & 2 deletions pretender.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var FakeXMLHttpRequest = appearsBrowserified ? getModuleDefault(require('fake-xm
self.FakeXMLHttpRequest;

// fetch related ponyfills
var Yetch = appearsBrowserified ? getModuleDefault(require('yetch/dist/yetch-polyfill')) : self.Yetch;
var WHATWGFetch = appearsBrowserified ? getModuleDefault(require('whatwg-fetch')) : self.WHATWGFetch;

/**
* parseURL - decompose a URL into its parts
Expand Down Expand Up @@ -147,7 +147,7 @@ function Pretender(/* routeMap1, routeMap2, ..., options*/) {
this._fetchProps = ['fetch', 'Headers', 'Request', 'Response'];
this._fetchProps.forEach(function(name) {
this['_native' + name] = self[name];
self[name] = Yetch[name];
self[name] = WHATWGFetch[name];
}, this);

// 'start' the server
Expand Down
39 changes: 30 additions & 9 deletions test/fetch_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,36 @@ describe('pretender invoking by fetch', function(config) {
assert.equal(val, 0);
});


// TODO: Pretender doesn't work with abortable fetch
// src in fake_xml_http_request.js
// ```
// abort: function abort() {
// this.aborted = true;
// this.responseText = null;
// this.errorFlag = true;
// this.requestHeaders = {};

// if (this.readyState > FakeXMLHttpRequest.UNSENT && this.sendFlag) {
// this._readyStateChange(FakeXMLHttpRequest.DONE);
// this.sendFlag = false;
// }

// this.readyState = FakeXMLHttpRequest.UNSENT;

// this.dispatchEvent(new _Event("abort", false, false, this));
// if (typeof this.onerror === "function") {
// this.onerror();
// }
// }
// ```
// For `fake_xml_http_request` impl, the request is resolved once its state
// is changed to `DONE` so the `reject` is not cathed.
// So the senario happens in pretender is:
// 1. state chagne to `DONE`, trigger resolve request
// 2. abort, trigger reject
// 3. xhr.onerror, trigger reject
// The first resolve wins, error thus not rejected but an empty request is resolved.
it('has NO Abortable fetch', function(assert) {
assert.expect(1);
var done = assert.async();
Expand All @@ -87,15 +117,6 @@ describe('pretender invoking by fetch', function(config) {
})
.catch(function(err) {
// it should execute to here but won't due to FakeXmlHttpRequest limitation
//
// ### why it's not working for fetch
// For `fake_xml_http_request` impl, the request is resolved once its state
// is changed to `DONE` so the `reject` is not cathed.
// So the senario happens in pretender is:
// 1. state chagne to `DONE`, trigger resolve request
// 2. abort, trigger reject
// 3. xhr.onerror, trigger reject
// The first resolve wins, error thus not rejected but an empty request is resolved.
done();
});
});
Expand Down
Loading

0 comments on commit a0bb51f

Please sign in to comment.