11import { ElementRef } from "@angular/core" ;
22import { IkImageComponent } from "../../lib/src/imagekitio-angular/ik-image/ik-image.component" ;
33import { ImagekitService } from "../../lib/src/imagekitio-angular/imagekit.service" ;
4+ const pjson = require ( "../../lib/package.json" ) ;
5+ const version = `angular-${ pjson . version } ` ;
46
57describe ( "IkImageComponent" , ( ) => {
68 let component : IkImageComponent ;
79 let imageKitService : ImagekitService ;
810
911 beforeEach ( ( ) => {
1012 imageKitService = new ImagekitService ( {
11- urlEndpoint : "url " ,
12- publicKey : "pub " ,
13- authenticationEndpoint : "auth"
13+ urlEndpoint : "https://ik.imagekit.io/company/ " ,
14+ publicKey : "abc " ,
15+ authenticationEndpoint : "http://example.com/ auth"
1416 } ) ;
1517 let elRef : ElementRef ;
1618 component = new IkImageComponent ( elRef , imageKitService ) ;
1719 } ) ;
1820
21+ it ( "urlEndpoint passed to component should be used over initialized value" , ( ) => {
22+ component . setUrl ( null , "def" , null , null , 'localhost:3000' ) ;
23+ expect ( component . url ) . toBe ( `localhost:3000/def?ik-sdk-version=${ version } ` ) ;
24+ } ) ;
25+
26+ it ( "Presence and absence of trailing slash in should not result in double slash (//) in the returned url" , ( ) => {
27+ let comp : IkImageComponent ;
28+ let iKService : ImagekitService ;
29+ iKService = new ImagekitService ( {
30+ urlEndpoint : "https://ik.imagekit.io/company" ,
31+ publicKey : "abc" ,
32+ authenticationEndpoint : "http://example.com/auth"
33+ } ) ;
34+ let elRef : ElementRef ;
35+ comp = new IkImageComponent ( elRef , iKService ) ;
36+
37+ comp . setUrl ( null , "/abc.png" ) ;
38+ expect ( comp . url ) . toBe (
39+ `https://ik.imagekit.io/company/abc.png?ik-sdk-version=${ version } `
40+ ) ;
41+ iKService = new ImagekitService ( {
42+ urlEndpoint : "https://ik.imagekit.io/company/" ,
43+ publicKey : "abc" ,
44+ authenticationEndpoint : "http://example.com/auth"
45+ } ) ;
46+ comp . setUrl ( null , "/def.png" ) ;
47+ expect ( comp . url ) . toBe (
48+ `https://ik.imagekit.io/company/def.png?ik-sdk-version=${ version } `
49+ ) ;
50+ } ) ;
51+
52+ it ( "Presence and absence of leading slash in parameter should not result in double slash (//) in the returned url" , ( ) => {
53+ let comp : IkImageComponent ;
54+ let iKService : ImagekitService ;
55+ iKService = new ImagekitService ( {
56+ urlEndpoint : "https://ik.imagekit.io/company/" ,
57+ publicKey : "abc" ,
58+ authenticationEndpoint : "http://example.com/auth"
59+ } ) ;
60+ let elRef : ElementRef ;
61+ comp = new IkImageComponent ( elRef , iKService ) ;
62+ comp . setUrl ( null , "/abc.png" ) ;
63+ expect ( comp . url ) . toContain (
64+ `https://ik.imagekit.io/company/abc.png?ik-sdk-version=${ version } `
65+ ) ;
66+ comp . setUrl ( null , "abc.png" ) ;
67+ expect ( comp . url ) . toContain (
68+ `https://ik.imagekit.io/company/abc.png?ik-sdk-version=${ version } `
69+ ) ;
70+ } ) ;
71+
72+ it ( "new unsupported transformation parameter is passed then it should come in URL as it is" , ( ) => {
73+ const transformation = [ { foo : "200" , bar : "200" } ] ;
74+ component . setUrl ( "https://abc.com/def" , null , transformation ) ;
75+ expect ( component . url ) . toBe ( `https://abc.com/def?ik-sdk-version=${ version } &tr=foo-200%2Cbar-200` ) ;
76+ } ) ;
77+
78+ it ( "supported transformation parameter is passed then it should come in query parameters after transformation" , ( ) => {
79+ const transformation = [ { height : "200" , width : "200" } , { rotation : "90" } ] ;
80+ component . setUrl ( "https://abc.com/def" , null , transformation ) ;
81+ expect ( component . url ) . toBe ( `https://abc.com/def?ik-sdk-version=${ version } &tr=h-200%2Cw-200%3Art-90` ) ;
82+ } ) ;
83+
84+ it ( "if SRC is used to create URL, transformartioPosition should be query" , ( ) => {
85+ const config = component . getConfigObject ( 'abc' ) ;
86+ expect ( config [ 'transformationPosition' ] ) . toBe ( 'query' )
87+ } ) ;
88+
89+ it ( "if SRC is used to create URL, transformartioPosition should be query even if anything else is passed" , ( ) => {
90+ const transformation = [ { height : "200" , width : "200" } , { rotation : "90" } ] ;
91+ const config = component . getConfigObject ( 'https://example.com/ab.png' , null , transformation , 'path' , null ) ;
92+ expect ( config [ 'transformationPosition' ] ) . toBe ( 'query' )
93+ component . setUrl ( 'https://example.com/ab.png' , null , transformation , 'path' , null )
94+ expect ( component . url ) . toContain ( '&tr=' ) ;
95+ } ) ;
96+
97+ it ( "Parameters passed to queryParameters should be present in URL if src is used" , ( ) => {
98+ component . setUrl ( 'https://example.com/ab.png' , null , null , null , null , null , { version :5 , name : 'check' } ) ;
99+ expect ( component . url ) . toContain ( '&version=5&name=check' ) ;
100+ } ) ;
101+
102+ it ( "Parameters passed to queryParameters should be present in URL if src with existing query is used" , ( ) => {
103+ component . setUrl ( 'https://example.com/ab.png?foo=bar&baz=nax' , null , null , null , null , null , { version :5 , name : 'check' } ) ;
104+ expect ( component . url ) . toContain ( '&version=5&name=check' ) ;
105+ expect ( component . url ) . toBe ( 'https://example.com/ab.png?foo=bar&baz=nax&ik-sdk-version=angular-0.0.1&version=5&name=check' ) ;
106+ } ) ;
107+
108+ it ( "Parameters passed to queryParameters should be present in URL if path is used" , ( ) => {
109+ component . setUrl ( null , '/default.png' , null , null , null , null , { version :6 , name : 'bar' } ) ;
110+ expect ( component . url ) . toContain ( '&version=6&name=bar' ) ;
111+ } ) ;
112+
19113 it ( "setUrl should create correct URL when src is provided" , ( ) => {
20114 component . setUrl ( "abc" ) ;
21- expect ( component . url ) . toContain ( " /abc?ik-sdk-version=angular-" ) ;
115+ expect ( component . url ) . toContain ( ` /abc?ik-sdk-version=${ version } ` ) ;
22116 } ) ;
23117
24118 it ( "setUrl should create correct URL when path is provided" , ( ) => {
25- component . setUrl ( null , "def" ) ;
26- expect ( component . url ) . toContain ( "/url/ def?ik-sdk-version=angular-" ) ;
119+ component . setUrl ( null , "def" , null , null ) ;
120+ expect ( component . url ) . toContain ( `https://ik.imagekit.io/company/ def?ik-sdk-version=${ version } ` ) ;
27121 } ) ;
28122
29123 it ( "setUrl should create correct lqipURL in addition to URL when lqip is provided" , ( ) => {
30124 component . setUrl ( "abc" , null , null , { active : true , quality : 1 } ) ;
31- expect ( component . url ) . toContain ( " /abc?ik-sdk-version=angular-" ) ;
125+ expect ( component . url ) . toContain ( ` /abc?ik-sdk-version=${ version } ` ) ;
32126 expect ( component . lqipUrl ) . toContain ( "tr=q-1" ) ;
33127 } ) ;
34128
@@ -49,54 +143,4 @@ describe("IkImageComponent", () => {
49143 ) ;
50144 expect ( lqipURl ) . toContain ( "tr=q-10" ) ;
51145 } ) ;
52-
53- it ( "setUrl should add transformations in query parameters" , ( ) => {
54- const transformation = [
55- { height : "200" , width : "200" } ,
56- {
57- rotation : "90"
58- }
59- ] ;
60- component . setUrl ( "abc" , null , transformation ) ;
61- expect ( component . url ) . toContain ( "&tr=h-200%2Cw-200%3Art-90" ) ;
62- } ) ;
63-
64- it ( "setUrl should handle the presence and absence of leading slash in path parameters" , ( ) => {
65- let comp : IkImageComponent ;
66- let iKService : ImagekitService ;
67- iKService = new ImagekitService ( {
68- urlEndpoint : "https://ik.imagekit.io/company/" ,
69- publicKey : "abc" ,
70- authenticationEndpoint : "http://example.com/auth"
71- } ) ;
72- let elRef : ElementRef ;
73- comp = new IkImageComponent ( elRef , iKService ) ;
74- comp . setUrl ( null , "/abc.png" ) ;
75- expect ( comp . url ) . toContain ( "https://ik.imagekit.io/company/abc.png?ik-sdk-version=angular-" ) ;
76- comp . setUrl ( null , "abc.png" ) ;
77- expect ( comp . url ) . toContain ( "https://ik.imagekit.io/company/abc.png?ik-sdk-version=angular-" ) ;
78- } ) ;
79-
80- it ( "setUrl should handle the presence and absence of leading slash in urlEndpoint parameters" , ( ) => {
81- let comp : IkImageComponent ;
82- let iKService : ImagekitService ;
83- iKService = new ImagekitService ( {
84- urlEndpoint : "https://ik.imagekit.io/company" ,
85- publicKey : "abc" ,
86- authenticationEndpoint : "http://example.com/auth"
87- } ) ;
88- let elRef : ElementRef ;
89- comp = new IkImageComponent ( elRef , iKService ) ;
90-
91- comp . setUrl ( null , "/abc.png" ) ;
92- expect ( comp . url ) . toContain ( "https://ik.imagekit.io/company/abc.png?ik-sdk-version=angular-" ) ;
93- iKService = new ImagekitService ( {
94- urlEndpoint : "https://ik.imagekit.io/company/" ,
95- publicKey : "abc" ,
96- authenticationEndpoint : "http://example.com/auth"
97- } ) ;
98- comp . setUrl ( null , "/def.png" ) ;
99- expect ( comp . url ) . toContain ( "https://ik.imagekit.io/company/def.png?ik-sdk-version=angular-" ) ;
100- } ) ;
101-
102146} ) ;
0 commit comments