-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1670b30
commit 5b907e4
Showing
5 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}); | ||
}); |