Skip to content

Commit a715c16

Browse files
authored
Merge pull request #11 from mayashavin/improvements
2 parents fcc852b + 388e6ee commit a715c16

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+2048
-600
lines changed

docs/url-docs/content/en/usage/transformations.md

Lines changed: 529 additions & 301 deletions
Large diffs are not rendered by default.

docs/url-docs/content/en/usage/transformer.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ const transArr = Transformer.transform({
3030
},
3131
gravity: 'auto',
3232
chaining: [{
33-
effect: 'grayscale'
33+
effect: {
34+
name: 'grayscale'
35+
}
3436
}]
3537
})
3638

package.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
"workspaces": [
66
"packages/*"
77
],
8+
"husky": {
9+
"hooks": {
10+
"pre-commit": "yarn test:unit"
11+
}
12+
},
813
"scripts": {
914
"build": "lerna run build --stream",
1015
"prelint": "yarn build",
@@ -15,10 +20,14 @@
1520
"release": "lerna publish --conventional-commits",
1621
"pretest": "yarn build && jest --rootDir __tests__",
1722
"test": "lerna run test --stream",
23+
"test:unit": "yarn test:url",
1824
"pretest:ci": "yarn pretest",
1925
"test:ci": "lerna run test:ci --stream",
2026
"prettier": "yarn prettier --write .",
21-
"changelog": "node scripts/changelog.js"
27+
"changelog": "node scripts/changelog.js",
28+
"test:url": "npm --prefix packages/url run test:unit",
29+
"bundlewatch:url": "npm --prefix packages/url run bundlewatch",
30+
"doc-dev:url": "npm --prefix docs/url-docs run dev"
2231
},
2332
"author": "Maya Shavin <[email protected]>",
2433
"license": "MIT",
@@ -33,7 +42,8 @@
3342
"jest": "^26.6.3",
3443
"lerna": "^3.22.1",
3544
"prettier": "^2.1.2",
36-
"typescript": "^4.0.5"
45+
"typescript": "^4.0.5",
46+
"husky": "^4.3.0"
3747
},
3848
"keywords": [
3949
"cloudinary",
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { computeConditionExpression, computeCondition, condition } from '../lib/transformers/condition'
2+
3+
describe('Condition modifier', () => {
4+
describe('condition()', () => {
5+
it('should return empty string if nothing passed', () => {
6+
expect(condition()).toBe('')
7+
});
8+
9+
it('should return empty string if condition.if does not exist', () => {
10+
expect(condition({
11+
if: {
12+
transformations: [{ rotate: 20 }]
13+
}
14+
})).toBe('')
15+
});
16+
17+
it('should return if condition string', () => {
18+
expect(condition({
19+
if: {
20+
expression: [{
21+
characteristic: "width",
22+
operator: 'equal',
23+
value: 200
24+
}],
25+
transformations: [{ rotate: 20 }]
26+
}
27+
})).toEqual(['if_w_eq_200/a_20/if_end'])
28+
});
29+
30+
it('should return if and else condition string', () => {
31+
expect(condition({
32+
if: {
33+
expression: [{
34+
characteristic: "width",
35+
operator: 'equal',
36+
value: 200
37+
}],
38+
transformations: [{ rotate: 20 }]
39+
},
40+
else: {
41+
transformations: [{
42+
resize: {
43+
width: 120,
44+
height: 120,
45+
type: 'fill'
46+
}
47+
}]
48+
}
49+
})).toEqual(['if_w_eq_200/a_20/if_else/c_fill,w_120,h_120/if_end'])
50+
});
51+
it('should return if value expression with mapped', () => {
52+
expect(condition({
53+
if: {
54+
expression: [{
55+
characteristic: "width",
56+
operator: 'equal',
57+
value: 'shoes'
58+
}],
59+
transformations: [{ rotate: 20 }]
60+
},
61+
})).toEqual(['if_w_eq_!shoes!/a_20/if_end'])
62+
});
63+
});
64+
65+
describe('computeConditionExpression()', () => {
66+
it('should map all characteristic together', () => {
67+
expect(computeConditionExpression({
68+
characteristic: ['width', 'height'],
69+
operator: 'include',
70+
value: 'shoes'
71+
})).toBe('w:h_in_!shoes!')
72+
});
73+
});
74+
75+
describe('computeCondition()', () => {
76+
it('should map multiple conditions together', () => {
77+
expect(computeCondition({
78+
expression: [{
79+
characteristic: 'aspectRatio',
80+
operator: 'greaterThan',
81+
value: 3
82+
}, {
83+
characteristic: 'width',
84+
operator: 'greaterOrEqual',
85+
value: 300
86+
}, {
87+
characteristic: 'height',
88+
operator: 'lessThan',
89+
value: 20
90+
}],
91+
transformations: [{
92+
resize: {
93+
width: 300,
94+
height: 200,
95+
type: 'crop'
96+
}
97+
}]
98+
})).toEqual({
99+
expression: 'ar_gt_3_and_w_gte_300_and_h_lt_20',
100+
transformations: 'c_crop,w_300,h_200'
101+
})
102+
});
103+
});
104+
});
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
})

0 commit comments

Comments
 (0)