This repository was archived by the owner on Sep 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.test.js
More file actions
85 lines (73 loc) · 3.32 KB
/
Copy pathfetch.test.js
File metadata and controls
85 lines (73 loc) · 3.32 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 test from 'ava';
import sinon from 'sinon';
import { fetchJSON, getJSON, patchJSON, postJSON, putJSON, deleteJSON } from './fetch.js';
const POSITIVE_RESPONSE = {
status: 200,
statusText: 'OK',
headers: { 'content-type': 'application/json' },
ok: true,
text: () => '{"id": 42, "email": "ozzy@example.com", "name": "Ozzy"}'
};
const NEGATIVE_RESPONSE = {
status: 404,
ok: false,
statusText: 'Not Found'
};
test('Call `window.fetch` with given url, HTTP method, credentials, and body)', t => {
window.fetch = sinon.fake.resolves(POSITIVE_RESPONSE);
fetchJSON('/some/url', 'GET', 'omit', '{ "foo": "bar" }');
t.is(window.fetch.firstCall.args[0], '/some/url');
t.is(window.fetch.firstCall.args[1].method, 'GET');
t.is(window.fetch.firstCall.args[1].body, '{ "foo": "bar" }');
t.is(window.fetch.firstCall.args[1].credentials, 'omit');
});
test('Pass response JSON through a callback upon positive response', async t => {
window.fetch = sinon.fake.resolves(POSITIVE_RESPONSE);
const callback = sinon.spy();
await fetchJSON('/some/url', 'GET', 'omit', '{ "foo": "bar" }', callback);
t.true(callback.calledWith({ id: 42, name: 'Ozzy', email: 'ozzy@example.com' }));
});
test('Ignore the callback upon negative response', async t => {
window.fetch = sinon.fake.resolves(NEGATIVE_RESPONSE);
const callback = sinon.spy();
await fetchJSON('/some/url', 'GET', 'omit', null, callback);
t.is(callback.callCount, 0);
});
test('Return a promise that resolves when request succeeds', async t => {
window.fetch = sinon.fake.resolves(POSITIVE_RESPONSE);
const result = await fetchJSON('/some/url', 'GET', 'omit', '{ "foo": "bar" }');
t.deepEqual(result, { id: 42, name: 'Ozzy', email: 'ozzy@example.com' });
});
test('Call `window.fetch` with `PATCH` and the given request body', t => {
window.fetch = sinon.fake.resolves(POSITIVE_RESPONSE);
patchJSON('/some/url', '{ "foo": "bar" }');
t.is(window.fetch.firstCall.args[0], '/some/url');
t.is(window.fetch.firstCall.args[1].method, 'PATCH');
t.is(window.fetch.firstCall.args[1].body, '{ "foo": "bar" }');
});
test('Call `window.fetch` with `POST` and the given request body', t => {
window.fetch = sinon.fake.resolves(POSITIVE_RESPONSE);
postJSON('/some/url', '{ "foo": "bar" }');
t.is(window.fetch.firstCall.args[0], '/some/url');
t.is(window.fetch.firstCall.args[1].method, 'POST');
t.is(window.fetch.firstCall.args[1].body, '{ "foo": "bar" }');
});
test('Call `window.fetch` with `PUT` and the given request body', t => {
window.fetch = sinon.fake.resolves(POSITIVE_RESPONSE);
putJSON('/some/url', '{ "foo": "bar" }');
t.is(window.fetch.firstCall.args[0], '/some/url');
t.is(window.fetch.firstCall.args[1].method, 'PUT');
t.is(window.fetch.firstCall.args[1].body, '{ "foo": "bar" }');
});
test('Call `window.fetch` with `GET`', t => {
window.fetch = sinon.fake.resolves(POSITIVE_RESPONSE);
getJSON('/some/url');
t.is(window.fetch.firstCall.args[0], '/some/url');
t.is(window.fetch.firstCall.args[1].method, 'GET');
});
test('Call `window.fetch` with `DELETE`', t => {
window.fetch = sinon.fake.resolves(POSITIVE_RESPONSE);
deleteJSON('/some/url');
t.is(window.fetch.firstCall.args[0], '/some/url');
t.is(window.fetch.firstCall.args[1].method, 'DELETE');
});