cloudflare/cloudflare-typescript

Public

mirrored fromhttps://github.com/cloudflare/cloudflare-typescriptAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
181a3a8830dc573fbcec19cd254a25409705b5ed

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/uploads.test.ts

65lines · modecode

1import fs from 'fs';
2import { toFile, type ResponseLike } from 'cloudflare/uploads';
3import { File } from 'cloudflare/_shims/index';
4
5class MyClass {
6 name: string = 'foo';
7}
8
9function mockResponse({ url, content }: { url: string; content?: Blob }): ResponseLike {
10 return {
11 url,
12 blob: async () => content as any,
13 };
14}
15
16describe('toFile', () => {
17 it('throws a helpful error for mismatched types', async () => {
18 await expect(
19 // @ts-expect-error intentionally mismatched type
20 toFile({ foo: 'string' }),
21 ).rejects.toThrowErrorMatchingInlineSnapshot(
22 `"Unexpected data type: object; constructor: Object; props: ["foo"]"`,
23 );
24
25 await expect(
26 // @ts-expect-error intentionally mismatched type
27 toFile(new MyClass()),
28 ).rejects.toThrowErrorMatchingInlineSnapshot(
29 `"Unexpected data type: object; constructor: MyClass; props: ["name"]"`,
30 );
31 });
32
33 it('disallows string at the type-level', async () => {
34 // @ts-expect-error we intentionally do not type support for `string`
35 // to help people avoid passing a file path
36 const file = await toFile('contents');
37 expect(file.text()).resolves.toEqual('contents');
38 });
39
40 it('extracts a file name from a Response', async () => {
41 const response = mockResponse({ url: 'https://example.com/my/audio.mp3' });
42 const file = await toFile(response);
43 expect(file.name).toEqual('audio.mp3');
44 });
45
46 it('extracts a file name from a File', async () => {
47 const input = new File(['foo'], 'input.jsonl');
48 const file = await toFile(input);
49 expect(file.name).toEqual('input.jsonl');
50 });
51
52 it('extracts a file name from a ReadStream', async () => {
53 const input = fs.createReadStream('tests/uploads.test.ts');
54 const file = await toFile(input);
55 expect(file.name).toEqual('uploads.test.ts');
56 });
57
58 it('does not copy File objects', async () => {
59 const input = new File(['foo'], 'input.jsonl', { type: 'jsonl' });
60 const file = await toFile(input);
61 expect(file).toBe(input);
62 expect(file.name).toEqual('input.jsonl');
63 expect(file.type).toBe('jsonl');
64 });
65});
66