Skip to content

Commit 9207d95

Browse files
author
Maya Shavin
committed
add tests and fixes constants
1 parent ea5a424 commit 9207d95

33 files changed

+653
-111
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { customFunction } from '../lib/transformers/customFunc'
2+
3+
describe('customFunc', () => {
4+
it('should return empty string if there is no type or source', () => {
5+
expect(customFunction({ source: '', type: 'remote' })).toBe('')
6+
});
7+
8+
it('should return formatted custome Function', () => {
9+
expect(customFunction({ type: 'remote', source: 'my_example'})).toBe('fn_remote:my_example')
10+
});
11+
});

packages/url/__tests__/index.test.ts

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { buildUrl, buildVideoUrl, getConfig, setConfig } from '../lib/index'
1+
import { buildImageUrl, buildUrl, buildVideoUrl, extractPublicId, getConfig, RESOURCE_TYPES, setConfig, Transformer } from '../lib/index'
2+
import { RESOURCE_TYPES as OriginalResourceTypes, STORAGE_TYPES as OriginalStorageTypes, ROTATION_MODES as OriginalRotationModes, STORAGE_TYPES, ROTATION_MODES } from '../lib/constants'
3+
import { extractPublicId as originalExtractPublicId } from '../lib/url'
4+
import { transform, toTransformationStr } from '../lib/transformers'
25

