-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmock-fetch-api-test.js
192 lines (136 loc) · 5.98 KB
/
mock-fetch-api-test.js
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
ISC License:
Copyright (c) 2004-2010 by Internet Systems Consortium, Inc. ("ISC")
Copyright (c) 1995-2003 by Internet Software Consortium
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
jest.autoMockOff();
describe('MockFetch test', () => {
pit("can set a condition which is returned by fetch", () => {
var MockFetch = require('../mock-fetch-api.js');
MockFetch.when('GET', 'http://mydomain.com').respondWith(200, '"Hello World"');
return fetch('http://mydomain.com').then((response) => {
return response.json();
}).then((data) => {
expect(data).toBe('Hello World');
});
});
pit("can set a condition which is returned by fetch", () => {
var MockFetch = require('../mock-fetch-api.js');
MockFetch.when('GET', 'http://mydomain.com').respondWith(200, '"Hello World"');
return fetch('http://mydomain.com').then((response) => {
response.json().then((data) => {
expect(data).toBe('Hello World');
});
});
});
pit("test connection with default method GET", () => {
var MockFetch = require('../mock-fetch-api.js');
MockFetch.when('GET', 'http://mydomain.com').respondWith(200, '"Hello World"');
return fetch('http://mydomain.com', {}).then((response) => {
expect(response.status).toBe(200);
});
});
pit("only responds when matched correctly", () => {
var MockFetch = require('../mock-fetch-api.js');
MockFetch.when('GET', 'http://mydomain.com').respondWith(200, '"Hello World"');
return fetch('http://mydomain.com', { method: 'PUT'}).then((response) => {
expect(response.status).toBe(404);
expect(response.statusText).toBe('Not Found');
});
});
pit("also checks for an expected header value", () => {
var MockFetch = require('../mock-fetch-api.js');
MockFetch.when('GET', 'http://mydomain.com')
.withExpectedHeader('X-AuthToken','1234')
.otherwiseRespondWith(401, "Not Authorized")
.respondWith(200, '"Hello World"');
return fetch('http://mydomain.com', { method: 'GET', headers: new Headers({
'X-AuthToken':'1234'
})}).then((response) => {
expect(response.status).toBe(200);
});
});
pit("fails when expected header is not set", () => {
var MockFetch = require('../mock-fetch-api.js');
MockFetch.when('GET', 'http://mydomain.com')
.withExpectedHeader({'X-AuthToken':'1234'}).otherwiseRespondWith(401, "Not Authorized")
.respondWith(200, '"Hello World"');
return fetch('http://mydomain.com', { method: 'GET'}).then((response) => {
expect(response.status).toBe(401);
expect(response.statusText).toBe('Not Authorized');
});
});
pit("fails when expected header is has the wrong value", () => {
var MockFetch = require('../mock-fetch-api.js');
MockFetch.when('GET', 'http://mydomain.com')
.withExpectedHeader('X-AuthToken','1234').otherwiseRespondWith(401, "Not Authorized")
.respondWith(200, '"Hello World"');
return fetch('http://mydomain.com', { method: 'GET', headers: new Headers({
'X-AuthToken':'4321'
})}).then((response) => {
expect(response.status).toBe(401);
expect(response.statusText).toBe('Not Authorized');
});
});
pit("can check for multiple expected headers", () => {
var MockFetch = require('../mock-fetch-api.js');
MockFetch.when('GET', 'http://mydomain.com')
.withExpectedHeader('X-AuthToken','1234')
.withExpectedHeader('BANANA','8757').otherwiseRespondWith(401, "Not Authorized")
.respondWith(200, '"Hello World"');
return fetch('http://mydomain.com', { method: 'GET', headers: new Headers({
'X-AuthToken':'1234',
'BANANA':'8757'
})}).then((response) => {
expect(response.status).toBe(200);
});
});
pit("rejects the promise when simulating a failed network connection", () => {
var MockFetch = require('../mock-fetch-api.js');
MockFetch.when('GET', 'http://mydomain.com')
.respondWith(200, '"Hello World"');
MockFetch.failNextCall();
return fetch('http://mydomain.com').then((response) => {
expect(false).toBe(true);
}, (error) => {
expect(true).toBe(true);
});
});
pit("rejects the promise ONLY for the next call when simulating a failed network connection", () => {
var MockFetch = require('../mock-fetch-api.js');
MockFetch.when('GET', 'http://mydomain.com')
.respondWith(200, '"Hello World"');
MockFetch.failNextCall();
fetch('http://mydomain.com').then((response) => { }, (error) => { });
// should then succeed again...
return fetch('http://mydomain.com').then((response) => {
expect(response.status).toBe(200);
}, (error) => {
expect(false).toBe(true);
});
});
pit("can match on the uploaded body", () => {
var MockFetch = require('../mock-fetch-api.js');
MockFetch.when('POST', 'http://mydomain.com')
.respondWith(200, '"Hello World"');
MockFetch.failNextCall();
return fetch('http://mydomain.com', {
method: 'POST',
body: '{"ID":"5"}'
}).then((response) => {
expect(false).toBe(true);
}, (error) => {
expect(true).toBe(true);
});
});
});