Skip to content

Commit 1937751

Browse files
author
Amir Tocker
committed
Don't convert keys to snake_case in normalizeOptions. Fixes #6.
1 parent e4f486e commit 1937751

File tree

5 files changed

+51
-7
lines changed

5 files changed

+51
-7
lines changed

src/components/CloudinaryComponent/CloudinaryComponent.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, {Component, PropTypes} from 'react';
22
import {Cloudinary, Configuration, Transformation, Util} from 'cloudinary-core';
3+
import {mapKeys} from '../../Util'
34

45
const camelCase = Util.camelCase;
56
const snakeCase = Util.snakeCase;
@@ -64,7 +65,7 @@ export default class CloudinaryComponent extends Component {
6465
for (let key in right) {
6566
let value = right[key];
6667
if (value !== null && value !== undefined) {
67-
left[snakeCase(key)] = value;
68+
left[key] = value;
6869
}
6970
}
7071
return left;
@@ -79,8 +80,8 @@ export default class CloudinaryComponent extends Component {
7980
*/
8081
getUrl(options) {
8182
let transformation = this.getTransformation(options);
82-
let cl = Cloudinary.new(options);
83-
return cl.url(options.public_id, transformation);
83+
let cl = Cloudinary.new(Util.withSnakeCaseKeys(options));
84+
return cl.url(options.publicId, transformation);
8485
}
8586

8687
}

src/components/Image/Image.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export default class Image extends CloudinaryComponent {
102102
}
103103

104104
render() {
105-
var {public_id, responsive, responsive_debounce, children, ...options} = CloudinaryComponent.normalizeOptions(this.props,
105+
var {publicId, responsive, responsiveDebounce, children, ...options} = CloudinaryComponent.normalizeOptions(this.props,
106106
this.context);
107107
var attributes = cloudinary.Transformation.new(options).toHtmlAttributes();
108108
return (

src/components/Video/Video.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ export default class Video extends CloudinaryComponent {
2020
sourceTransformation = sourceTransformation || {};
2121
sourceTypes = sourceTypes || Cloudinary.DEFAULT_VIDEO_PARAMS.source_types;
2222
options = CloudinaryComponent.normalizeOptions(options, {});
23-
let cld = Cloudinary.new(options);
23+
let cld = Cloudinary.new(Util.withSnakeCaseKeys(options));
2424
let sources = [];
2525
let tagAttributes = Transformation.new(options).toHtmlAttributes();
2626
if (Util.isPlainObject(poster)) {
2727
let defaults = poster.publicId !== undefined && poster.publicId !== null ? Cloudinary.DEFAULT_IMAGE_PARAMS : DEFAULT_POSTER_OPTIONS;
28-
poster = cld.url(poster.publicId || publicId, Util.defaults({}, poster, defaults));
28+
poster = cld.url(poster.publicId || publicId, Util.defaults({}, Util.withSnakeCaseKeys(poster), defaults));
2929
}
3030
if (!Util.isEmpty(poster)) {
3131
tagAttributes.poster = poster;

stories/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ storiesOf('Image', module).addWithInfo('image', "Basic tag", ()=> {
3838
<Image {...t} cloudName="demo" publicId="sample" style={{border: "20px solid"}}/>
3939
)
4040
}
41+
).addWithInfo('image with onError', 'image with onError', ()=> {
42+
let t = {width: 0.5, crop: "scale"};
43+
let onError = (e)=> { e.target.insertAdjacentHTML("afterend", "<div>Error handler was invoked.</div>")};
44+
return (
45+
<div>
46+
<Image {...t} cloudName="demo" publicId="i-dont-exist" style={{border: "20px solid"}} onError={onError}/>
47+
</div>
48+
)
49+
}
4150
).addWithInfo('image with chained transformation', 'image with chained transformation', ()=> {
4251
return (
4352
<div>
@@ -99,7 +108,7 @@ storiesOf('Video', module).addWithInfo('Simple tag', 'Simple tag', ()=> {
99108
}
100109
).addWithInfo('With source types', 'With source types', ()=> {
101110
return (
102-
<Video cloudName="demo" controls="controls" publicId="dog" sourceTypes={['webm', 'ogv', 'mp4']}>
111+
<Video cloudName="demo" controls="controls" publicId="dog" sourceTypes={['webm', 'ogv', 'mp4']} sourceTransformation={{webm: {aspectRatio: "1:1"}, ogv: {aspect_ratio: "3:2"}}}>
103112
Cannot display video.
104113
</Video>
105114
)

test/ImageTest.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,38 @@ describe('Image', () => {
1717
expect(tag.type()).to.equal("img");
1818
expect(tag.state("url")).to.equal("http://res.cloudinary.com/demo/image/upload/c_scale,w_300/sample");
1919
});
20+
it("should set event handlers", function() {
21+
let width = 300;
22+
let tag = shallow(<Image publicId="sample" cloudName="demo" width={width} crop="scale" onLoad={()=> "foo"}/>);
23+
expect(tag.type()).to.equal("img");
24+
expect(tag.state("url")).to.equal("http://res.cloudinary.com/demo/image/upload/c_scale,w_300/sample");
25+
expect(tag.props().onLoad()).to.equal("foo")
26+
27+
});
28+
it("should not pass-through Cloudinary attributes", function() {
29+
let width = 300;
30+
31+
let tag2 = mount(<div width="300"><Image publicId="sample" cloudName="demo" width="auto" crop="scale" privateCdn="private" defaultImage="foobar" responsive responsiveUseBreakpoints /></div>);
32+
let tag = shallow(<Image publicId="sample" cloudName="demo" width="auto" crop="scale" privateCdn="private" defaultImage="foobar" responsive responsiveUseBreakpoints />);
33+
expect(tag.type()).to.equal("img");
34+
expect(tag.state("url")).to.equal("");
35+
expect(tag.props()).to.have.property('src');
36+
37+
// We are checking for both snake_case and camelCase keys
38+
expect(tag.props()).not.to.have.property('privateCdn');
39+
expect(tag.props()).not.to.have.property('private_cdn');
40+
expect(tag.props()).not.to.have.property('defaultImage');
41+
expect(tag.props()).not.to.have.property('default_image');
42+
expect(tag.props()).not.to.have.property('responsiveUseBreakpoints');
43+
expect(tag.props()).not.to.have.property('responsive_use_breakpoints');
44+
tag = shallow(<Image publicId="sample" cloudName="demo" width={width} crop="scale" private_cdn="private" default_image="foobar"/>);
45+
expect(tag.type()).to.equal("img");
46+
expect(tag.state("url")).to.equal("http://demo-res.cloudinary.com/image/upload/c_scale,d_foobar,w_300/sample");
47+
expect(tag.props()).to.have.property('src');
48+
expect(tag.props()).not.to.have.property('privateCdn');
49+
expect(tag.props()).not.to.have.property('private_cdn');
50+
expect(tag.props()).not.to.have.property('defaultImage');
51+
expect(tag.props()).not.to.have.property('default_image');
52+
53+
});
2054
});

0 commit comments

Comments
 (0)