36
describe('General tests', () => {
47
it('configurations', () => {
@@ -12,6 +15,13 @@ describe('General tests', () => {
1215
})
1316

1417
describe('buildUrl()', () => {
18+
it('should return default transformation', () => {
19+
expect(buildUrl('example', {
20+
cloud: {
21+
cloudName: 'demo'
22+
}
23+
})).toBe('https://res.cloudinary.com/demo/image/upload/q_auto,f_auto/example')
24+
});
1525
it('should return right url with configurations', () => {
1626
setConfig({
1727
cloudName: 'm'
@@ -48,6 +58,24 @@ describe('General tests', () => {
4858
cloudName: 'm'
4959
})
5060
});
61+
})
62+
63+
describe('buildVideoUrl()', () => {
64+
it('should return url with default config when no cloud', () => {
65+
setConfig({
66+
cloudName: 'demo'
67+
})
68+
69+
expect(buildVideoUrl('example', { transformations: {} })).toBe('https://res.cloudinary.com/demo/video/upload/q_auto,f_auto/example')
70+
});
71+
72+
it('should return url with default config', () => {
73+
setConfig({
74+
cloudName: 'demo'
75+
})
76+
77+
expect(buildVideoUrl('example', {})).toBe('https://res.cloudinary.com/demo/video/upload/q_auto,f_auto/example')
78+
});
5179

5280
it('should return right url format for video', () => {
5381
expect(buildVideoUrl('example', {
@@ -63,5 +91,61 @@ describe('General tests', () => {
6391
})).toEqual('https://res.cloudinary.com/demo/video/upload/c_scale,w_200,q_auto,f_auto/example')
6492

6593
})
66-
})
94+
});
95+
96+
describe('buildImageUrl()', () => {
97+
98+
it('should return url with default config when no cloud', () => {
99+
setConfig({
100+
cloudName: 'demo'
101+
})
102+
103+
expect(buildImageUrl('example', { transformations: {} })).toBe('https://res.cloudinary.com/demo/image/upload/q_auto,f_auto/example')
104+
});
105+
it('should return url with default config', () => {
106+
setConfig({
107+
cloudName: 'demo'
108+
})
109+
110+
expect(buildImageUrl('example', {})).toBe('https://res.cloudinary.com/demo/image/upload/q_auto,f_auto/example')
111+
});
112+
113+
it('should return url with cloud config', () => {
114+
115+
expect(buildImageUrl('example', {
116+
cloud: {
117+
cloudName: 'demo'
118+
},
119+
transformations: {
120+
rotate: 10,
121+
}
122+
})).toBe('https://res.cloudinary.com/demo/image/upload/q_auto,a_10,f_auto/example')
123+
});
124+
});
125+
126+
describe('exports', () => {
127+
it('should export resourceTypes', () => {
128+
expect(RESOURCE_TYPES).toEqual(OriginalResourceTypes)
129+
});
130+
131+
it('should export extractPublicId', () => {
132+
expect(extractPublicId).toEqual(extractPublicId)
133+
});
134+
135+
it('should export STORAGE_TYPES', () => {
136+
expect(STORAGE_TYPES).toEqual(OriginalStorageTypes)
137+
});
138+
139+
it('should export ROTATION_MODES', () => {
140+
expect(ROTATION_MODES).toEqual(OriginalRotationModes)
141+
});
142+
143+
it('should export transform', () => {
144+
expect(Transformer.transform).toEqual(transform)
145+
});
146+
147+
it('should export toTransformationStr', () => {
148+
expect(Transformer.toString).toEqual(toTransformationStr)
149+
});
150+
});
67151
})

packages/url/__tests__/transformers.test.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { position } from '../lib/transformers/position'
55
import { effect } from '../lib/transformers/effect'
66
import { flags } from '../lib/transformers/flags'
77
import { convert } from '../lib/transformers/expression'
8+
import { TransformerVideoOption } from '../lib/types/CldOptions'
9+
import { Brightness } from '../lib/constants/effects'
810

911
describe('Modifiers', () => {
1012
describe('transform()', () => {
@@ -73,6 +75,26 @@ describe('Modifiers', () => {
7375
['bo_1px_dashed_#fff', 'e_pixelate']
7476
])
7577
})
78+
79+
it('should transform video url', () => {
80+
const options:TransformerVideoOption = {
81+
audioCodec: 'aac',
82+
colorSpace: 'srgb',
83+
effect: {
84+
name: Brightness.Auto,
85+
value: 50
86+
},
87+
flags: ['animated', 'waveform'],
88+
gravity: 'auto'
89+
}
90+
expect(transform(options)).toEqual([
91+
'ac_aac',
92+
'cs_srgb',
93+
'g_auto',
94+
'e_auto_brightness:50',
95+
'fl_animated:waveform'
96+
])
97+
});
7698
})
7799

78100
describe('getBorder()', () => {
@@ -237,6 +259,14 @@ describe('border', () => {
237259
it('should return options', () => {
238260
expect(border({ color: 'blue', width: 10 })).toEqual('bo_10px_solid_blue')
239261
})
262+
263+
it('should return empty string', () => {
264+
expect(border({ width: '' })).toBe('')
265+
});
266+
267+
it('should return converted expression', () => {
268+
expect(border({ width: 'initialWidth + 3 / 10' })).toBe('bo_iw_add_3_div_10_solid_black')
269+
});
240270
})
241271

242272
describe('position', () => {
@@ -288,19 +318,19 @@ describe('effect', () => {
288318

289319
describe('flags', () => {
290320
it('should return empty string for empty string', () => {
291-
expect(flags('')).toBe('')
321+
expect(flags()).toBe('')
292322
});
293323

294324
it('should return empty string for empty array', () => {
295325
expect(flags([])).toBe('')
296326
});
297327

298328
it('should return legal string for none-empty string', () => {
299-
expect(flags('cutter')).toBe('fl_cutter')
329+
expect(flags('awebp')).toBe('fl_awebp')
300330
});
301331

302332
it('should return legal string for non-empty array', () => {
303-
expect(flags(['cutter', 'hello'])).toBe('fl_cutter:hello')
333+
expect(flags(['awebp', 'any_format'])).toBe('fl_awebp:any_format')
304334
});
305335
})
306336

packages/url/__tests__/url.test.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { url, getPrefix, getPathToAsset, getVersion, getSignature, extractPublicId } from '../lib/url'
1+
import { SEO_TYPES, STORAGE_TYPES } from '../lib/constants'
2+
import { url, getPrefix, getPathToAsset, getVersion, getSignature, extractPublicId, getSubDomain, getResourceType } from '../lib/url'
23

34
describe('Url', () => {
45
describe('getPrefix()', () => {
@@ -119,6 +120,11 @@ describe('Url', () => {
119120
})
120121

121122
describe('url()', () => {
123+
it('should throw error if no cloudname', () => {
124+
expect(() => {
125+
url('123')
126+
}).toThrow('cloudName is required!')
127+
});
122128
it('should return delivery url', () => {
123129
expect(url('example', {
124130
cloudName: 'demo',
@@ -207,4 +213,58 @@ describe('Url', () => {
207213
expect(extractPublicId('example')).toEqual('example')
208214
})
209215
})
216+
217+
describe('getSubDomain', () => {
218+
it('should return empty string if no cname', () => {
219+
expect(getSubDomain('abc', {})).toBe('res')
220+
});
221+
222+
it('should return res subdomain if cdnSubdomain but no cname', () => {
223+
expect(getSubDomain('abe', { cdnSubdomain: true })).toBe('res-abe')
224+
});
225+
226+
it('should return a subdomain if cname', () => {
227+
expect(getSubDomain('abe', { cname: 'a', cdnSubdomain: true })).toBe('aabe.')
228+
});
229+
});
230+
231+
describe('getResourceType', () => {
232+
it('should return shortern root path', () => {
233+
expect(getResourceType({
234+
shortern: true,
235+
useRootPath: true
236+
})).toBe('iu')
237+
});
238+
239+
it('should return empty string if not shortern but use root path', () => {
240+
expect(getResourceType({
241+
shortern: false,
242+
useRootPath: true
243+
})).toBe('')
244+
});
245+
246+
it('should throw error if use root path for not supported type', () => {
247+
expect(() => {
248+
getResourceType({
249+
storageType: STORAGE_TYPES.AUTHENTICATED,
250+
useRootPath: true
251+
})
252+
}).toThrow('Root path only supported for image/upload')
253+
});
254+
255+
it('should return url suffix', () => {
256+
expect(getResourceType({
257+
urlSuffix: 'h'
258+
})).toBe(SEO_TYPES["image/upload"])
259+
});
260+
261+
it('should throw error if not found seo', () => {
262+
expect(() => {
263+
getResourceType({
264+
storageType: STORAGE_TYPES.FACEBOOK,
265+
urlSuffix: 'h'
266+
})
267+
}).toThrow(`URL Suffix only supported for ${Object.keys(SEO_TYPES).join(', ')}`)
268+
});
269+
});
210270
})

packages/url/__tests__/variables.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@ describe('computeVariable()', () => {
4141

4242
expect(computeVariable(variable)).toBe('$small_!breadth!_to_f')
4343
});
44+
45+
it('should return empty string if no expression', () => {
46+
expect(computeVariable({
47+
name: 'small',
48+
value: {
49+
expression: ''
50+
}
51+
})).toBe('')
52+
});
53+
54+
it('should return mapped expression from array', () => {
55+
expect(computeVariable({
56+
name: 'small',
57+
value: {
58+
expression: ['a', 'b']
59+
}
60+
})).toBe('$small_!a:b!')
61+
});
4462
});
4563

4664
describe('variables()', () => {
@@ -76,5 +94,5 @@ describe('variables()', () => {
7694
}
7795
}]
7896
expect(variables(input)).toBe('$small_!breadth!_to_f,$big_2_mul_$small')
79-
})
97+
})
8098
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { fps } from "../lib/transformers/video/fps";
2+
import { offset } from "../lib/transformers/video/offset";
3+
4+
describe('Video transformation', () => {
5+
describe('fps()', () => {
6+
it('should map the fps accordingly', () => {
7+
expect(fps(90)).toBe('fps_90')
8+
});
9+
10+
it('should return empty if there is no input', () => {
11+
expect(fps()).toBe('')
12+
});
13+
14+
it('should map fps with min', () => {
15+
expect(fps({
16+
min: 90
17+
})).toBe('fps_90')
18+
});
19+
20+
it('should map fps with max and min', () => {
21+
expect(fps({
22+
min: 90,
23+
max: 100
24+
})).toBe('fps_90-100')
25+
});
26+
});
27+
28+
describe('offset()', () => {
29+
it('should return empty string when no input', () => {
30+
expect(offset()).toBe('')
31+
});
32+
33+
it('should return start offset', () => {
34+
expect(offset({
35+
start: 10
36+
})).toBe('so_10')
37+
});
38+
39+
it('should return start offset', () => {
40+
expect(offset({
41+
end: 10
42+
})).toBe('eo_10')
43+
});
44+
45+
it('should return start, end offset', () => {
46+
expect(offset({
47+
start: 10,
48+
end: 10
49+
})).toBe('so_10,eo_10')
50+
});
51+
52+
it('should return start, end offset and duration', () => {
53+
expect(offset({
54+
start: 10,
55+
end: 10,
56+
duration: "70"
57+
})).toBe('so_10,eo_10,du_70')
58+
});
59+
});
60+
});

0 commit comments

Comments
 (0)