-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathindex.js
More file actions
85 lines (71 loc) · 2.19 KB
/
index.js
File metadata and controls
85 lines (71 loc) · 2.19 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import fetch from '../src/index.mjs';
import fetchDist from '..';
describe('unfetch', () => {
it('should be a function', () => {
expect(fetch).toEqual(expect.any(Function));
});
it('should be compiled correctly', () => {
expect(fetchDist).toEqual(expect.any(Function));
expect(fetchDist).toHaveLength(2);
});
describe('fetch()', () => {
let xhr;
beforeEach(() => {
xhr = {
setRequestHeader: jest.fn(),
getAllResponseHeaders: jest.fn().mockReturnValue('X-Foo: bar\nX-Foo:baz'),
open: jest.fn(),
send: jest.fn(),
readyState: 4,
status: 200,
statusText: 'OK',
responseText: '{"a":"b"}',
responseURL: '/foo?redirect'
};
global.XMLHttpRequest = jest.fn(() => xhr);
});
afterEach(() => {
delete global.XMLHttpRequest;
});
it('sanity test', () => {
let p = fetch('/foo', { headers: { a: 'b' } })
.then( r => {
expect(r).toMatchObject({
text: expect.any(Function),
json: expect.any(Function),
blob: expect.any(Function),
clone: expect.any(Function),
headers: expect.any(Object)
});
expect(r.clone()).not.toBe(r);
expect(r.clone().url).toEqual('/foo?redirect');
expect(r.headers.get).toEqual(expect.any(Function));
expect(r.headers.get('x-foo')).toEqual('bar,baz');
return r.json();
})
.then( data => {
expect(data).toEqual({ a: 'b' });
expect(xhr.setRequestHeader).toHaveBeenCalledTimes(1);
expect(xhr.setRequestHeader).toHaveBeenCalledWith('a', 'b');
expect(xhr.open).toHaveBeenCalledTimes(1);
expect(xhr.open).toHaveBeenCalledWith('get', '/foo', true);
expect(xhr.send).toHaveBeenCalledTimes(1);
expect(xhr.send).toHaveBeenCalledWith(null);
});
expect(xhr.onreadystatechange).toEqual(expect.any(Function));
expect(xhr.onerror).toEqual(expect.any(Function));
xhr.onreadystatechange();
return p;
});
it('handles empty header values', () => {
xhr.getAllResponseHeaders = jest.fn().mockReturnValue('Server: \nX-Foo:baz');
let p = fetch('/foo')
.then(r => {
expect(r.headers.get('server')).toEqual('');
expect(r.headers.get('X-foo')).toEqual('baz');
});
xhr.onreadystatechange();
return p;
});
});
});