Skip to content

Commit

Permalink
AbortController ponyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
stereobooster committed Sep 29, 2018
1 parent 1670b30 commit 5b907e4
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"test": "eslint src test && jest",
"build": "microbundle src/index.mjs && microbundle -f cjs polyfill/polyfill.mjs -o polyfill/index.js --no-sourcemap && cp dist/unfetch.mjs dist/unfetch.es.js",
"prepare": "npm run -s build",
"release": "cross-var npm run build -s && cross-var git commit -am $npm_package_version && cross-var git tag $npm_package_version && git push && git push --tags && npm publish"
"release": "cross-var npm run build -s && cross-var git commit -am $npm_package_version && cross-var git tag $npm_package_version && git push && git push --tags && npm publish",
"format": "eslint {src,test} --fix"
},
"repository": "developit/unfetch",
"keywords": [
Expand Down
6 changes: 6 additions & 0 deletions src/AbortController.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function() {
this.signal = { onabort: () => {} };
this.abort = () => {
this.signal.onabort();
};
}
3 changes: 3 additions & 0 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export default function(url, options) {

request.onerror = reject;

if (options.signal) options.signal.onabort = () => { request.abort(); }
request.onabort = () => reject(new DOMException('The user aborted a request.'));

request.send(options.body || null);

function response() {
Expand Down
46 changes: 46 additions & 0 deletions test/AbortController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import fetch from '../src/index.mjs';
import AbortController from '../src/AbortController.mjs';

describe('AbortController', () => {
it('should be a function', () => {
expect(AbortController).toEqual(expect.any(Function));
});

describe('AbortController()', () => {
let xhr;

beforeEach(() => {
xhr = {
setRequestHeader: jest.fn(),
getAllResponseHeaders: jest.fn().mockReturnValue('X-Foo: bar\nX-Foo:baz'),
open: jest.fn(),
send: jest.fn(),
abort: jest.fn(() => xhr.onabort({ type: 'abort' })) ,
status: 200,
statusText: 'OK',
responseText: '{"a":"b"}',
responseURL: '/foo?redirect'
};

global.XMLHttpRequest = jest.fn(() => xhr);
});

afterEach(() => {
delete global.XMLHttpRequest;
});

it('handles abort', () => {
let controller = new AbortController();
let signal = controller.signal;
let p = fetch('/foo', { signal })
.then(() => {})
.catch((e) => {
expect(e.message).toEqual('The user aborted a request.');
});

controller.abort();

return p;
});
});
});

0 comments on commit 5b907e4

Please sign in to comment.