-
Notifications
You must be signed in to change notification settings - Fork 906
/
form.test.ts
65 lines (58 loc) · 1.62 KB
/
form.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
import { multipartFormRequestOptions, createForm } from 'openai/core';
import { Blob } from 'openai/_shims/index';
import { toFile } from 'openai';
describe('form data validation', () => {
test('valid values do not error', async () => {
await multipartFormRequestOptions({
body: {
foo: 'foo',
string: 1,
bool: true,
file: await toFile(Buffer.from('some-content')),
blob: new Blob(['Some content'], { type: 'text/plain' }),
},
});
});
test('null', async () => {
await expect(() =>
multipartFormRequestOptions({
body: {
null: null,
},
}),
).rejects.toThrow(TypeError);
});
test('undefined is stripped', async () => {
const form = await createForm({
foo: undefined,
bar: 'baz',
});
expect(form.has('foo')).toBe(false);
expect(form.get('bar')).toBe('baz');
});
test('nested undefined property is stripped', async () => {
const form = await createForm({
bar: {
baz: undefined,
},
});
expect(Array.from(form.entries())).toEqual([]);
const form2 = await createForm({
bar: {
foo: 'string',
baz: undefined,
},
});
expect(Array.from(form2.entries())).toEqual([['bar[foo]', 'string']]);
});
test('nested undefined array item is stripped', async () => {
const form = await createForm({
bar: [undefined, undefined],
});
expect(Array.from(form.entries())).toEqual([]);
const form2 = await createForm({
bar: [undefined, 'foo'],
});
expect(Array.from(form2.entries())).toEqual([['bar[]', 'foo']]);
});
});