-
Notifications
You must be signed in to change notification settings - Fork 14
/
input-resolvers.test.ts
281 lines (259 loc) · 8.28 KB
/
input-resolvers.test.ts
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import { assertEquals, describe, it } from './prelude.ts'
import * as subject from '../input-resolvers.ts'
const makePost: (entries: Array<[string, string]>, url?: string) => Request = (
entries,
url = 'http://localhost/test',
) =>
new Request(url, {
method: 'POST',
body: new URLSearchParams(entries).toString(),
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
})
const makeGet: (entries: Array<[string, string]>, url?: string) => Request = (
entries,
url = 'http://localhost/test',
) =>
new Request(url + '?' + new URLSearchParams(entries).toString(), {
method: 'GET',
})
describe('inputFromForm', () => {
it('should parse all symbols correctly', async () => {
const request = makePost([['formula', '3 % 2']])
assertEquals(await subject.inputFromForm(request), {
formula: '3 % 2',
})
})
it("extracts the input values from a Request's FormData as an Object", async () => {
const request = makePost([
['foo', 'bar'],
['fizz', 'buzz'],
])
assertEquals(await subject.inputFromForm(request), {
foo: 'bar',
fizz: 'buzz',
})
})
it('accepts array-like values', async () => {
const request = makePost([
['foo', 'bar'],
['arr[]', '1'],
['arr[]', '2'],
])
assertEquals(await subject.inputFromForm(request), {
foo: 'bar',
arr: ['1', '2'],
})
})
it('accepts structured values', async () => {
const request = makePost([
['person[0][name]', 'John'],
['person[0][email]', '[email protected]'],
['person[1][name]', 'Bill'],
['person[1][email]', '[email protected]'],
])
assertEquals(await subject.inputFromForm(request), {
person: [
{ name: 'John', email: '[email protected]' },
{ name: 'Bill', email: '[email protected]' },
],
})
})
})
describe('inputFromUrl', () => {
it("extracts the input values from a Request's URL as an Object", () => {
const request = makeGet([
['foo', 'bar'],
['fizz', 'buzz'],
])
assertEquals(subject.inputFromUrl(request), { foo: 'bar', fizz: 'buzz' })
})
it('accepts array-like values', async () => {
const request = makeGet([
['foo', 'bar'],
['arr[]', '1'],
['arr[]', '2'],
])
assertEquals(await subject.inputFromUrl(request), {
foo: 'bar',
arr: ['1', '2'],
})
})
it('accepts structured values', async () => {
const request = makeGet([
['person[0][name]', 'John'],
['person[0][email]', '[email protected]'],
['person[1][name]', 'Bill'],
['person[1][email]', '[email protected]'],
])
assertEquals(await subject.inputFromUrl(request), {
person: [
{ email: '[email protected]', name: 'John' },
{ name: 'Bill', email: '[email protected]' },
],
})
})
})
describe('inputFromFormData', () => {
it('extracts a structured object from a FormData', async () => {
const request = makePost([
['person[0][name]', 'John'],
['person[0][email]', '[email protected]'],
['person[1][name]', 'Bill'],
['person[1][email]', '[email protected]'],
])
assertEquals(subject.inputFromFormData(await request.formData()), {
person: [
{ name: 'John', email: '[email protected]' },
{ name: 'Bill', email: '[email protected]' },
],
})
})
it('accepts manually constructed FormData', () => {
const formData = new FormData()
formData.append('email', '[email protected]')
formData.append('tasks[]', 'one')
formData.append('tasks[]', 'two')
assertEquals(subject.inputFromFormData(formData), {
email: '[email protected]',
tasks: ['one', 'two'],
})
})
})
describe('inputFromSearch', () => {
it('extracts a structured object from a URLSearchParams', () => {
const request = makeGet([
['person[0][name]', 'John'],
['person[0][email]', '[email protected]'],
['person[1][name]', 'Bill'],
['person[1][email]', '[email protected]'],
])
assertEquals(subject.inputFromSearch(new URL(request.url).searchParams), {
person: [
{ email: '[email protected]', name: 'John' },
{ name: 'Bill', email: '[email protected]' },
],
})
})
it('creates flat objects', () => {
const qs = new URLSearchParams()
qs.append('person[name]', 'John')
qs.append('person[address]', '6 Some Rd')
assertEquals(subject.inputFromSearch(qs), {
person: { name: 'John', address: '6 Some Rd' },
})
})
it('creates nested objects', () => {
const qs = new URLSearchParams()
qs.append('person[name]', 'John')
qs.append('person[address][street]', 'Some Rd')
qs.append('person[address][unit]', '6')
assertEquals(subject.inputFromSearch(qs), {
person: { name: 'John', address: { street: 'Some Rd', unit: '6' } },
})
})
it('creates nested objects in arrays', () => {
const qs = new URLSearchParams()
qs.append('person[0][name]', 'John')
qs.append('person[0][address][street]', 'Some Rd')
qs.append('person[0][address][unit]', '6')
assertEquals(subject.inputFromSearch(qs), {
person: [{ name: 'John', address: { street: 'Some Rd', unit: '6' } }],
})
})
it('all together now 🎺', () => {
const qs = new URLSearchParams()
qs.append('person[0][name]', 'John')
qs.append('person[0][address][1][street]', 'Some Rd')
qs.append('person[0][address][1][unit]', '1')
qs.append('person[0][address][0][street]', 'Some Rd')
qs.append('person[0][address][0][unit]', '0')
qs.append('person[0][address][2][street]', 'Some Rd')
qs.append('person[0][address][2][unit]', '2')
qs.append('person[0][address][2][postal-code]', '222')
qs.append('order', '1')
qs.append('path', 'home')
qs.append('path', 'product page')
qs.append('path', 'purchase')
qs.append('timestamps[]', '1')
qs.append('timestamps[]', '2')
qs.append('timestamps[]', '3')
qs.append('how-about-multiple-dimensions[0][]', 'we can')
qs.append('how-about-multiple-dimensions[0][]', 'nest lists')
qs.append('how-about-multiple-dimensions[1][]', 'so lists of tuples')
qs.append('how-about-multiple-dimensions[1][]', 'are also possible')
assertEquals(subject.inputFromSearch(qs), {
order: '1',
path: ['home', 'product page', 'purchase'],
timestamps: ['1', '2', '3'],
'how-about-multiple-dimensions': [
['we can', 'nest lists'],
['so lists of tuples', 'are also possible'],
],
person: [
{
name: 'John',
address: [
{ street: 'Some Rd', unit: '0' },
{
street: 'Some Rd',
unit: '1',
},
{ street: 'Some Rd', unit: '2', 'postal-code': '222' },
],
},
],
})
})
it('square brackets are unnecessary to build lists', () => {
const qs = new URLSearchParams()
qs.append('colors', 'red')
qs.append('colors', 'green')
qs.append('colors', 'blue')
assertEquals(subject.inputFromSearch(qs), {
colors: ['red', 'green', 'blue'],
})
})
it('creates array with one element', () => {
const qs = new URLSearchParams()
qs.append('colors[0]', 'blue')
assertEquals(subject.inputFromSearch(qs), {
colors: ['blue'],
})
})
it('orders elements according to index in query string', () => {
const qs = new URLSearchParams()
qs.append('colors[1]', 'red')
qs.append('colors[0]', 'blue')
assertEquals(subject.inputFromSearch(qs), {
colors: ['blue', 'red'],
})
})
it('takes keys encoded as URI components', () => {
const qs = new URLSearchParams()
qs.append('some colors[0]', 'blue')
qs.append('some colors[1]', 'red ish')
assertEquals(subject.inputFromSearch(qs), {
'some colors': ['blue', 'red ish'],
})
})
it('takes values encoded as URI components', () => {
const qs = new URLSearchParams()
qs.append('colors[0]', 'blue')
qs.append('colors[1]', 'red ish')
qs.append('person[name]', 'Average Joe')
assertEquals(subject.inputFromSearch(qs), {
colors: ['blue', 'red ish'],
person: { name: 'Average Joe' },
})
})
it('accepts manually constructed URLSearchParams', () => {
const qs = new URLSearchParams()
qs.append('colors[]', 'red')
qs.append('colors[]', 'blue')
assertEquals(subject.inputFromSearch(qs), {
colors: ['red', 'blue'],
})
})
})