-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathfetch.js
More file actions
239 lines (212 loc) · 10.4 KB
/
fetch.js
File metadata and controls
239 lines (212 loc) · 10.4 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import {test, expect} from '../fixtures.js'
test.describe("the fetch command", () => {
test("can do a simple fetch", async ({html, find, mock}) => {
await mock('GET', '/test', 'yay', {status: 200, contentType: 'text/html'});
await html("<div _='on click fetch \"/test\" then put it into my.innerHTML'></div>");
await find('div').dispatchEvent('click');
await expect(find('div')).toHaveText("yay");
});
test("can do a simple fetch w/ a naked URL", async ({html, find, mock}) => {
await mock('GET', '/test', 'yay', {status: 200, contentType: 'text/html'});
await html("<div _='on click fetch /test then put it into my.innerHTML'></div>");
await find('div').dispatchEvent('click');
await expect(find('div')).toHaveText("yay");
});
test("can do a simple fetch w/ html", async ({html, find, mock}) => {
await mock('GET', '/test', '<br>', {status: 200, contentType: 'text/html'});
await html(
"<div _='on click fetch /test as html then log it then put it into my.innerHTML put its childElementCount into my @data-count'></div>"
);
await find('div').dispatchEvent('click');
await expect(find('div')).toHaveText("[object DocumentFragment]");
await expect(find('div')).toHaveAttribute('data-count', '1');
});
test("can do a simple fetch w/ json", async ({html, find, mock}) => {
await mock('GET', '/test', '{"foo":1}', {status: 200, contentType: 'application/json'});
await html(
"<div _='on click fetch /test as json then get result as JSONString then put it into my.innerHTML'></div>"
);
await find('div').dispatchEvent('click');
await expect(find('div')).toHaveText('{"foo":1}');
});
test("can do a simple fetch w/ json using JSON syntax", async ({html, find, mock}) => {
await mock('GET', '/test', '{"foo":1}', {status: 200, contentType: 'application/json'});
await html(
"<div _='on click fetch /test as JSON then get result as JSONString then put it into my.innerHTML'></div>"
);
await find('div').dispatchEvent('click');
await expect(find('div')).toHaveText('{"foo":1}');
});
test("can do a simple fetch w/ json using Object syntax", async ({html, find, mock}) => {
await mock('GET', '/test', '{"foo":1}', {status: 200, contentType: 'application/json'});
await html(
"<div _='on click fetch /test as Object then get result as JSONString then put it into my.innerHTML'></div>"
);
await find('div').dispatchEvent('click');
await expect(find('div')).toHaveText('{"foo":1}');
});
test("can do a simple fetch w/ json using Object syntax and an 'an' prefix", async ({html, find, mock}) => {
await mock('GET', '/test', '{"foo":1}', {status: 200, contentType: 'application/json'});
await html(
"<div _='on click fetch /test as an Object then get result as JSONString then put it into my.innerHTML'></div>"
);
await find('div').dispatchEvent('click');
await expect(find('div')).toHaveText('{"foo":1}');
});
test("can do a simple fetch with a response object", async ({html, find, mock}) => {
await mock('GET', '/test', '{"foo":1}', {status: 200, contentType: 'application/json'});
await html("<div _='on click fetch /test as response then if its.ok put \"yep\" into my.innerHTML'></div>");
await find('div').dispatchEvent('click');
await expect(find('div')).toHaveText("yep");
});
test("can do a simple fetch w/ a custom conversion", async ({html, find, mock}) => {
await mock('GET', '/test', '1.2000', {status: 200, contentType: 'text/plain'});
await html("<div _='on click fetch /test as Number then put it into my.innerHTML'></div>");
await find('div').dispatchEvent('click');
await expect(find('div')).toHaveText("1.2");
});
test("can do a simple post", async ({html, find, mock}) => {
await mock('POST', '/test', 'yay', {status: 200, contentType: 'text/html'});
await html("<div _='on click fetch /test {method:\"POST\"} then put it into my.innerHTML'></div>");
await find('div').dispatchEvent('click');
await expect(find('div')).toHaveText("yay");
});
test("can do a simple post alt syntax without curlies", async ({html, find, mock}) => {
await mock('POST', '/test', 'yay', {status: 200, contentType: 'text/html'});
await html("<div _='on click fetch /test with method:\"POST\" then put it into my.innerHTML'></div>");
await find('div').dispatchEvent('click');
await expect(find('div')).toHaveText("yay");
});
test("can do a simple post alt syntax w/ curlies", async ({html, find, mock}) => {
await mock('POST', '/test', 'yay', {status: 200, contentType: 'text/html'});
await html("<div _='on click fetch /test with {method:\"POST\"} then put it into my.innerHTML'></div>");
await find('div').dispatchEvent('click');
await expect(find('div')).toHaveText("yay");
});
test("can put response conversion after with", async ({html, find, mock}) => {
await mock('POST', '/test', 'yay', {status: 200, contentType: 'text/html'});
await html("<div _='on click fetch /test with {method:\"POST\"} as text then put it into my.innerHTML'></div>");
await find('div').dispatchEvent('click');
await expect(find('div')).toHaveText("yay");
});
test("can put response conversion before with", async ({html, find, mock}) => {
await mock('POST', '/test', 'yay', {status: 200, contentType: 'text/html'});
await html("<div _='on click fetch /test as text with {method:\"POST\"} then put it into my.innerHTML'></div>");
await find('div').dispatchEvent('click');
await expect(find('div')).toHaveText("yay");
});
test("triggers an event just before fetching", async ({html, find, mock, evaluate}) => {
await mock('GET', '/test', 'yay', {status: 200, contentType: 'text/html'});
await evaluate(() => {
window.addEventListener('hyperscript:beforeFetch', (event) => {
event.target.className = "foo-set";
});
});
await html("<div _='on click fetch \"/test\" then put it into my.innerHTML end'></div>");
await expect(find('div')).not.toHaveClass(/foo-set/);
await find('div').dispatchEvent('click');
await expect(find('div')).toHaveClass(/foo-set/);
await expect(find('div')).toHaveText("yay");
});
test("submits the fetch parameters to the event handler", async ({html, find, mock, evaluate}) => {
await mock('GET', '/test', 'yay', {status: 200, contentType: 'text/html'});
await evaluate(() => {
window.headerCheckPassed = false;
window.addEventListener('hyperscript:beforeFetch', (event) => {
if (event.detail.headers && event.detail.headers['X-CustomHeader'] === 'foo') {
window.headerCheckPassed = true;
}
});
});
await html("<div _='on click fetch \"/test\" {headers: {\"X-CustomHeader\": \"foo\"}} then put it into my.innerHTML end'></div>");
await find('div').dispatchEvent('click');
await expect(find('div')).toHaveText("yay");
expect(await evaluate(() => window.headerCheckPassed)).toBe(true);
});
test("allows the event handler to change the fetch parameters", async ({html, find, evaluate, page}) => {
await evaluate(() => {
window.addEventListener('hyperscript:beforeFetch', (event) => {
event.detail.headers = {'X-CustomHeader': 'foo'};
});
});
await page.route('**/test', async (route) => {
const headers = route.request().headers();
if (headers['x-customheader'] === 'foo') {
await route.fulfill({status: 200, contentType: 'text/html', body: 'yay'});
} else {
await route.fulfill({status: 200, contentType: 'text/html', body: 'no header'});
}
});
await html("<div _='on click fetch \"/test\" then put it into my.innerHTML end'></div>");
await find('div').dispatchEvent('click');
await expect(find('div')).toHaveText("yay");
});
test("can catch an error that occurs when using fetch", async ({html, find, page}) => {
await page.route('**/test', async (route) => {
await route.abort();
});
await html("<div _='on click fetch /test catch e log e put \"yay\" into me'></div>");
await find('div').dispatchEvent('click');
await expect(find('div')).toHaveText("yay");
});
test("throws on non-2xx response by default", async ({html, find, page}) => {
await page.route('**/test', async (route) => {
await route.fulfill({ status: 404, body: 'not found' });
});
await html("<div _='on click fetch /test catch e put \"caught\" into me'></div>");
await find('div').dispatchEvent('click');
await expect(find('div')).toHaveText("caught");
});
test("do not throw passes through 404 response", async ({html, find, page}) => {
await page.route('**/test', async (route) => {
await route.fulfill({ status: 404, body: 'the body' });
});
await html("<div _='on click fetch /test do not throw then put it into me'></div>");
await find('div').dispatchEvent('click');
await expect(find('div')).toHaveText("the body");
});
test("don't throw passes through 404 response", async ({html, find, page}) => {
await page.route('**/test', async (route) => {
await route.fulfill({ status: 404, body: 'the body' });
});
await html('<div _="on click fetch /test don\'t throw then put it into me"></div>');
await find('div').dispatchEvent('click');
await expect(find('div')).toHaveText("the body");
});
test("does not throw on 204 No Content", async ({html, find, page}) => {
await page.route('**/test', async (route) => {
await route.fulfill({ status: 204, body: '' });
});
await html("<div _='on click fetch /test then put \"ok\" into me'></div>");
await find('div').dispatchEvent('click');
await expect(find('div')).toHaveText("ok");
});
test("does not throw on 304 Not Modified", async ({html, find, page}) => {
await page.route('**/test', async (route) => {
await route.fulfill({ status: 304, body: '' });
});
await html("<div _='on click fetch /test then put \"ok\" into me'></div>");
await find('div').dispatchEvent('click');
await expect(find('div')).toHaveText("ok");
});
test("as response does not throw on 404", async ({html, find, page}) => {
await page.route('**/test', async (route) => {
await route.fulfill({ status: 404, body: 'not found' });
});
await html("<div _='on click fetch /test as response then put it.status into me'></div>");
await find('div').dispatchEvent('click');
await expect(find('div')).toHaveText("404");
});
test("Response can be converted to JSON via as JSON", async ({html, find, page}) => {
await page.route('**/test', async (route) => {
await route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ name: "Joe" })
});
});
await html("<div _=\"on click fetch /test as Response then put (it as JSON).name into me\"></div>");
await find('div').dispatchEvent('click');
await expect(find('div')).toHaveText("Joe");
});
});