|
| 1 | +import {createCloudinaryV1URL} from "../../src/backwards/createCloudinaryURL"; |
| 2 | +import {generateTransformationString} from "../../src/backwards/generateTransformationString"; |
| 3 | + |
| 4 | +describe('Create v1 urls', () => { |
| 5 | + it('works', () => { |
| 6 | + const url = createCloudinaryV1URL('sample', { |
| 7 | + cloud_name: 'demo', |
| 8 | + width:100 |
| 9 | + }); |
| 10 | + |
| 11 | + expect(url).toBe('https://res.cloudinary.com/demo/image/upload/w_100/sample'); |
| 12 | + }); |
| 13 | + |
| 14 | + it('Sets resource_type and type', () => { |
| 15 | + const url = createCloudinaryV1URL('sample', { |
| 16 | + cloud_name: 'demo', |
| 17 | + resource_type: 'video', |
| 18 | + type: 'fetch', |
| 19 | + width:100 |
| 20 | + }); |
| 21 | + |
| 22 | + expect(url).toBe('https://res.cloudinary.com/demo/video/fetch/w_100/sample'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('Sets resource_type and type', () => { |
| 26 | + const url = createCloudinaryV1URL('sample', { |
| 27 | + cloud_name: 'demo', |
| 28 | + resource_type: 'video', |
| 29 | + type: 'fetch', |
| 30 | + width:100 |
| 31 | + }); |
| 32 | + |
| 33 | + expect(url).toBe('https://res.cloudinary.com/demo/video/fetch/w_100/sample'); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should include conditional transformation", function () { |
| 37 | + let url = createCloudinaryV1URL("test", { |
| 38 | + cloud_name: 'test123', |
| 39 | + resource_type: 'video', |
| 40 | + if: "duration > 30", |
| 41 | + width: 100 |
| 42 | + }); |
| 43 | + expect(url).toEqual("https://res.cloudinary.com/test123/video/upload/if_du_gt_30,w_100/test"); |
| 44 | + url = createCloudinaryV1URL("test", { |
| 45 | + cloud_name: 'test123', |
| 46 | + resource_type: 'video', |
| 47 | + if: "initialDuration > 30", |
| 48 | + width: 100 |
| 49 | + }); |
| 50 | + expect(url).toEqual("https://res.cloudinary.com/test123/video/upload/if_idu_gt_30,w_100/test"); |
| 51 | + url = createCloudinaryV1URL("test", { |
| 52 | + cloud_name: 'test123', |
| 53 | + resource_type: 'video', |
| 54 | + if: "initial_duration > 30", |
| 55 | + width: 100 |
| 56 | + }); |
| 57 | + expect(url).toEqual("https://res.cloudinary.com/test123/video/upload/if_idu_gt_30,w_100/test"); |
| 58 | + }); |
| 59 | + |
| 60 | + it("should allow multiple conditions when chaining transformations ", function () { |
| 61 | + const url = createCloudinaryV1URL("sample", { |
| 62 | + cloud_name: 'test123', |
| 63 | + transformation: [ |
| 64 | + { |
| 65 | + if: "w_lt_200", |
| 66 | + crop: "fill", |
| 67 | + height: 120, |
| 68 | + width: 80 |
| 69 | + }, |
| 70 | + { |
| 71 | + if: "w_gt_400", |
| 72 | + crop: "fit", |
| 73 | + width: 150, |
| 74 | + height: 150 |
| 75 | + }, |
| 76 | + { |
| 77 | + effect: "sepia" |
| 78 | + } |
| 79 | + ] |
| 80 | + }); |
| 81 | + expect(url).toEqual("https://res.cloudinary.com/test123/image/upload/if_w_lt_200,c_fill,h_120,w_80/if_w_gt_400,c_fit,h_150,w_150/e_sepia/sample"); |
| 82 | + }); |
| 83 | + |
| 84 | + it("should translate operators", function () { |
| 85 | + const url = createCloudinaryV1URL("sample", { |
| 86 | + cloud_name: 'test123', |
| 87 | + if: "w < 200", |
| 88 | + crop: "fill", |
| 89 | + height: 120, |
| 90 | + width: 80 |
| 91 | + }); |
| 92 | + expect(url).toEqual("https://res.cloudinary.com/test123/image/upload/if_w_lt_200,c_fill,h_120,w_80/sample"); |
| 93 | + }); |
| 94 | + |
| 95 | + it("should allow multiple tags condition", function () { |
| 96 | + const url = createCloudinaryV1URL("sample", { |
| 97 | + cloud_name: 'test123', |
| 98 | + transformation: [ |
| 99 | + { |
| 100 | + if: "!tag1:tag2:tag3!_in_tags", |
| 101 | + crop: "fill", |
| 102 | + height: 120, |
| 103 | + width: 80 |
| 104 | + }, |
| 105 | + { |
| 106 | + if: "else", |
| 107 | + crop: "fit", |
| 108 | + width: 150, |
| 109 | + height: 150 |
| 110 | + }, |
| 111 | + { |
| 112 | + effect: "sepia" |
| 113 | + } |
| 114 | + ] |
| 115 | + }); |
| 116 | + expect(url).toEqual("https://res.cloudinary.com/test123/image/upload/if_!tag1:tag2:tag3!_in_tags,c_fill,h_120,w_80/if_else,c_fit,h_150,w_150/e_sepia/sample"); |
| 117 | + }); |
| 118 | + |
| 119 | + it("should include the if_end as the last parameter in its component", function () { |
| 120 | + const url = createCloudinaryV1URL("sample", { |
| 121 | + cloud_name: 'test123', |
| 122 | + transformation: [ |
| 123 | + { |
| 124 | + if: "w_lt_200" |
| 125 | + }, |
| 126 | + { |
| 127 | + crop: "fill", |
| 128 | + height: 120, |
| 129 | + width: 80, |
| 130 | + effect: "sharpen" |
| 131 | + }, |
| 132 | + { |
| 133 | + effect: "brightness:50" |
| 134 | + }, |
| 135 | + { |
| 136 | + effect: "shadow", |
| 137 | + color: "red" |
| 138 | + }, |
| 139 | + { |
| 140 | + if: "end" |
| 141 | + } |
| 142 | + ] |
| 143 | + }); |
| 144 | + expect(url).toEqual("https://res.cloudinary.com/test123/image/upload/if_w_lt_200/c_fill,e_sharpen,h_120,w_80/e_brightness:50/co_red,e_shadow/if_end/sample"); |
| 145 | + }); |
| 146 | + |
| 147 | + it("should support if_else with transformation parameters", function () { |
| 148 | + const url = createCloudinaryV1URL("sample", { |
| 149 | + cloud_name: 'test123', |
| 150 | + transformation: [ |
| 151 | + { |
| 152 | + if: "w_lt_200", |
| 153 | + crop: "fill", |
| 154 | + height: 120, |
| 155 | + width: 80 |
| 156 | + }, |
| 157 | + { |
| 158 | + if: "else", |
| 159 | + crop: "fill", |
| 160 | + height: 90, |
| 161 | + width: 100 |
| 162 | + } |
| 163 | + ] |
| 164 | + }); |
| 165 | + expect(url).toEqual("https://res.cloudinary.com/test123/image/upload/if_w_lt_200,c_fill,h_120,w_80/if_else,c_fill,h_90,w_100/sample"); |
| 166 | + }); |
| 167 | + |
| 168 | + it("if_else should be without any transformation parameters", function () { |
| 169 | + const url = createCloudinaryV1URL("sample", { |
| 170 | + cloud_name: 'test123', |
| 171 | + transformation: [ |
| 172 | + { |
| 173 | + if: "aspect_ratio_lt_0.7" |
| 174 | + }, |
| 175 | + { |
| 176 | + crop: "fill", |
| 177 | + height: 120, |
| 178 | + width: 80 |
| 179 | + }, |
| 180 | + { |
| 181 | + if: "else" |
| 182 | + }, |
| 183 | + { |
| 184 | + crop: "fill", |
| 185 | + height: 90, |
| 186 | + width: 100 |
| 187 | + } |
| 188 | + ] |
| 189 | + }); |
| 190 | + expect(url).toEqual("https://res.cloudinary.com/test123/image/upload/if_ar_lt_0.7/c_fill,h_120,w_80/if_else/c_fill,h_90,w_100/sample"); |
| 191 | + }); |
| 192 | + |
| 193 | + it("should support and translate operators: '=', '!=', '<', '>', '<=', '>=', '&&', '||'", function () { |
| 194 | + const allOperators = 'if_' + 'w_eq_0_and' + '_h_ne_0_or' + '_ar_lt_0_and' + '_pc_gt_0_and' + '_fc_lte_0_and' + '_w_gte_0' + ',e_grayscale'; |
| 195 | + |
| 196 | + expect(createCloudinaryV1URL("sample", { |
| 197 | + cloud_name: 'test123', |
| 198 | + "if": "w = 0 && height != 0 || aspectRatio < 0 and pageCount > 0 and faceCount <= 0 and width >= 0", |
| 199 | + "effect": "grayscale" |
| 200 | + })).toMatch(new RegExp(allOperators)); |
| 201 | + }); |
| 202 | + |
| 203 | + it("should support text values", function () { |
| 204 | + const url = createCloudinaryV1URL("sample", { |
| 205 | + cloud_name: 'demo', |
| 206 | + effect: "$efname:100", |
| 207 | + $efname: "!blur!" |
| 208 | + }); |
| 209 | + expect(url).toBe(`https://res.cloudinary.com/demo/image/upload/$efname_!blur!,e_$efname:100/sample`); |
| 210 | + }); |
| 211 | + |
| 212 | + it("should support string interpolation", function () { |
| 213 | + const url = createCloudinaryV1URL("sample", { |
| 214 | + cloud_name: 'demo', |
| 215 | + crop: "scale", |
| 216 | + overlay: { |
| 217 | + text: "$(start)Hello $(name)$(ext), $(no ) $( no)$(end)", |
| 218 | + font_family: "Arial", |
| 219 | + font_size: "18" |
| 220 | + } |
| 221 | + }); |
| 222 | + |
| 223 | + expect(url).toContain(`l_text:Arial_18:$(start)Hello%20$(name)$(ext)%252C%20%24%28no%20%29%20%24%28%20no%29$(end)/sample`); |
| 224 | + }); |
| 225 | + |
| 226 | + it("should escape public_ids", function () { |
| 227 | + const expressions = [ |
| 228 | + // [source, target] |
| 229 | + ["a b", "a%20b"], |
| 230 | + ["a+b", "a%2Bb"], |
| 231 | + ["a%20b", "a%20b"], |
| 232 | + ["a-b", "a-b"], |
| 233 | + ["a??b", "a%3F%3Fb"], |
| 234 | + ["parentheses(interject)", "parentheses(interject)"], |
| 235 | + ["abcαβγאבג", "abc%CE%B1%CE%B2%CE%B3%D7%90%D7%91%D7%92"] |
| 236 | + ]; |
| 237 | + expressions.forEach(([source, target]) => { |
| 238 | + expect(createCloudinaryV1URL(source, {cloud_name: 'demo'})).toEqual(`https://res.cloudinary.com/demo/image/upload/${target}`); |
| 239 | + }); |
| 240 | + }); |
| 241 | +}); |
0 commit comments