Skip to content

Commit 29cf700

Browse files
authored
Add upload tests
1 parent 9bd1363 commit 29cf700

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import { Cloudinary, CloudinaryImage } from '@cloudinary/url-gen';
2+
import { upload, explicit, rename } from '../src/uploader';
3+
import { buildRequest, makeRequest } from '../src/uploader_utils';
4+
import { UploadApiOptions } from 'cloudinary-react-native';
5+
6+
const cloudinary = new Cloudinary({
7+
cloud: {
8+
cloudName: 'test'
9+
},
10+
url: {
11+
secure: true
12+
}
13+
});
14+
jest.mock('../src/uploader_utils', () => ({
15+
buildRequest: jest.fn(),
16+
makeRequest: jest.fn()
17+
}));
18+
19+
describe('upload function', () => {
20+
afterEach(() => {
21+
jest.clearAllMocks();
22+
});
23+
24+
it('uploads a file with file path', async () => {
25+
const filePath = '/path/to/file';
26+
27+
const options: UploadApiOptions = {
28+
upload_preset: 'ios_sample',
29+
unsigned: true,
30+
}
31+
await upload(cloudinary, { file: filePath });
32+
33+
expect(buildRequest).toHaveBeenCalledWith(cloudinary, 'upload', {
34+
file: filePath,
35+
headers: {},
36+
options: {},
37+
config: null
38+
});
39+
expect(makeRequest).toHaveBeenCalled();
40+
});
41+
42+
it('uploads a file with Base64 payload', async () => {
43+
const base64Payload = 'yourBase64StringHere';
44+
45+
await upload(cloudinary, { file: base64Payload });
46+
47+
expect(buildRequest).toHaveBeenCalledWith(cloudinary, 'upload', {
48+
file: base64Payload,
49+
headers: {},
50+
options: {},
51+
config: null
52+
});
53+
expect(makeRequest).toHaveBeenCalled();
54+
});
55+
it('uploads a file with Base64 payload and custom options', async () => {
56+
const base64Payload = 'SomeBase64StringMock';
57+
const customOptions = { folder: 'uploads', tags: ['react', 'native'] };
58+
59+
await upload(cloudinary, { file: base64Payload, options: customOptions });
60+
61+
expect(buildRequest).toHaveBeenCalledWith(cloudinary, 'upload', {
62+
file: base64Payload,
63+
headers: {},
64+
options: customOptions,
65+
config: null
66+
});
67+
expect(makeRequest).toHaveBeenCalled();
68+
});
69+
it('uploads a file with file path, custom headers, and options', async () => {
70+
const filePath = '/path/to/file';
71+
const customHeaders = { Authorization: 'Bearer token' };
72+
const customOptions = { folder: 'uploads', tags: ['react', 'native'] };
73+
74+
await upload(cloudinary, { file: filePath, headers: customHeaders, options: customOptions });
75+
76+
expect(buildRequest).toHaveBeenCalledWith(cloudinary, 'upload', {
77+
file: filePath,
78+
headers: expect.objectContaining(customHeaders),
79+
options: expect.objectContaining(customOptions),
80+
config: null
81+
});
82+
expect(makeRequest).toHaveBeenCalled();
83+
});
84+
});
85+
describe('rename function', () => {
86+
afterEach(() => {
87+
jest.clearAllMocks(); // Clear mock calls after each test
88+
});
89+
90+
it('renames a file', async () => {
91+
const from_public_id = 'old_public_id';
92+
const to_public_id = 'new_public_id';
93+
const customOptions = { someOption: 'value' };
94+
95+
await rename(cloudinary, { from_public_id: from_public_id, to_public_id: to_public_id, options: customOptions });
96+
97+
expect(buildRequest).toHaveBeenCalledWith(cloudinary, 'rename', {
98+
file: undefined,
99+
headers: undefined,
100+
options: expect.objectContaining({
101+
from_public_id,
102+
to_public_id,
103+
someOption: 'value'
104+
}),
105+
config: null
106+
});
107+
expect(makeRequest).toHaveBeenCalled();
108+
});
109+
});
110+
111+
describe('explicit function', () => {
112+
afterEach(() => {
113+
jest.clearAllMocks();
114+
});
115+
116+
it('requests explicit transformation for a file', async () => {
117+
const publicId = 'image_public_id';
118+
const customOptions = { type: 'upload', eager: [{ width: 300, height: 300, crop: 'pad' }] };
119+
120+
await explicit(cloudinary, { publicId, options: customOptions });
121+
122+
expect(buildRequest).toHaveBeenCalledWith(cloudinary, 'explicit', {
123+
headers: undefined,
124+
options: expect.objectContaining({
125+
public_id: publicId,
126+
type: 'upload',
127+
eager: [{ width: 300, height: 300, crop: 'pad' }]
128+
}),
129+
config: null
130+
});
131+
expect(makeRequest).toHaveBeenCalled();
132+
});
133+
});

0 commit comments

Comments
 (0)