Skip to content

Commit 097a4e8

Browse files
eitanp461tocker
authored andcommitted
Use nextProps for updating calculated URL correctly (#73)
1 parent 01625aa commit 097a4e8

File tree

3 files changed

+59
-45
lines changed

3 files changed

+59
-45
lines changed

src/components/CloudinaryComponent/CloudinaryComponent.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,20 @@ class CloudinaryComponent extends Component {
4747
/**
4848
* Returns an object with all the transformation parameters based on the context and properties of this element
4949
* and any children.
50-
* @param options
50+
* @param extendedProps
5151
* @returns {object} a hash of transformation and configuration parameters
5252
* @protected
5353
*/
54-
getTransformation(options) {
54+
getTransformation(extendedProps) {
5555
var transformation;
56-
if (this.props.children !== undefined) {
57-
let childrenOptions = this.getChildTransformations(this.props.children);
56+
if (extendedProps.children !== undefined) {
57+
let childrenOptions = this.getChildTransformations(extendedProps.children);
5858
if (!Util.isEmpty(childrenOptions)) {
5959
transformation = childrenOptions;
60-
return {...options, transformation};
60+
return {...extendedProps, transformation};
6161
}
6262
}
63-
return {...options};
63+
return {...extendedProps};
6464
}
6565

6666
/**
@@ -85,14 +85,14 @@ class CloudinaryComponent extends Component {
8585

8686
/**
8787
* Generate a Cloudinary resource URL based on the options provided and child Transformation elements
88-
* @param options
88+
* @param extendedProps React props combined with custom Cloudinary configuration options
8989
* @returns {string} a cloudinary URL
9090
* @protected
9191
*/
92-
getUrl(options) {
93-
let transformation = this.getTransformation(options);
94-
let cl = Cloudinary.new(Util.withSnakeCaseKeys(options));
95-
return cl.url(options.publicId, transformation);
92+
getUrl(extendedProps) {
93+
let transformation = this.getTransformation(extendedProps);
94+
let cl = Cloudinary.new(Util.withSnakeCaseKeys(extendedProps));
95+
return cl.url(extendedProps.publicId, transformation);
9696
}
9797

9898
}

src/components/Image/Image.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ class Image extends CloudinaryComponent {
4545
* @private
4646
*/
4747
prepareState(props = this.props, context = this.context) {
48-
let options = CloudinaryComponent.normalizeOptions(context, props);
49-
let url = this.getUrl(options);
48+
let extendedProps = CloudinaryComponent.normalizeOptions(context, props);
49+
let url = this.getUrl(extendedProps);
5050
let state = {};
51-
if (options.breakpoints !== undefined) {
52-
state.breakpoints = options.breakpoints;
51+
if (extendedProps.breakpoints !== undefined) {
52+
state.breakpoints = extendedProps.breakpoints;
5353
}
54-
if (options.responsive) {
54+
if (extendedProps.responsive) {
5555
state.responsive = true;
5656
url = this.cloudinary_update(url, state);
5757
}

test/TransformationTest.js

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,58 @@
1-
import React from 'react';
2-
import { expect } from 'chai';
3-
import { shallow, mount, render } from 'enzyme';
4-
import Image from '../src/components/Image';
5-
import Transformation from '../src/components/Transformation';
1+
import React from "react";
2+
import { expect } from "chai";
3+
import { shallow, mount, render } from "enzyme";
4+
import Image from "../src/components/Image";
5+
import Transformation from "../src/components/Transformation";
66

7-
8-
describe('Transformation', () => {
9-
beforeEach(() => {
10-
});
7+
describe("Transformation", () => {
8+
beforeEach(() => {});
119
it("should create an img tag", function() {
1210
let tag = shallow(
13-
<Image publicId="sample" cloudName="demo" >
14-
<Transformation width="100" crop="scale" angle="10"/>
11+
<Image publicId="sample" cloudName="demo">
12+
<Transformation width="100" crop="scale" angle="10" />
1513
</Image>
1614
);
1715
expect(tag.name()).to.equal("img");
18-
expect(tag.state("url")).to.equal("http://res.cloudinary.com/demo/image/upload/a_10,c_scale,w_100/sample");
16+
expect(tag.state("url")).to.equal(
17+
"http://res.cloudinary.com/demo/image/upload/a_10,c_scale,w_100/sample"
18+
);
1919
});
2020
it("should allow chained transformations", function() {
2121
let tag = shallow(
22-
<Image publicId="sample" cloudName="demo" >
23-
<Transformation width="100" crop="scale"/>
22+
<Image publicId="sample" cloudName="demo">
23+
<Transformation width="100" crop="scale" />
2424
<Transformation width="200" crop="pad">
25-
<Transformation angle="30"/>
25+
<Transformation angle="30" />
2626
</Transformation>
2727
</Image>
2828
);
2929
expect(tag.type()).to.equal("img");
30-
expect(tag.state("url")).to.equal("http://res.cloudinary.com/demo/image/upload/c_scale,w_100/a_30/c_pad,w_200/sample");
30+
expect(tag.state("url")).to.equal(
31+
"http://res.cloudinary.com/demo/image/upload/c_scale,w_100/a_30/c_pad,w_200/sample"
32+
);
33+
});
34+
it("array should define a set of variables", function() {
35+
let tag = shallow(
36+
<Image
37+
cloudName="demo"
38+
publicId="sample"
39+
variables={[["$z", 5], ["$foo", "$z * 2"]]}
40+
/>
41+
);
42+
expect(tag.type()).to.equal("img");
43+
expect(tag.state("url")).to.equal(
44+
"http://res.cloudinary.com/demo/image/upload/$z_5,$foo_$z_mul_2/sample"
45+
);
46+
});
47+
it("updates transformations dynamically", function() {
48+
let image = mount(
49+
<Image publicId="sample" cloudName="demo">
50+
<Transformation width="100" crop="scale" />
51+
</Image>
52+
);
53+
expect(image.find('img').getElement().props.src).to.equal('http://res.cloudinary.com/demo/image/upload/c_scale,w_100/sample');
54+
let transformation = mount(<Transformation width="200" crop="scale" />);
55+
image.setProps({children: [transformation]});
56+
expect(image.find('img').getElement().props.src).to.equal('http://res.cloudinary.com/demo/image/upload/c_scale,w_200/sample');
3157
});
32-
it("array should define a set of variables", function () {
33-
let tag = shallow(
34-
<Image cloudName='demo'
35-
publicId='sample'
36-
variables={[
37-
["$z", 5], ["$foo", "$z * 2"]
38-
]}
39-
>
40-
</Image>);
41-
expect(tag.type()).to.equal("img");
42-
expect(tag.state("url")).to.equal("http://res.cloudinary.com/demo/image/upload/$z_5,$foo_$z_mul_2/sample");
43-
});
44-
});
58+
});

0 commit comments

Comments
 (0)