forked from developit/unfetch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbortController.js
More file actions
46 lines (38 loc) · 1.04 KB
/
AbortController.js
File metadata and controls
46 lines (38 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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;
});
});
});