File tree Expand file tree Collapse file tree 2 files changed +43
-2
lines changed
Filter options
projects/angular-cld/src/lib Expand file tree Collapse file tree 2 files changed +43
-2
lines changed
Original file line number Diff line number Diff line change @@ -308,6 +308,39 @@ describe('CloudinaryImage', () => {
308
308
} ) ;
309
309
} ) ;
310
310
311
+ describe ( 'should support user variables' , ( ) => {
312
+ @Component ( {
313
+ template :
314
+ `
315
+ <cl-image public-id="sample">
316
+ <cl-transformation variables="[['$imgWidth','150']]"></cl-transformation>
317
+ <cl-transformation width="$imgWidth" crop="crop" ></cl-transformation>
318
+ </cl-image>
319
+ `
320
+ } )
321
+ class TestComponent { }
322
+
323
+ let fixture : ComponentFixture < TestComponent > ;
324
+ let des : DebugElement ; // the elements w/ the directive
325
+
326
+ beforeEach ( ( ) => {
327
+ fixture = TestBed . configureTestingModule ( {
328
+ declarations : [ CloudinaryTransformationDirective , CloudinaryImage , TestComponent ] ,
329
+ providers : [ { provide : Cloudinary , useValue : localCloudinary } ]
330
+ } ) . createComponent ( TestComponent ) ;
331
+
332
+ fixture . detectChanges ( ) ; // initial binding
333
+
334
+ // Our element under test, which is attached to CloudinaryImage
335
+ des = fixture . debugElement . query ( By . directive ( CloudinaryImage ) ) ;
336
+ } ) ;
337
+
338
+ it ( 'creates an img element with user variables' , ( ) => {
339
+ const img = des . children [ 0 ] . nativeElement as HTMLImageElement ;
340
+ expect ( img . attributes . getNamedItem ( 'src' ) . value ) . toEqual ( 'http://res.cloudinary.com/@@fake_angular2_sdk@@/image/upload/$imgWidth_150/c_crop,w_$imgWidth/sample' ) ;
341
+ } ) ;
342
+ } ) ;
343
+
311
344
describe ( 'Sample code presented in README' , ( ) => {
312
345
@Component ( {
313
346
template :
Original file line number Diff line number Diff line change @@ -15,6 +15,10 @@ const isJsonLikeString = function (str: any): boolean {
15
15
return str && typeof str === 'string' && ( str . trim ( ) . match ( / ^ { [ \s \S ] * ?} $ / ) !== null ) ;
16
16
} ;
17
17
18
+ const isArrayLikeString = function ( str : any ) : boolean {
19
+ return str && typeof str === 'string' && ( str . trim ( ) . match ( / ^ \[ [ \s \S ] * ?] $ / ) !== null ) ;
20
+ } ;
21
+
18
22
const isNamedNodeMap = function ( obj : any ) : boolean {
19
23
return obj && ( obj . constructor . name === 'NamedNodeMap' || obj instanceof NamedNodeMap ) ;
20
24
} ;
@@ -31,9 +35,13 @@ const namedNodeMapToObject = function (source: NamedNodeMap): any {
31
35
32
36
const transformKeyNames = function ( obj : any ) : any {
33
37
let _obj = obj ;
34
- if ( isJsonLikeString ( obj ) ) {
38
+ if ( isJsonLikeString ( obj ) || isArrayLikeString ( obj ) ) {
35
39
// Given attribute value is in the form of a JSON object -
36
- // Transforms the string into an object, as the Javascript API expects
40
+ // Transforms the string into an object or array, as the Javascript API expects
41
+
42
+ if ( isArrayLikeString ( obj ) ) {
43
+ obj = obj . replace ( / ' / g, '"' ) ;
44
+ }
37
45
_obj = JSON . parse ( obj ) ;
38
46
} else if ( isNamedNodeMap ( obj ) ) {
39
47
_obj = namedNodeMapToObject ( obj ) ;
You can’t perform that action at this time.
0 commit comments